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

April 3rd, 2012, 07:26 AM

Norm

Re: one java progarm handle two socket connection

Is the program to work as a server or as a client?
You can use more than one ServerSocket or Socket statement in your program.

April 3rd, 2012, 08:59 AM

chuikingman

Re: one java progarm handle two socket connection

Quote:

Originally Posted by Norm

Is the program to work as a server or as a client?
You can use more than one ServerSocket or Socket statement in your program.

I understand I can creat more than one socket connection .
but how can I read the data from socket connection .
If I have A,B connection , so, I read A , then B ,then A ,then B ..
Apart from this , any interrupt method , so, if there is data in connection A , it can call the read function auto ????
Please mention more detailed for your suggestion

April 3rd, 2012, 09:06 AM

Norm

Re: one java progarm handle two socket connection

When the Socket connects you can get I/O streams from the socket object and read from that. Look at the API doc for the methods to get the streams.

April 6th, 2012, 10:29 AM

chuikingman

Re: one java progarm handle two socket connection

Quote:

Originally Posted by Norm

When the Socket connects you can get I/O streams from the socket object and read from that. Look at the API doc for the methods to get the streams.

Any example/sample code for eventhandler to monitor connection or string ????
Any example ???

April 6th, 2012, 10:31 AM

Norm

Re: one java progarm handle two socket connection

Do a search for code that uses Sockets.

When a method that is receiving input over a socket gets data, it could call another method and pass it the input it received.