title: C-Server Socket become wrong Packet
body: <p>I have the following problem:</p>
  
  <ul>
  <li>I have a Java-client using the apache mina framework to send string.</li>
  <li>I wrote a C server to receive the string.</li>
  <li>So, I tried to send an utf8 character to the server:myStringToSend = "Ä"</li>
  <li>I debugged the mina source to see the packet in byte or hex and the mina convert the string well, so it sent <code>0xC3</code> <code>0x84</code>.</li>
  <li>I also check the sent packet in wireshark network viewer and It see also <code>0xC3</code> <code>0x84</code> as packet, all ok.</li>
  <li>But my C-server receive the following bytes: <code>FFFFFFC3FFFFFF84</code></li>
  </ul>
  
  <p>I don't know what's going wrong?</p>
  
  <p>The code of my C-Server:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;io.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;winsock2.h&gt;
  
  #pragma comment(lib,"ws2_32.lib") //Winsock Library
  
  #define DEFAULT_BUFLEN 512
  #define TOOBIG 100000
  
  int main(int argc, char *argv[])
  {
  WSADATA wsa;
  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),&amp;wsa) != NO_ERROR)
  {
      printf("Failed. Error Code : %d",WSAGetLastError());
      return 1;
  }
  printf("Initialised.\n");     
  
  if((listenSocket = socket(AF_INET , SOCK_STREAM , IPPROTO_TCP )) == INVALID_SOCKET) //IPv4, TCP
  {
      printf("Could not create socket : %d" , WSAGetLastError());
      WSACleanup();
      return 1;
  }
  
  printf("Socket created.\n");
  
  memset(&amp;server, '0', sizeof(server));
  memset(sendbuf, '0', sizeof(sendbuf)); 
  
  server.sin_family = AF_INET;
  //server.sin_addr.s_addr = htonl(0x7F000001); //localhost - 127.0.0.1
  server.sin_addr.s_addr = inet_addr("127.0.0.1");
  //server.sin_addr.s_addr = inet_addr("10.48.53.166");
  server.sin_port = htons(7368); 
  
  if( bind(listenSocket ,(struct sockaddr *)&amp;server , sizeof(server)) == SOCKET_ERROR)
  {
      printf("Bind failed with error code : %d" , WSAGetLastError());
      closesocket(listenSocket);
      WSACleanup();
      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 &gt; 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());
                  closesocket(clientSocket);
                  WSACleanup();
                  return 1;
              }
              printf("Bytes sent: %d\n", iSendResult);
          }
          else if ( iResult == 0 )
              printf("Connection closed\n");
          else
              printf("recv failed: %d\n", WSAGetLastError());
      } while( iResult &gt; 0 );
  
   }
  if (clientSocket == INVALID_SOCKET)
  {
      printf("accept failed with error code : %d" , WSAGetLastError());
      return 1;
  }
  closesocket(listenSocket);
  WSACleanup();
  
  return 0;
  }
  </code></pre>