J
Johny Franslay
Hi, I am new to socket programming in Java and I have a newbie question.
I'm writing a P2P network where each node maintains 5 separate
connections to 5 other nodes and each connection is maintained on a
separate thread. When one of the connection receives a query message, it
would forward it to the other 4 connections.
So in short i would like each thread to do both of these without
invoking any new thread:
1. listen for incoming message from peer and forward it to other thread.
2. listen for forwarded message from another thread and send it to the peer.
My dilemma is in (1.) I use BufferedReader.readLine() function to wait
for incoming msg from peer. But does readLine() blocks until an input is
received? If so, that means it will keep waiting for input even though
there's a msg ready to be forwarded from the other thread.
Does anybody know of a good idea on how to solve this problem?
Is there a more suitable way of waiting for socket input other than
readLine() in this context?
What's the best way of notifying and passing a message to other threads?
Any help would be appreciated.
Thanks in advance.
-Johny
I'm writing a P2P network where each node maintains 5 separate
connections to 5 other nodes and each connection is maintained on a
separate thread. When one of the connection receives a query message, it
would forward it to the other 4 connections.
So in short i would like each thread to do both of these without
invoking any new thread:
1. listen for incoming message from peer and forward it to other thread.
2. listen for forwarded message from another thread and send it to the peer.
My dilemma is in (1.) I use BufferedReader.readLine() function to wait
for incoming msg from peer. But does readLine() blocks until an input is
received? If so, that means it will keep waiting for input even though
there's a msg ready to be forwarded from the other thread.
Does anybody know of a good idea on how to solve this problem?
Is there a more suitable way of waiting for socket input other than
readLine() in this context?
What's the best way of notifying and passing a message to other threads?
Any help would be appreciated.
Thanks in advance.
-Johny