aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Client Server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Client Server" Watch "Client Server" New topic
Author

Client Server

Duncan Godfrey
Greenhorn

Joined: Apr 17, 2004
Posts: 3
Hello,

I was wondering if anyone was able to help me. I�m trying to write a server which will only allow 3 clients to have active connections; this is the code I am currently using:

try {
//create a server socket on specified port
welcomeSocket = new ServerSocket(port);
//try {
while (true) {
//listen for connection attempts
Socket socket = welcomeSocket.accept();

//The sonar server can only have 3 active connections
//Once we have 3 connections then any further attempts
//are rejected.
if(connected < 3) {
//each connection recives there own thread
new SonarHandler(socket).start();
addConnection();
}
else {
socket.close();
System.out.println("Connection rejected");
}
}
} catch (IOException e) {
e.printStackTrace();
}

The problem is that it seems impossible to detect that the socket has been closed by the server and the clients assume they have a working connection. Is there a better way to write a server that limits connections?

Thanks in advance,

Duncan
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It's typical to start a handler thread for each new request. Let's say instead of creating new handlers we use a set of three over and over. We put them in a BlockingQueue. The main loop looks like:

When you get a handler from the queue, if one of the three is available you get it immediately. If all three are busy, you block until one of them finishes and goes back into the queue.

If you get a burst of requests that you can't handle immediately, they'll block while waiting for a handler. The clients will experience a slow-down and eventually time out. That's probably the correct behavior, but that depends on your application.

One part of this idea I don't like much is that the handlers have to put themselves back on the queue. That seems a little funky.

BlockingQueue is new in JDK 5. If you're in an older JDK there is a good blocking queue in the Apache Commons Thread Pool, and a much more robust and complicated one in Doug Lea's concurrency packages.

Lemme know if that sounds like an interesting direction. We can play with more detail if you need.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Duncan Godfrey:
The problem is that it seems impossible to detect that the socket has been closed by the server and the clients assume they have a working connection. Is there a better way to write a server that limits connections?
You cuold add a response from the server to represent that the connection is refused and then have the clients check for that response before continuing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Client Server