Is there a way to implement a socket server that services clients serially.
Generally the practice is to dispatch connected clients to a new thread that services requests and response however amounting to one thread per client on the server side.I don't want to do this because I later want to port this app to Java ME that may have a limit on the number of concurrent threads running at a point of time.
I was wondering how to solve开发者_如何转开发 this problem?
Sure, just don't fire off a background thread to handle the client.
EDIT
It seems like what you really want is for a large number of clients to be able to connect, but not create a load of threads. Since NIO doesn't appear to be supported how about using two threads:
- One thread loops accepting connections, passing the connected
Socket
to the second thread which just adds it to a list of connected sockets (this needs to be synchronized) - A second thread which loops through its internal list of live connections, and does "some work" with each in turn.
Calling socket.setSoTimeout()
with a reasonably small value should prevent the second thread waiting for too long on one connection.
Normally server handling looks something like this:
ServerSocket s;
Socket newSocket;
while (newSocket = s.accept()) {
new SocketHandlingThread(newSocket).start();
}
where the SocketHandlingThread() is a class you created to do whatever the server side of the socket conversation should be.
There are two basic ways to do what you're asking (which is to handle the sockets synchronously). The first is to simply join on the handler thread before moving back on to accept() again, like this
while (newSocket = s.accept()) {
SocketHandlingThread thread = new SocketHandlingThread(newSocket);
thread.start();
thread.join();
}
As pointed out in the comments below, you can avoid a join by just calling the thread's run method like so
thread.run();
in place of the start and join calls.
The other method is to take whatever code is in the SocketHandlingThread's run method and move it into the loop directly.
There is a great example of server side socket handling including pooling that can be found here.
However consider that you might not actually need pooling - I have had no problem serving 800 simultaneous clients from one server each with their own dedicated socket thread.
You could use non-blocking sockets. If you do this, then you don't need a thread for each client. Java has supported this for a while now via NIO. I'm not sure if this is supported by Java ME. Java ME is growing up these days such that it includes many of the features of JSE. It is probably a little unusual that you have server-side functionality in a Java ME environment that needs to service many client connections.
In your situation, is the traffic still not routed through a server? If so, there is no reason that the J2ME environment cannot receive messages from many other clients or peers (if you want to call them that) via a single socket connection to the server.
You can set a SO_TIMEOUT on your accepting socket. This will force the accept call to be non-blocking. That way you can wait for a little bit of time, then serve one of the previously accepted connections, then back to accepting new ones, and so on and so forth. The code would vaguely look like this:
do(
try{
socket.setSoTimeout(200);
Socket connected = socket.accept()
} catch (SocketTimeoutException e){//just ignore}
//handle your other threads/connections here
} while (!shutDown)
精彩评论