title: Socket communication between Java and C++
body: <p>I'm trying to have a connection between a Java server and a C++ client. But when I read the data in my client I always have the same strange character (¬í). I tried to change the encoding in both side but nothing work.</p>
  
  <p>Here is my Java code : </p>
  
  <pre><code>public class Serveur
  {
      public static void main(String[] args) throws Exception
      {
          final int PORT = 13370;
          try
          {
              ServerSocket service= new ServerSocket(PORT);
              Socket connection = service.accept();
              PrintWriter pw = new PrintWriter(connection.getOutputStream());
              String s = Integer.toString(5);
              while(true)
              {
                  pw.print(s.getBytes("UTF-8"));
                  pw.flush();
                  pw.close();
              }
              connection.close();
          }
  }
  </code></pre>
  
  <p>I also tried to use an OutputStream, a DataOutputStream and a BufferedOutputStream.</p>
  
  <p>And here is the  C++ code : </p>
  
  <pre><code>int main(int argc, char* argv[])
  {
      WSADATA WSAData;
      WSAStartup(MAKEWORD(2,0), &amp;WSAData);
      SOCKET sock;
      SOCKADDR_IN sin;
      char buffer[512];
      sin.sin_addr.s_addr = inet_addr("127.0.0.1");
      sin.sin_family      = AF_INET;
      sin.sin_port        = htons(13370);
      sock = socket(AF_INET,SOCK_STREAM,0);
      if(connect(sock, (SOCKADDR*)&amp;sin, sizeof(sin)) != SOCKET_ERROR)
  {
      cout&lt;&lt;"connection"&lt;&lt;endl;
      if(recv(sock, buffer, sizeof(buffer), 0) != SOCKET_ERROR)
      {
          string s = buffer;
          wchar_t *pwchello = L"Hi";
          wchar_t *pwc      = (wchar_t *)malloc( sizeof( wchar_t ));
          char    *pmbhello = buffer;
          int i = mbstowcs(pwc,pmbhello, MB_CUR_MAX);
          cout &lt;&lt; i &lt;&lt; endl;
          cout&lt;&lt;"cout : "&lt;&lt;pwc&lt;&lt;endl;
          cout &lt;&lt;buffer&lt;&lt;endl;
          printf("printf : %s\n", buffer);
  
          cout &lt;&lt; "wsagetlasterror() : "&lt;&lt;WSAGetLastError();
          closesocket(sock);
          WSACleanup();
          free(m_pBuffer);
      }
      return 0;
  }
  </code></pre>
  
  <p>As you can see, I tried different solution but without success.</p>
  
  <p>Thanks in advance, and sorry for my english it may be not very good</p>