• Post Reply Bookmark Topic Watch Topic
  • New Topic

discover a client "has gone"

 
Jessid Leon Velez Gutierrez
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I am writing a simple app using a server and several clients, connected through a socket. I want to handle several clients and in the server class I have a Vector which stores each new socket, using something like this:

...
conexiones.add(socketServidor.accept());
...

being conexiones the Vector. It is very easy, this way to know when a client connects to the server...but how is it possible to know if a client (who might be running the Client class from a command line) has, for example got bored and simply terminated the program. I don't know if I am clear or very confusing.

I thought that this bored client was going to become a null into the Vector, but it remains always unchanged, as if it were in memory, or i dont know where...

If some of you have any idea, or a better way to handle several clients...it would be very welcome any suggestion

Thanks a lot
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way I've found out about a client like that is when the server attempts to write a response it gets an exception with something about connection terminated by the partner. I can make this happen in my web server by banging on the refresh button faster than the server can return pages. Every time the browser sends a new request it abandons the old one. When the server gets around to sending the response, it gets the error.
 
Jessid Leon Velez Gutierrez
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan James first, I want to thank you for your answer. I dont know if i am not doing good what you suggested, because I do not get any error message. It writes to the client and remains very quiet, jejeje. I am thinking that the Vector is causing the problem, as if elements were kept in memory and never modified...I dont know...

or is there any other suggestion to handle these multiple clients? not a vector but another thing.

Good bye
 
Sushil Sharma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your code snippets, the add to vector will block until acceptsucceeds, or a timeout execption is thrown. I assume you are running server on a separate thread.

Now as to monitor client connect/disconnect state, the java.net Socket Api is not clear if an exception is thrown when client disconnects. The only way I can see is to have some sort of a thread which poll socket in the Vector. The thread can check Socket's .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!