user2963830 user2963830 - 1 year ago 129
C Question

C-Server Socket become wrong Packet

I have the following problem:

  • I have a Java-client using the apache mina framework to send string.

  • I wrote a C server to receive the string.

  • So, I tried to send an utf8 character to the server:myStringToSend = "Ä"

  • I debugged the mina source to see the packet in byte or hex and the mina convert the string well, so it sent

  • I also check the sent packet in wireshark network viewer and It see also
    as packet, all ok.

  • But my C-server receive the following bytes:

I don't know what's going wrong?

The code of my C-Server:

#include "stdafx.h"
#include <io.h>
#include <stdio.h>
#include <winsock2.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

#define DEFAULT_BUFLEN 512
#define TOOBIG 100000

int main(int argc, char *argv[])
int i, c, iResult, iSendResult, outputCounter;

SOCKET listenSocket = INVALID_SOCKET, clientSocket = INVALID_SOCKET;
struct sockaddr_in server, client;
char *message;

char sendbuf[DEFAULT_BUFLEN];
unsigned char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;

printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != NO_ERROR)
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;

if((listenSocket = socket(AF_INET , SOCK_STREAM , IPPROTO_TCP )) == INVALID_SOCKET) //IPv4, TCP
printf("Could not create socket : %d" , WSAGetLastError());
return 1;

printf("Socket created.\n");

memset(&server, '0', sizeof(server));
memset(sendbuf, '0', sizeof(sendbuf));

server.sin_family = AF_INET;
//server.sin_addr.s_addr = htonl(0x7F000001); //localhost -
server.sin_addr.s_addr = inet_addr("");
//server.sin_addr.s_addr = inet_addr("");
server.sin_port = htons(7368);

if( bind(listenSocket ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
printf("Bind failed with error code : %d" , WSAGetLastError());
return 1;

puts("Bind done");

listen(listenSocket, 10);

//Accept and incoming connection
puts("Waiting for incoming connections...");

c = sizeof(struct sockaddr_in);

message = "{\"id\":2,\"result\":{\"code\":\"999\",\"message\":\"AddPageRange StartIndex don't match Inspection Counter\"},\"Client_ID\":\"PQCS1\",\"jsonrpc\":\"2.0\",\"Protocol\":\"2H\"}";

while( (clientSocket = accept(listenSocket , (struct sockaddr *)NULL, NULL)) != INVALID_SOCKET )
puts("Connection accepted");
do {
iResult = recv(clientSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
printf("Bytes received: %d\n", iResult);
recvbuf[iResult] = '\0';
outputCounter = 0;
while(recvbuf[outputCounter] != '\0')
printf("%X", (unsigned char)recvbuf[outputCounter++]);
iSendResult = send( clientSocket, message, strlen(message), 0 );
if (iSendResult == SOCKET_ERROR)
printf("send failed with error: %d\n", WSAGetLastError());
return 1;
printf("Bytes sent: %d\n", iSendResult);
else if ( iResult == 0 )
printf("Connection closed\n");
printf("recv failed: %d\n", WSAGetLastError());
} while( iResult > 0 );

if (clientSocket == INVALID_SOCKET)
printf("accept failed with error code : %d" , WSAGetLastError());
return 1;

return 0;

Answer Source

Your recvbuf is of type char which is suspect is signed on your platform. Both bytes of your message are > 127(decimal) and so are seen as being negative. The printf("%X") format specifier expects an integer (32 bit on your platform by the look of it) so the number gets sign extended (fill the most significant 24 bits of the integer with the most significant bit of the byte).

Try printing out how many bytes you received. Also change your printf to

printf("%X",  ((int)recvbuf[outputCounter++]) & 0xff);             
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download