title: how to connect two computers over the same network using java sockets?
body: <p>I am not able to connect two different machines over the same network using the following client server programs.
  the code however runs fine on the same machine.
  I think in the client program it goes in an infinite loop just before <code>socket.accept()</code>;
  please suggest a possible solution.</p>
  
  <p>server.java</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  import java.lang.*;
  
  class server{
      public static void main(String args[]){
          try{
              int one,zero;
              one=zero=0;
              ServerSocket sock=new ServerSocket(2000);
              Socket soc=sock.accept();
              DataInputStream dis=new DataInputStream(soc.getInputStream());
              System.out.println("Connection Established");
              String msg =dis.readLine();
              System.out.println("MESSAGE : "+msg);
              for(int i=0;i&lt;msg.length();i++){
                  if(msg.charAt(i)=='0')
                      zero++;
                  else
                      one++;
              }
              System.out.println("Ones are "+one);
              System.out.println("Zeros are "+zero);
  
              soc.close();
          }
          catch(IOException e){
              System.out.println(e);
  
          }
      }
  }
  </code></pre>
  
  <p>client.java</p>
  
  <pre><code>    import java.io.*;
      import java.net.*;
      class client{
          public static void main(String args[]){
              try{
                  Socket soc=new Socket("localhost",2000);//or ipv4 address for different computers
                  BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
                  PrintStream pr=new PrintStream(soc.getOutputStream());
  
                  System.out.println("Enter message..");
                  String msg =is.readLine();
                  pr.println(msg);
                  System.out.println("YOU ENTERED.."+msg);
                  soc.close();
              }
              catch(IOException e){
                  System.out.println(e);
  
              }
          }
      }
  </code></pre>