I am trying to send image frames over the network using Winsock. I know array length and their dimensions, so I'm just using a char buffer of sufficient constant size. The following conversion works fine and in real time:
char buffer[BUFFER_SIZE]; //BUFFER_SIZE = 1000000
...
UINT16 *pBuffer = NULL; //send buffer
UINT16 *pBuffer2 = NULL; //receive buffer
...
//copying to the buffer of correct length to avoid errors.
//Empirically found that it seems contiguous
memcpy(&buffer[1], pBuffer, nBufferSize*2);//img is int16, nBufferSize*2 ~ 400000
buffer[0] = '~'; //adding special symbol to mark correct data stream
// here supposed to be send-receive with buffer
//convert received result back to UINT16
pBuffer2 = (UINT16*)&buffer[1];
Here is how correct image from pBuffer2 looks like:

Now I'm sending this char buffer over the network:
client_send(buffer);
//receiving frame from server
receive_data(buffer);
Where the functions look like the following:
Client part(send):
#define DEFAULT_BUFLEN 1000000 //1MB
...
int client_send(char *sendbuf) {
// Send an initial buffer
//iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
iResult = send(ConnectSocket, sendbuf, DEFAULT_BUFLEN, 0);
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %ld\n", iResult);
return 0;
}
Server part(receive) - I truncated the code to one client at the moment:
const int BUFFER_SIZE = 1000000;//1MB
...
int receive_client(_client *current_client, char *buffer, int size)
{
if (FD_ISSET(current_client->socket, ¤t_client->socket_data))
{
// Store the return data of what we have sent
current_client->address_length = recv(current_client->socket, buffer, size, 0);
if (current_client->address_length == 0)
{ // Data error on client
disconnect_client(current_client);
return (FALSE);
}
return (TRUE);
}
return (FALSE);
}
int receive_data(char* buffer)
{
//char buffer[BUFFER_SIZE];
for (int j = 0; j < MAX_CLIENTS; j++)
{
if (client[j].connected)
{
if (receive_client(&client[j], buffer, BUFFER_SIZE))
{
if (buffer[0] == '~') {
// if we have correct packet
return 1;
//buffer[0] = '/0';
}
}
}
}
return 0;
}
The result becomes randomly messed up like frame was randomly shifted in XY plane, and the shift is constant over time (though different for different sessions):
I have ensured that correct stream begins with ~ symbol and I have no idea on the nature of that shift.

send/recvin the code above with char array of constant size and it worked fine for text messages, what I need to do to reassemble image chunks correctly? - SlowpokenBufferSize*2(after conversion touint16) on both sides and I am receiving a message with single buffer of size 1000000(which is bigger than image written into it) inrecvfunction, having~at the beginning which is supposed to be followed by image data. Then I access this buffer data by pointer that points to the first element. What do you mean by reassembling it? Do you mean that I need to receive image in one byte per packet? - Slowpoke