title: Python 3 socket client sending data and C++ socket server receiving data with offset?
body: <p>I have made a small test to send an integer from a python application (client socket) to a c++ application (socket server) both are TCP stream sockets. I have also tested sending the same integer from the client socket in python to a server socket in python.</p>
  
  <p>What I do is I send an array of 4 bytes (char in c++), each char has the integer shifted right (>>) like this (python syntax):</p>
  
  <pre><code>...
  [my_int &gt;&gt; i &amp; 0xff for i in (24,16,8,0)]:
  ...
  </code></pre>
  
  <p>The problem is that when sending from the client socket in python to the server socket in python the data "arrives ok", for instance if I send the integer 1390248303</p>
  
  <p>the python server socket first prints the bytes stream received then for each byte it prints its ascii code and then I do: </p>
  
  <pre><code>sum([l[3-i] &lt;&lt; 8*i for i in (3,2,1,0)])
  </code></pre>
  
  <p>to "rebuild" the integer and this is the result (which is ok):</p>
  
  <pre><code>RECEIVED: b'R\xdd\x81o'
  82
  221
  129
  111
  RECEIVED: 1390248303
  </code></pre>
  
  <p>BUT the C++ server socket, in which I do the same but with more verbose in the code:</p>
  
  <pre><code>...
  int sum = 0;
  int term = 0;
  for(int i = 3;i &gt; -1;i--)
  {
      printf("%d\n",msg[3-i]);
      term = msg[3-i];
      //if (term &lt; 0)
      //  term = 256 + term;
      suma += term &lt;&lt; 8*i;                
  };
  printf("Received: %d\n",sum);
  ...
  </code></pre>
  
  <p>Outputs</p>
  
  <pre><code>82
  -35
  -127
  111
  Received: 1373405551
  </code></pre>
  
  <p>Did you see that the 2 bytes in the middle are different to the 2 bytes in the middle corresponding to what the server socket in python outputs? Not only that but if I add 256 to them they became the same:</p>
  
  <pre><code> -35 + 256 = 221
  -127 + 256 = 129
  </code></pre>
  
  <p>What is the reason of this behaviour? Thanks beforehand for any hint!</p>
  
  <p>Here is the code of the applications:</p>
  
  <p>python client socket:</p>
  
  <pre><code>import socket
  client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  client_socket.connect(("localhost", 7000))
  my_int = 1390248303
  my_bytes = bytearray()        
  for e in [my_int &gt;&gt; i &amp; 0xff for i in (24,16,8,0)]:
      my_bytes.append(e)
  print("To be sent:", my_bytes)    
  client_socket.send(my_bytes)
  print("Sent:", my_bytes)    
  client_socket.close()
  </code></pre>
  
  <p>python server socket:</p>
  
  <pre><code>import socket
  server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  server_socket.bind(("", 7000))
  server_socket.listen(5)
  
  print("TCPServer Waiting for client on port 7000")
  
  while 1:
      client_socket, address = server_socket.accept()
      print("I got a connection from ", address)
      while 1:
          data = client_socket.recv(32)
          print("RECEIVED:",data)
          l = []
          for e in data:
              l.append(e)
              print(e)
          print("RECEIVED:",sum([l[3-i] &lt;&lt; 8*i for i in (3,2,1,0)]))
          if (data == b''):
              break;
      break;
  </code></pre>
  
  <p>C++ server socket:</p>
  
  <pre><code>#define WIN32_LEAN_AND_MEAN
  
  #include &lt;WinSock2.h&gt;
  #include &lt;WS2tcpip.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  
  // link with Ws2_32.lib
  #pragma comment(lib, "Ws2_32.lib")
  
  #define DEFAULT_PORT "7000"
  //"27015"
  #define DEFAULT_BUFFER_LENGTH 32
  //512
  
  int main() {
  
      WSADATA wsaData;
  
      // Initialize Winsock
      int iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
      if(iResult != 0)
      {
          printf("WSAStartup failed: %d\n", iResult);
          return 1;
      }
  
      struct addrinfo *result = NULL,
                      hints;
  
      ZeroMemory(&amp;hints, sizeof(hints));
      hints.ai_family = AF_INET;      // Internet address family is unspecified so that either an IPv6 or IPv4 address can be returned
      hints.ai_socktype = SOCK_STREAM;    // Requests the socket type to be a stream socket for the TCP protocol
      hints.ai_protocol = IPPROTO_TCP;
      hints.ai_flags = AI_PASSIVE;
  
      // Resolve the local address and port to be used by the server
      iResult = getaddrinfo(NULL, DEFAULT_PORT, &amp;hints, &amp;result);
      if (iResult != 0)
      {
          printf("getaddrinfo failed: %d\n", iResult);
          WSACleanup();
          return 1;
      }
  
      SOCKET ListenSocket = INVALID_SOCKET;
  
      // Create a SOCKET for the server to listen for client connections
      ListenSocket = socket(result-&gt;ai_family, result-&gt;ai_socktype, result-&gt;ai_protocol);
  
      if (ListenSocket == INVALID_SOCKET)
      {
          printf("Error at socket(): %d\n", WSAGetLastError());
          freeaddrinfo(result);
          WSACleanup();
          return 1;
      }
  
      // Setup the TCP listening socket
      iResult = bind(ListenSocket, result-&gt;ai_addr, (int)result-&gt;ai_addrlen);
  
      if (iResult == SOCKET_ERROR)
      {
          printf("bind failed: %d", WSAGetLastError());
          freeaddrinfo(result);
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      freeaddrinfo(result);
  
      // To listen on a socket
      if ( listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
      {
          printf("listen failed: %d\n", WSAGetLastError());
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      SOCKET ClientSocket;
  
      ClientSocket = INVALID_SOCKET;
  
      // Accept a client socket
      ClientSocket = accept(ListenSocket, NULL, NULL);
  
      if (ClientSocket == INVALID_SOCKET)
      {
          printf("accept failed: %d\n", WSAGetLastError());
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      char recvbuf[DEFAULT_BUFFER_LENGTH];
      int iSendResult;
  
      // receive until the client shutdown the connection
      do {
          iResult = recv(ClientSocket, recvbuf, DEFAULT_BUFFER_LENGTH, 0);
          if (iResult &gt; 0)
          {
              char msg[DEFAULT_BUFFER_LENGTH];
              memset(&amp;msg, 0, sizeof(msg));
              strncpy(msg, recvbuf, iResult);         
  
              printf("Received: %s\n", msg);
                      //Here is where I implement the python code:
              //sum([l[3-i] &lt;&lt; 8*i for i in (3,2,1,0)]));
              int sum = 0;
              int term = 0;
              for(int i = 3;i &gt; -1;i--)
              {
                  printf("%d\n",msg[3-i]);
                  term = msg[3-i];
                  //if (term &lt; 0)
                  //  term = 256 + term;
                  sum += term &lt;&lt; 8*i;             
              };
              printf("Received: %d\n",sum);
  
              iSendResult = send(ClientSocket, recvbuf, iResult, 0);
  
              if (iSendResult == SOCKET_ERROR)
              {
                  printf("send failed: %d\n", WSAGetLastError());
                  closesocket(ClientSocket);
                  WSACleanup();
  
                  getchar();
                  return 1;
              }
  
              printf("Bytes sent: %ld\n", iSendResult);
          }
          else if (iResult == 0)
              printf("Connection closed\n");
          else
          {
              printf("recv failed: %d\n", WSAGetLastError());
              closesocket(ClientSocket);
              WSACleanup();
  
              getchar();
              return 1;
          }
      } while (iResult &gt; 0);
  
      // Free the resouces
      closesocket(ListenSocket);
      WSACleanup();
  
      getchar();
      //while (true){};
      return 0;
  }
  </code></pre>