Recv and fwding in multi-threaded sockets programming

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
 
S

Steve Horsley

Johny said:
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

Have a look at java.nio, which allows asynchronous notifications of
available data. I haven't looked at it in detail myself yet, so I
can't say more.

The following applies if you don't use java.nio:

Any read will either block until data is available, or return after
a timeout. Neither will give good performance the way you are trying.

Instead of trying to pass the message to a blocked thread, why not
just write to the Writer? I don't really see why you want to hand
the data to another thread. Your listening thread can just recieve
the message and then write it to every other socket in turn.

The only problem with that is that a frozen peer will eventually
cause writes to block, and lock up your peer. So maybe you need
TWO threads per peer:
1: A listener that recieves messages and posts them to a queue for
every other peer. This spends most of its time blocked in a
socket read.
2: A sender that recieves messages from the queue and sends them
over the socket. This spends most of its time blocked waiting for
a message from the queue.
Now you can make the queue drop messages when it reaches a limit,
and there's no hangup in your app. If the sender blocks because
of a frozen peer, the queue drops messages without blocking
the other listener threads.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top