My server recv() the packet from the client and must display it.
Problem is its not displaying the correct data
Here is the code:
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
for ( int i = 0; i < iResult; i++ ) {
printf( "%02x ", buf[i] );
}
printf( "\n" );
// return "cc cc cc cc cc cc cc cc cc cc"
// correct return "0A 00 2C 01 29 00 0D 00 B4 01"
When a client connects the server displays cc cc cc cc cc cc cc cc cc cc but that’s not right
It should be 0A 00 2C 01 29 00 0D 00 B4 01
UPDATE
#define DEFAULT_BUFLEN 512
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
for ( int i = 0; i < iResult; i++ ) {
printf( "%02x ", recvbuf[i] );
}
printf( "\n" );
// return "0A 00 2C 01 23 00 0c 00 ffffffB3 01"
// correct return "0A 00 2C 01 23 00 0c 00 B3 01"
now there are extra ffffff on the packet, even though i’m only recv 10 bytes.
The data is correct now. What you’re seeing is sign extension when the
charvalue you’re passing toprintfis promoted to anint. You can cast thecharto anunsigned charand it should display properly.