Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3626690
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:47:39+00:00 2026-05-18T23:47:39+00:00

I have number of sockets connected to different systems with different ip-address with different

  • 0

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?

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


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();
            }
    }
}

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..


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();
                }
       }
    }
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:47:40+00:00Added an answer on May 18, 2026 at 11:47 pm

    You might want to write a communication class that implements Runnable and does all the socket comms. You could then create an instance of this class for each socket and run the classes in some sort of thread pool.

    Edit: Here’s a start for you

    public class SocketHandler implements Runnable
    {
      private Socket socket;
    
      public SocketHandler(String host, int port)
      {
        socket = new Socket(host, port);
      }
    
      public void run()
      {
        //Do the comms to the remote server
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stuck with the following scenario and appreciate any help/advice.. Requirement I have number
I have a number of classes (more than 40), each one has a number
Ok, I have connected to an IP address using the following code: IPAddress myIpAddress
I want to connect to a number of different sockets/webservices on the command line
I have a program that listens on a port waiting for a small amount
We have a smpp server for which we have ip address username password port
I have created an asynchronous server that can read in messages from any number
I have an IP = '127.0.0.1', port number 'port', and trying class AClass(asyncore.dispatcher): def
I have number of line breaks in a string. But I only want it
I have a number input that should trigger jQuery on every change. To do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.