9 Replies - 3614 Views - Last Post: 06 April 2012 - 07:43 PM Rate Topic: -----

#1 chuikingman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-March 10

one java program handle two socket connection

Posted 02 April 2012 - 11:56 PM

Hi,
I need to write a java program handle two sockect connection A ,B .
So, in normal case ,one connection , the program will read data from connection and process it .
Then read data from connection again and process it .
Work like a while loop .

But if I need to handle two socket connection ,how can I write in one program .
Apart from read data from A ,process it , then read data from B , process it .
work like A ,B ,A,B sequential .

Any other method I can write in this program to handle two connection.
As, it is not promised that there are data in the connection , sometime there are no data in the message queue of the two connection .


Please advice ...

Is This A Good Question/Topic? 0
  • +

Replies To: one java program handle two socket connection

#2 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 519
  • Joined: 14-January 09

Re: one java program handle two socket connection

Posted 03 April 2012 - 01:13 AM

Pseudocode

while(...)
{

data = conn1.read;
// handle here?

data2 = conn2.read;
// handle here?

}




Something along those lines should do.
Was This Post Helpful? 0
  • +
  • -

#3 chuikingman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-March 10

Re: one java program handle two socket connection

Posted 03 April 2012 - 02:22 AM

View PostGhlavac, on 03 April 2012 - 01:13 AM, said:

Pseudocode

while(...)
{

data = conn1.read;
// handle here?

data2 = conn2.read;
// handle here?

}




Something along those lines should do.



So, you suggest to handle A,B,A,B in this sequence .
But as there are not always data in A and B connection . So, is there any thinkg like interrrupt . So, if there is data present/exist in the connection , it will trigger the conn.read function ????
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: one java program handle two socket connection

Posted 03 April 2012 - 07:07 PM

Better to use a Thread fo each connection
To keep you design you will have to be ensured that connections A and B will receive a message in that order
As far as if you have only one connection

while(...)
{

   if(conn1 != null) {
      data = conn1.read;
      // handle here?
   }
   if(conn2 != null) {
      data2 = conn1.read;
      // handle here?
   }
}


but Thread are the way to go
Was This Post Helpful? 0
  • +
  • -

#5 chuikingman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-March 10

Re: one java program handle two socket connection

Posted 06 April 2012 - 07:26 AM

View Postpbl, on 03 April 2012 - 07:07 PM, said:

Better to use a Thread fo each connection
To keep you design you will have to be ensured that connections A and B will receive a message in that order
As far as if you have only one connection

while(...)
{

   if(conn1 != null) {
      data = conn1.read;
      // handle here?
   }
   if(conn2 != null) {
      data2 = conn1.read;
      // handle here?
   }
}


but Thread are the way to go

Any sample for eventhandler handle montior connection or string ??
any detailed example as I think eventhandler can help me to solve it
Was This Post Helpful? 0
  • +
  • -

#6 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: one java program handle two socket connection

Posted 06 April 2012 - 11:01 AM

Something like this:

public class Blah {

    public static void main(String[] args) {
    
        new Thread(new Connection("127.0.0.1", 7856)).start();
        new Thread(new Connection("127.0.0.1", 7856)).start();
    
    }
    
    private class Connection implements Runnable {
    
        Socket soc;
        BufferedReader r;
        BufferedWriter w;
        
        public Connection(String serv, int port) {
            try {
                soc = new Socket(serv, port);
                r = new BufferedReader(new OutputStreamReader(soc.getOutputStream()));
                w = new BufferedWriter(new InputStreamWriter(soc.getInputStream()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            String line = new String();
            while ((line = r.readLine()) != null) {
            
                //do whatever with the line
            
            }
            
            try {
                r.close();
                w.close();
                soc.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
    }

}


Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: one java program handle two socket connection

Posted 06 April 2012 - 12:42 PM

View Postchuikingman, on 06 April 2012 - 10:26 AM, said:

Any sample for eventhandler handle montior connection or string ??

What do you mean ? An EventHandler is for dynamically generate events.
Was This Post Helpful? 0
  • +
  • -

#8 chuikingman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-March 10

Re: one java program handle two socket connection

Posted 06 April 2012 - 04:40 PM

View PostThe_Programmer-, on 06 April 2012 - 11:01 AM, said:

Something like this:

public class Blah {

    public static void main(String[] args) {
    
        new Thread(new Connection("127.0.0.1", 7856)).start();
        new Thread(new Connection("127.0.0.1", 7856)).start();
    
    }
    
    private class Connection implements Runnable {
    
        Socket soc;
        BufferedReader r;
        BufferedWriter w;
        
        public Connection(String serv, int port) {
            try {
                soc = new Socket(serv, port);
                r = new BufferedReader(new OutputStreamReader(soc.getOutputStream()));
                w = new BufferedWriter(new InputStreamWriter(soc.getInputStream()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            String line = new String();
            while ((line = r.readLine()) != null) {
            
                //do whatever with the line
            
            }
            
            try {
                r.close();
                w.close();
                soc.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
    }

}


No.. your suggest to not solve my problem .
I do not expect the scoket connnection handle like the sequent A,B,A, B .
I expect read data from both socekt connect and it can handle by event handle as the data not always present in the message queue .that iswhat I want eventhandler ...
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: one java program handle two socket connection

Posted 06 April 2012 - 07:31 PM

View Postchuikingman, on 06 April 2012 - 07:40 PM, said:

No.. your suggest to not solve my problem .
I do not expect the scoket connnection handle like the sequent A,B,A, B .
I expect read data from both socekt connect and it can handle by event handle as the data not always present in the message queue .that iswhat I want eventhandler ...

That is why you need two threads
or, horrible design, you read the two sockets in a loop with a timer
Was This Post Helpful? 0
  • +
  • -

#10 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: one java program handle two socket connection

Posted 06 April 2012 - 07:43 PM

View Postchuikingman, on 06 April 2012 - 04:40 PM, said:

View PostThe_Programmer-, on 06 April 2012 - 11:01 AM, said:

Something like this:

public class Blah {

    public static void main(String[] args) {
    
        new Thread(new Connection("127.0.0.1", 7856)).start();
        new Thread(new Connection("127.0.0.1", 7856)).start();
    
    }
    
    private class Connection implements Runnable {
    
        Socket soc;
        BufferedReader r;
        BufferedWriter w;
        
        public Connection(String serv, int port) {
            try {
                soc = new Socket(serv, port);
                r = new BufferedReader(new OutputStreamReader(soc.getOutputStream()));
                w = new BufferedWriter(new InputStreamWriter(soc.getInputStream()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            String line = new String();
            while ((line = r.readLine()) != null) {
            
                //do whatever with the line
            
            }
            
            try {
                r.close();
                w.close();
                soc.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
    }

}


No.. your suggest to not solve my problem .
I do not expect the scoket connnection handle like the sequent A,B,A, B .
I expect read data from both socekt connect and it can handle by event handle as the data not always present in the message queue .that iswhat I want eventhandler ...


That is not going in sequential order. Those are two different threads. They will be ran "at the same time." And I don't know what you mean by a eventhandler.

Here. I'll comment on this so you can understand it more:
public class Blah {

    public static void main(String[] args) {
        
        //This creates two new threads.
        //The outputstream of the threads will be read at the same time so no need
        //for sequential stuff.
        new Thread(new Connection("127.0.0.1", 7856)).start();
        new Thread(new Connection("127.0.0.1", 7856)).start();
    
    }
    
    private class Connection implements Runnable {
    
        Socket soc;
        BufferedReader r;
        BufferedWriter w;
        
        public Connection(String serv, int port) {
            try {
                //Creates a socket connection and a buffered reader and writer.
                soc = new Socket(serv, port);
                r = new BufferedReader(new OutputStreamReader(soc.getOutputStream()));
                w = new BufferedWriter(new InputStreamWriter(soc.getInputStream()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            String line = new String();
            //This while loop will keep reading the output until the socket connection is cosed.
            while ((line = r.readLine()) != null) {
            
                //do whatever with the line
                //Use the line variable for reading strings.
            
            }
            
            try {
                r.close();
                w.close();
                soc.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
    }

}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1