title: testing a client server
body: <p>I wrote a server that listens for client messages, it's a variation of <a href="http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html" rel="nofollow">http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html</a>.  I wrote them both in eclipse as java classes in the same project.  To test it I have a client class with a main that starts the server and then sends messages to it.  When I run it the program just hangs at serverSocket.accept(); according to the javadoc for ServerSocket accept is not asynchronous?  That would explain the hanging, but then how does the tutorial code work then?</p>
  
  <p>UPDATE - here is my working code:
  Here is the working code:</p>
  
  <p>MyServer.java</p>
  
  <pre><code>/*imports neglected for brevity */
  public class MyServer {
  public static final String hostname = "localhost";
  public static final int portNum = 4444;
  
  ServerSocket serverSocket;
  BufferedReader serverReader;
  
  File serverLog;
  FileWriter fw;
  BufferedWriter serverWriter;
  
  Socket clientSocket;
  
  public static void main(String[] args) {
  MyServer server = new MyServer(portNum);
      // start the server so it can listen to client messages
      server.start();
  
  
  }
  
  
  public MyServer(int portNum) {
  
      try {
          // endpt for server side, used to listen for client socket
          serverSocket = new ServerSocket(portNum);
  
          /* have server socket listen for connection, return client socket.
           * serverSocket can now talk to clientSocket
           */
          clientSocket = serverSocket.accept();
  
          // server writes messages to this log
          serverLog = new File("ServerLog.txt");
          if(!serverLog.exists())
              serverLog.createNewFile();
          fw = new FileWriter(serverLog.getAbsoluteFile());
          serverWriter = serverWriter = new BufferedWriter(fw);
  
          /* server reads from this stream that is populated by the client's
           * OUTPUT stream/client socket's INPUT stream
           */
          serverReader = new BufferedReader(
              new InputStreamReader(clientSocket.getInputStream())
                                                );
      }    
      catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  public void start() {
      String clientMsg;
      try {
          while((clientMsg = serverReader.readLine()) != null) {
              if(clientMsg.startsWith("exit")) {
                  break;
              }
              serverWriter.append(clientMsg);
          }
          serverWriter.flush();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>MyClient.java
          public class MyClient {
          public static final String hostname = "localhost";
          public static final int portNum = 4444;</p>
  
  <pre><code>public static void main(String[] args) {
  
      String msg = "message 1";
      try {
          // server is listening on http://localhost:4444
          Socket serversSocket = new Socket(hostname, portNum);
          PrintWriter clientOut = new     PrintWriter(serversSocket.getOutputStream(), true);
  
          // send first message
          clientOut.println(msg);
  
          msg = "message 2";
          // send second message
          clientOut.println(msg);
  
          msg = "exit";
          // this will stop the server
          clientOut.println(msg);
      }
      catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  }
  </code></pre>