Re: Recv and fwding in multi-threaded sockets programming

From: Steve Horsley (shoot_at_the.moon)
Date: 03/01/04


Date: Mon, 01 Mar 2004 20:04:07 +0000

Johny Franslay wrote:
> 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



Relevant Pages

  • Threading problem when many sockets open
    ... I have written a socket based service in python and under fairly heavy ... enqueues the connection on a Queue. ... connections do not pile up very quickly. ...
    (comp.lang.python)
  • Re: Configuring Remoting Socket
    ... It seems like the Thread pool does default to 25 threads per process per ... Even if I tie up all 25 threads and queue ... The socket that is listening ... socket connections from the queue. ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Configuring Remoting Socket
    ... It seems like the Thread pool does default to 25 threads per process per ... Even if I tie up all 25 threads and queue ... The socket that is listening ... socket connections from the queue. ...
    (microsoft.public.dotnet.framework.remoting)
  • [PATCH 2.6.25.7] af_unix: fix poll for write/ connected DGRAM sockets
    ... receive queue of the 'peer socket' with the max_ack_backlog value ... This routine is used by both SOCK_DGRAM and SOCK_SEQPACKET ... The poll-implementation for these socket types is ... abovementioned receive queue is currently considered to be full. ...
    (Linux-Kernel)
  • Re: [PATCH 2.6.25.7] af_unix: fix poll for write/ connected DGRAM sockets
    ... receive queue of the 'peer socket' with the max_ack_backlog value ... This routine is used by both SOCK_DGRAM and SOCK_SEQPACKET ... The poll-implementation for these socket types is ... abovementioned receive queue is currently considered to be full. ...
    (Linux-Kernel)