Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Client Server

 
Duncan Godfrey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic