title: Permanent and persistent Socket connection in java
body: <p>I've created a client-server connection, something like a chat system. Previously I was using a <code>while</code> loop on the client side, and it was waiting to read a message from the console every time (of course server has a <code>while</code> loop as well to serve forever). But now, I'm trying to first create a connection at the beginning of the session, and then occasionally send a message during the session, so to maintain a permanent and persistent connection.</p>
  
  <p>Currently, without the <code>while</code> loop, the client closes the connection and I don't know how to find a workaround. </p>
  
  <p>Here is the client code:</p>
  
  <pre><code>import java.net.*;
  import java.io.*;
  public class ControlClientTest {
      private Socket socket = null;
  //  private BufferedReader console = null;  
      private DataOutputStream streamOut = null;
  
      public static void main(String args[]) throws InterruptedException {
          ControlClientTest client = null;
  
          String IP="127.0.0.1";
          client = new ControlClientTest(IP, 5555);
      }
  
      public ControlClientTest(String serverName, int serverPort) throws InterruptedException {
          System.out.println("Establishing connection. Please wait ...");
          try {
              socket = new Socket(serverName, serverPort);
              System.out.println("Connected: " + socket);
              start();
          } catch (UnknownHostException uhe) {
              System.out.println("Host unknown: " + uhe.getMessage());
          } catch (IOException ioe) {
              System.out.println("Unexpected exception: " + ioe.getMessage());
          }
  
          String line = "";
  //      while (!line.equals(".bye")) {
              try {
                  Thread.sleep(1000);
                  //TODO get data from input
  //              line = console.readLine();
                  line="1";
                  if(line.equals("1"))
                      line="1,123";
                  streamOut.writeUTF(line);
                  streamOut.flush();
              } catch (IOException ioe) {
                  System.out.println("Sending error: " + ioe.getMessage());
              }
  //      }
      }
  
      public void start() throws IOException {
  //      console = new BufferedReader(new InputStreamReader(System.in));
          streamOut = new DataOutputStream(socket.getOutputStream());
      }
  
  }
  </code></pre>
  
  <p>And here is the Server code:</p>
  
  <pre><code>import java.awt.*;
  import java.io.*;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class ControlServer {
      private Socket socket = null;
      private ServerSocket server = null;
      private DataInputStream streamIn = null;
  
      public static void main(String args[]) {
  
          ControlServer server = null;
          server = new ControlServer(5555);
      }
  
      public ControlServer(int port) {
          try {
              System.out
              .println("Binding to port " + port + ", please wait  ...");
              server = new ServerSocket(port);
              System.out.println("Server started: " + server);
              System.out.println("Waiting for a client ...");
              socket = server.accept();
              System.out.println("Client accepted: " + socket);
              open();
              boolean done = false;
              while (!done) {
                  try {
                      String line = streamIn.readUTF();
                      // TODO get the data and do something
                      System.out.println(line);
  
                      done = line.equals(".bye");
                  } catch (IOException ioe) {
                      done = true;
                  }
              }
              close();
          } catch (IOException ioe) {
              System.out.println(ioe);
          }
  
      }
  
  
      public void open() throws IOException {
          streamIn = new DataInputStream(new BufferedInputStream(
                  socket.getInputStream()));
      }
  public void close() throws IOException {
      if (socket != null)
          socket.close();
      if (streamIn != null)
          streamIn.close();
  }
  
  }
  </code></pre>