biprinea biprinea - 1 year ago 71
Apache Configuration Question

Send/Receive a .txt file from client to server

I'm beginner in socket programming now I want to send a .txt file's content to server and receive it in the server and save it in a file to server.

I should use C/C++ code for client and php for server.

I use a MSDN sample code to connect to server and send a buffer to server but I don't know How to receive the buffer in the server.

Client side:

#ifndef UNICODE
#define UNICODE


#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>

// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT 27015

int main() {

// Declare and initialize variables.
int iResult;
WSADATA wsaData;

struct sockaddr_in clientService;

int recvbuflen = DEFAULT_BUFLEN;
char *sendbuf = "Client: sending data test";
char recvbuf[DEFAULT_BUFLEN] = "";

// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup failed with error: %d\n", iResult);
return 1;

// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
return 1;

// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "" );
clientService.sin_port = htons( DEFAULT_PORT );

// Connect to server.
iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
if (iResult == SOCKET_ERROR) {
wprintf(L"connect failed with error: %d\n", WSAGetLastError() );
return 1;

// Send an initial buffer
iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
wprintf(L"send failed with error: %d\n", WSAGetLastError());
return 1;

printf("Bytes Sent: %d\n", iResult);

// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
wprintf(L"shutdown failed with error: %d\n", WSAGetLastError());
return 1;

// Receive until the peer closes the connection
do {

iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
wprintf(L"Bytes received: %d\n", iResult);
else if ( iResult == 0 )
wprintf(L"Connection closed\n");
wprintf(L"recv failed with error: %d\n", WSAGetLastError());

} while( iResult > 0 );

// close the socket
iResult = closesocket(ConnectSocket);
if (iResult == SOCKET_ERROR) {
wprintf(L"close failed with error: %d\n", WSAGetLastError());
return 1;

return 0;

  • Is this code correct to send a .txt file's content to server?

  • What should I do for Server Side?


Answer Source

You should form a request string and send that string to the server. For example if your server accepts query string like this

http://localhost/test.php?input=this is a test

which is equivalent to


You would form a request like this:

"GET /test.php?input=this%20is%20a%20test HTTP/1.1\r\nConnection: Close\r\nHost: localhost\r\n\r\n""

You should also read the response code sent by the server if you want to know if it succeeded or not.

This is the server response from my machine:

HTTP/1.1 200 OK
Date: Sun, 02 Oct 2016 12:01:12 GMT
Server: Apache/2.4.17 (Win64) PHP/5.6.16
X-Powered-By: PHP/5.6.16
Content-Length: 14
Connection: close
Content-Type: text/html; charset=UTF-8

this is a test

from test.php file


    echo $_GET["input"];
    echo "Didn't get any query string";

Let me know if that helps

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download