title: Java + Raspberry Pi + ServerSocket
body: <p>Hello StackOverflow Community,</p>
  
  <p>I have a specific question launching a <a href="https://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html" rel="nofollow noreferrer">java.net.ServerSocket</a> on a Raspberry Pi Zero and connect to it with a <a href="https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html" rel="nofollow noreferrer">java.net.Socket</a>. The RPi is configured as a Wireless Access Point, so I can connect to it via WLAN with more than one Laptop/Tablets. The purpose is to launch a server-client game with the RPi as the server (without the need of a separate network and a server running on a device) and some devices as the clients (that can connect to the RPi).</p>
  
  <p>I tried the following four situations, all more than once to prove that they always show the same result:</p>
  
  <ol>
  <li><p>I connected to the RPi via WLAN with a tablet. Then i launched the ServerSocket on that tablet. After that I launched a Socket on that tablet and connected it to the ServerSocket. The connection worked and data was transmitted in both directions.</p></li>
  <li><p>I connected to the RPi via WLAN with a tablet. Then i launched the ServerSocket on that tablet. After that I launched a Socket <em>on the RPi</em> and connected it to the ServerSocket running on my tablet. The connection worked and data was transmitted in both directions.</p></li>
  <li><p>I connected to the RPi via WLAN with a tablet. Then i launched the ServerSocket <em>on the RPi</em>. After that I launched a Socket on my tablet and connected it to the ServerSocket running on my RPi. The first weird thing was that the IP of the ServerSocket is 0.0.0.0, the second problem that the client failed to connect to the RPi and therefore was not able to transmit data.</p></li>
  <li><p>I connected to the RPi via WLAN with a tablet. Then i launched the ServerSocket <em>on the RPi</em> and bind it to a specified IP address. After that I launched a Socket on my tablet and connect it to the ServerSocket running on my RPi. The ServerSocket failed to bind to the given IP address, therefore the Socket failed to connect to the Server and therefore no data was transmitted.</p></li>
  </ol>
  
  <p>Why is no IP bound (except 0.0.0.0) to the ServerSocket? Why can't I connect to the IP-Address 0.0.0.0? How can I get the ServerSocket running on the RPi with Java?</p>
  
  <p>Here are the java source codes I used for all three situations:</p>
  
  <p>Server:</p>
  
  <pre><code>import java.net.ServerSocket;
  import java.net.InetAddress;
  import java.net.Socket;
  import java.net.InetSocketAddress;
  
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  
  public class Server {
      private     ServerSocket    ss;
  
      public static void main(String[] args) {
          new Server();       // call the constructor
      }
  
      public Server() {
          try {
              ss = new ServerSocket(4444);    // create the ServerSocket
              new Thread(new Runnable() {     // create new Thread for listening to new clients
                  public void run() {
                      startAccept();          // run the accept-clients method
                  }
              }).start();     // don't forget to start the thread
              System.out.println(InetAddress.getLocalHost().getHostAddress());    // print the IP-Address
              System.out.println("4444");     // print the Port
          } catch (Exception e) {
              e.printStackTrace();        // show exceptions
          }
      }
  
      public void startAccept() {
          while (true) {      // listen to new clients
              try {
                  Socket so = ss.accept();    // accept the clients and create reader/writer
                  BufferedReader bure = new BufferedReader(new InputStreamReader(so.getInputStream()));
                  BufferedWriter buwr = new BufferedWriter(new OutputStreamWriter(so.getOutputStream()));
                  new Thread(new Runnable() {     // start new thread for reading from the client
                      public void run() {
                          readSocket(bure, buwr); // run the read/write method of the client
                      }
                  }).start();     // don't forget to start the thread
              } catch (Exception e) {
                  e.printStackTrace();        // show exceptions
              }
          }
      }
  
      public void readSocket(BufferedReader bure, BufferedWriter buwr) {
          while (true) {      // listen to all messages sent by the client
              try {
                  String l1 = bure.readLine();            // read first line
                  System.out.println("Rec1: &lt;"+l1+"&gt;");   // show it
                  String l2 = bure.readLine();            // read second line
                  System.out.println("Rec2: &lt;"+l2+"&gt;");   // show it
                  buwr.write(l1+" + "+l2+" = "+(Integer.parseInt(l1)+Integer.parseInt(l2))+"\n");
                  buwr.flush();       // convert both lines to integers and send the sum of them
              } catch (Exception e) {
                  e.printStackTrace();        // show exceptions
              }
          }
      }
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>import java.net.InetAddress;
  import java.net.InetSocketAddress;
  import java.net.Socket;
  
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  
  public class Client {
      public static void main(String[] args) {        // start the program
          try {
              Socket so = new Socket();       // create an unbound socket
              so.connect(new InetSocketAddress("0.0.0.0", 4444), 10000);
                  // bind socket to the IP-address printed by the Server and create reader/writer
              BufferedReader bure = new BufferedReader(new InputStreamReader(so.getInputStream()));
              BufferedWriter buwr = new BufferedWriter(new OutputStreamWriter(so.getOutputStream()));
              buwr.write("7\n4\n");   // send 7 and 4, both followed by a new line
              buwr.flush();
              System.out.println("Sent: &lt;7\n4\n&gt;");   // show the user what has been sent
              System.out.println("Message from Server: "+bure.readLine());    // sum sent by the server
              so.close();     // close socket
          } catch (Exception e) {
              e.printStackTrace();    // show exception
          }
      }
  }
  </code></pre>
  
  <p>I appreciate in advance for any help on this topic.</p>