Im having trouble understanding what/why the following is happening
The server sends the following 6 bytes data to the client.
Send - Server
Code:
//Send - Server
int RetVal;
char pd[6]; //Packet Data
pd[0] = 'A';
pd[1] = 'B';
pd[2] = 'C';
pd[3] = 'D';
pd[4] = 'E';
pd[5] = 'F';
RetVal = send(remoteConnection->clientsocketfd,pd,6,NULL);
The client reads only 3 bytes of the available 6 bytes sent.
Recv - Client
Code:
//Recv - Client
char rd[8]; //Received Data
recv(remoteConnection->socketfd,(char*) rd,3,NULL);
printf("\n0 Byte = %c",rd[0]);
printf("\n1 Byte = %c",rd[1]);
printf("\n2 Byte = %c",rd[2]);
printf("\n3 Byte = %c",rd[3]);
printf("\n4 Byte = %c",rd[4]);
printf("\n5 Byte = %c",rd[5]);
printf("\n6 Byte = %c",rd[6]);
printf("\n7 Byte = %c",rd[7]);
Output
Code:
//Output
0 Byte = D
1 Byte = E
2 Byte = F
3 Byte = ¦ //Null
4 Byte = ¦ //Null
5 Byte = ¦ //Null
6 Byte = ¦ //Null
7 Byte = ¦ //Null
The problem is when the received data is read its the last 3 bytes of data.
I was expecting it to be the first 3 bytes to be 'A','B' and 'C' of the data that was sent
not the last 3 which is 'D','E' and 'F'.
What I’m trying to do is as described in Beej's Guide to Network Programming Chapter 6.5. Son of Data Encapsulation
Where the length of a packet is put as a header as a first byte. The receiver receives the first few bytes then knows from the header how much more bytes he needs to read to get the full packet.
But by receiving the last few bytes instead of the first few Im having trouble to do this because the packet length (which is the first byte) isn’t read.
By the way im using WinSocks
Thanks