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 ...
one java program handle two socket connection
Page 1 of 19 Replies - 1422 Views - Last Post: 06 April 2012 - 07:43 PM
Replies To: one java program handle two socket connection
#2
Re: one java program handle two socket connection
Posted 03 April 2012 - 01:13 AM
Pseudocode
Something along those lines should do.
while(...)
{
data = conn1.read;
// handle here?
data2 = conn2.read;
// handle here?
}
Something along those lines should do.
#3
Re: one java program handle two socket connection
Posted 03 April 2012 - 02:22 AM
Ghlavac, on 03 April 2012 - 01:13 AM, said:
Pseudocode
Something along those lines should do.
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 ????
#4
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
but Thread are the way to go
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
#5
Re: one java program handle two socket connection
Posted 06 April 2012 - 07:26 AM
pbl, 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
but Thread are the way to go
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
#6
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();
}
}
}
}
#7
Re: one java program handle two socket connection
Posted 06 April 2012 - 12:42 PM
#8
Re: one java program handle two socket connection
Posted 06 April 2012 - 04:40 PM
The_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 ...
#9
Re: one java program handle two socket connection
Posted 06 April 2012 - 07:31 PM
chuikingman, 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 ...
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
#10
Re: one java program handle two socket connection
Posted 06 April 2012 - 07:43 PM
chuikingman, on 06 April 2012 - 04:40 PM, said:
The_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();
}
}
}
}
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|