views:

44

answers:

3

Hello,

i m writing simple application involving a server and many clients at time. i have to use DataGramSocket. application is simple exchange of messages through console like in chatting. but both operations in.readLine() and ds.receive() are blocking and putting them in seperate threads didn't help as blocking i/o will block the thread too. can any body tell me how can i do that without nio

A: 

First, you should use java.nio to get non-blocking I/O. Assuming you can't, for some reason...

You can easily have your server handle data from multiple clients using DatagramSocket and a work-queue, such as java.util.concurrent.ThreadPoolExecutor.

The idea is that you have a single receiver thread reading the socket, and each datagram is received, wrapped in a "ReceivedDatagram" object and dropped onto the work-queue. The work-queue has a thread pool whose threads dequeue and process each packet; if the packet requires a response, the thread sends the response (blocking) before blocking to dequeue another ReceivedDatagram.

To send data asynchronously, you simply put a "SendDatagram" object on the work-queue which represents a DatagramPacket to send.

Note, you will be using Datagram.receive(DatagramPacket) and Datagram.send(DatagramPacket).

Software Monkey
Note, that if you keep your packets relatively small, in terms of the time required to handle any given packet, this will easily scale to 10s of thousands of clients with a few hundred threads, since network bandwidth will be the constraining factor.
Software Monkey
A: 

You can do that by doing blocking operations in separate threads, using either manually spawned thread or better Thread Pool (http://www.ibm.com/developerworks/library/j-jtp0730.html)

Andrey
+1  A: 

If you have threads which are dedicated only to sending or receiving data over network, then thread blocking won't be a problem, as only that dedicated thread will be blocked.

Then, let's consider impact of this solution on number of threads in your application:

  • If you have only few clients per server, then having 2 I/O threads per client isn't a problem.
  • If you have lots of clients per server, then you should accept the fact that some of their requests will be processed not immediately but only then worker thread becomes available. You can try to spawn as much I/O threads as there are clients, but there are limitations on the number of threads single JVM instance can have. Exact numbers depend on the size of heap available to your JVM and whether your architecture is 32bit or 64bit, see here.

If you interested in general task of handling lots of clients, here is the classic web paper on this question.

Victor Sorokin