• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

discover a client "has gone"

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Your mother was a hamster and your father was a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic