title: Sending string from Java (PC) to C (Raspberry Pi) via Socket
body: <p>I have two apps, one written in Java that sends string, it looks like this:</p>
  
  <pre><code>    import java.io.*;
  import java.net.*;
  public class Gniazdo{
          public static void main(String[] args) throws IOException {
              DataOutputStream dataOutputStream = null;
              Socket socket = null;
              String mes = "Test message";
              try {
                  socket = new Socket("192.168.1.116", 4014);
                  dataOutputStream = new DataOutputStream(socket.getOutputStream());
  
                  dataOutputStream.writeChars(mes); 
              } catch (UnknownHostException e) {
                  System.err.print(e);
  
              } finally {
                  if(socket != null) {
                      socket.close();
                  }
                  if(dataOutputStream != null) {
                      dataOutputStream.close();
                  }
              }
          }
  
      }
  </code></pre>
  
  <p>and second one is written in C and runs on Raspberry Pi, that should receive string :</p>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;string.h&gt;
  
  int main(void) {
      unsigned int port;
      char bufor[1024];
      int gniazdo, gniazdo2;
      struct sockaddr_in adr, nadawca;
      socklen_t dl;
  
      printf("Enter port to listen : ");
      scanf("%u", &amp;port);
      gniazdo = socket(AF_INET, SOCK_STREAM, 0);
      adr.sin_family = AF_INET;
      adr.sin_port = htons(port);
      adr.sin_addr.s_addr = INADDR_ANY;
      if (bind(gniazdo, (struct sockaddr*) &amp;adr, 
               sizeof(adr)) &lt; 0) {
          printf("Bind failed.\n");
          return 1;
      }
      if (listen(gniazdo, 10) &lt; 0) {
          printf("Listen failed.\n");
          return 1;        
      }
      printf("Waiting for connection ...\n");
      while (gniazdo2 = accept(gniazdo,
                        (struct sockaddr*) &amp;nadawca,
                        &amp;dl)
            ) 
      {
     //     memset(bufor, 0, 1024);
          recv(gniazdo2, bufor, 1024, 0);
          printf("message from %s: %s\n", inet_ntoa(nadawca.sin_addr), bufor);
          close(gniazdo2);
      }
      close(gniazdo);    
  }
  </code></pre>
  
  <p>and my output is:</p>
  
  <pre><code>Enter port to listen : 4014
  Waiting for connection ...
  message from 192.168.1.103:
  message from 192.168.1.103:
  message from 192.168.1.103:
  message from 192.168.1.103:
  </code></pre>
  
  <p>Please could anyone tell me whats wrong?
  Why i don't receive the message?</p>
  
  <p>I even checked packets outgoing from my computer with Wireshark and they contains my message, but still don't know where I made mistake</p>