title: Simple Server/Client Socket connection?
body: <p>I am writing a java socket where a server runs and a client connects to the server and the server can read from the clients command line.</p>
  
  <p>First I wanted to connect to the server, here`s the code:</p>
  
  <pre><code>public class Server{
      public static void main(String[] args) throws IOException {
  
          ServerSocket serverSocket = null;
  
          try {
              serverSocket = new ServerSocket(10000);
          } catch (IOException e) {
              System.err.println("Could not listen on port: 4444.");
              e.printStackTrace();
              System.exit(1);
          }
  
          Socket clientSocket = null;
          try {
              clientSocket = serverSocket.accept();
          } catch (IOException e) {
              System.err.println("Accept failed.");
              System.exit(1);
          }
  
          PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
          BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
          String inputLine, outputLine;
  
          out.close();
          in.close();
          clientSocket.close();
          serverSocket.close();
      }
  
  }
  </code></pre>
  
  <p>and here is my client:</p>
  
  <pre><code>public class Client {
      public static void main(String[] args) throws IOException {
  
          Socket socket = null;
          PrintWriter out = null;
          BufferedReader in = null;
  
          try {
              socket = new Socket("taranis", 4444);
              out = new PrintWriter(socket.getOutputStream(), true);
              in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          } catch (UnknownHostException e) {
              System.err.println("Don't know about host: taranis.");
              System.exit(1);
          } catch (IOException e) {
              System.err.println("Couldn't get I/O for the connection to: taranis.");
              System.exit(1);
          }
  
          BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
          String fromServer;
          String fromUser;
  
          while ((fromServer = in.readLine()) != null) {
              System.out.println("Server: " + fromServer);
              if (fromServer.equals("Bye."))
                  break;
  
              fromUser = stdIn.readLine();
              if (fromUser != null) {
                  System.out.println("Client: " + fromUser);
                  out.println(fromUser);
              }
          }
  
          out.close();
          in.close();
          stdIn.close();
          socket.close();
      }
  }
  </code></pre>
  
  <p>When I start the server I get "Could not listen on port: 4444."</p>
  
  <p>Why?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Right now I cannot restart the server again, because I get</p>
  
  <pre><code>java.net.BindException: Address already in use: JVM_Bind
      at java.net.DualStackPlainSocketImpl.bind0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
      at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
      at java.net.PlainSocketImpl.bind(Unknown Source)
      at java.net.ServerSocket.bind(Unknown Source)
      at java.net.ServerSocket.&lt;init&gt;(Unknown Source)
      at java.net.ServerSocket.&lt;init&gt;(Unknown Source)
  </code></pre>
  
  <p>What can I do against that?(I am using windows7)</p>