title: how to communicate multiple sockets in java?
body: <p>I have number of sockets connected to different systems with different ip-address with different port numbers. my question is I have to run one program in any system to communicate all the sockets at a time and how can i get the messages from sockets in parallel? </p>
  
  <p>While I am using single socket it will work fine and I am getting messages continuously from hyper terminal. more than one connected ,then I am getting response from first one only.. so please provide the solution asap</p>
  
  <hr>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.DataOutputStream;
  import java.io.InputStreamReader;
  import java.net.Socket;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  import java.util.HashMap;
  import com.ibatis.sqlmap.client.SqlMapClient;
  
  public class Client1 extends Thread {
      static Socket socket;
      static Socket socket1;
      static String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
      static SqlMapClient sqlMap=GetDBConnection.sqlMap;
      static BufferedReader br = null;
      static DataOutputStream dos=null;
      static BufferedReader br1 = null;
      static DataOutputStream dos1=null;
      public static void main(String args[]){
          try{
              String host="192.168.1.151";
              int port=5002;
              String host1="192.168.1.150";
              int port1=5001;
              socket = new Socket(host, port);
              socket1=new Socket(host1, port1);
              dos = new DataOutputStream(socket.getOutputStream());
              br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              dos1 = new DataOutputStream(socket1.getOutputStream());
              br1 = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
  
              Thread t = new Thread(new Runnable() {
              public void run() {
                  while(socket.isConnected()) {
                      try{
                          HashMap map = new HashMap();
                          String str = br.readLine();
                          Calendar cal = Calendar.getInstance();
                          SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
                          String time = sdf.format(cal.getTime());
                          map.put("data", str);
                          map.put("port", socket.getPort());
                          map.put("date_time", time);
                          sqlMap.insert("insert", map);
                      }
                      catch(Exception e){
                          e.printStackTrace();
                      }
                  }
                 }
              });
  
              Thread t1 = new Thread(new Runnable() {
              public void run() {
                  while(socket1.isConnected()) {
                      try{
                          HashMap map = new HashMap();
                          String str = br1.readLine();
                          Calendar cal = Calendar.getInstance();
                          SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
                          String time = sdf.format(cal.getTime());
                          map.put("data", str);
                          map.put("port", socket1.getPort());
                          map.put("date_time", time);
                          sqlMap.insert("insert", map);
                      }
                      catch(Exception e){
                          e.printStackTrace();
                      }
                    }
                  }
              });
  
              t.start();
              t1.start();
              }
              catch(Exception e){
                  e.printStackTrace();
              }
      }
  }
  </code></pre>
  
  <p>I am running two threads for two sockets its work fine, but if I have 100 sockets then how can I do it?  I can't create 100's of threads...so provide the best solution..</p>
  
  <hr>
  
  <pre><code>public class Client implements Runnable{
  
      public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
          static BufferedReader br = null;
          static DataOutputStream dos=null;
          static Socket socket = null;
          SqlMapClient sqlMap=GetDBConnection.sqlMap;
  
          Client(String host, int port)
          {
              try {
  
                  socket = new Socket(host, port);
              } catch (UnknownHostException e) {
                          e.printStackTrace();
              } catch (IOException e) {
                          e.printStackTrace();
              } 
          }
  
      public void run()
      {
          try {
  
              HashMap map = new HashMap(5); 
              String str;
              int prt=socket.getPort();
              dos = new DataOutputStream(socket.getOutputStream());
              br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
              while((str=br.readLine())!=null)
              {
                  Calendar cal = Calendar.getInstance();
                  SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
                  String time = sdf.format(cal.getTime());
                  map.put("data", str);
  
                  map.put("port",prt );
                  map.put("date_time", time);
                  sqlMap.insert("insert", map);
               }
          }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
              finally 
              {
                  try {
  
                      socket.close();
                      dos.close();
                      br.close();
                  } catch (IOException e) {
                          e.printStackTrace();
                  }
              }
      }
  
      public static void main(String args[]) 
      {
          ResultSet rs=null; 
          Connection con = null;
  
        try {
              Class.forName("org.postgresql.Driver");
              con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Socket","username","passwd");
              Statement st=con.createStatement(); 
              String vsql="select port, ip_addr from port_instrument";
              rs=st.executeQuery(vsql);
                 while (rs.next()) {
                   String id = rs.getString(1);
  
                   String ipaddr = rs.getString(2);
  
                   new Thread(new Client(ipaddr, Integer.parseInt(id))).start();
  
                 }
         }
         catch(Exception e)
         {
           e.printStackTrace();
         }
         finally{
             try {
                  con.close();
                  rs.close();
                  } catch (SQLException e) {
                      e.printStackTrace();
                  }
         }
      }
  }
  </code></pre>
  
  <p>By using this code we can communicate with the single socket only. In my database I have 5 sockets but finally I get communication from last socket(5) only.</p>