How to check if server closed connection?

  • Thread starter daniel.w.gelder
  • Start date
D

daniel.w.gelder

Hi, I have a server and an applet that connect. The server uses
SocketChannel and the applet uses plain Socket in a thread. When the
server closes the socket (or if the connection is just broken somehow),
I want the applet to recognize this and react.

I know you can block for a byte from the socket's InputStream, with
read(), and get a -1 to show the socket is closed. The problem is I
don't want the thread to block because I am also sending data to the
server occasionally, so blocking would interfere.

How do you resolve this? I noticed that Socket.getInputStream(new
byte[0]) does not do anything, so that's out.
 
Z

zero

(e-mail address removed) wrote in @o13g2000cwo.googlegroups.com:
Hi, I have a server and an applet that connect. The server uses
SocketChannel and the applet uses plain Socket in a thread. When the
server closes the socket (or if the connection is just broken somehow),
I want the applet to recognize this and react.

I know you can block for a byte from the socket's InputStream, with
read(), and get a -1 to show the socket is closed. The problem is I
don't want the thread to block because I am also sending data to the
server occasionally, so blocking would interfere.

How do you resolve this? I noticed that Socket.getInputStream(new
byte[0]) does not do anything, so that's out.

I'm no expert in server-client connections, but here's what I would do:

1) if the server closes the connection for any reason, send a closing
message to the client. This can be anything you want (a string, an object,
a number, ...), just pick something based on what you usually use the
connection for.

2) if the connection fails because of some outside circumstance, I don't
think there is a lot you can do. Basically the client has to wait (no need
to block) until it tries to send/receive data, and if that fails it knows
something is wrong with the connection, and you can react.

In (little) the client-server stuff I wrote, the connection was pretty much
constantly used, so when the connection failed the client knew almost
immediately. If you only use the connection sporadically you won't know
until the client needs the connection.

There might be a better way, but that's how I would do it.
 
O

Oliver Wong

zero said:
I'm no expert in server-client connections, but here's what I would do:

1) if the server closes the connection for any reason, send a closing
message to the client. This can be anything you want (a string, an
object,
a number, ...), just pick something based on what you usually use the
connection for.

2) if the connection fails because of some outside circumstance, I don't
think there is a lot you can do. Basically the client has to wait (no
need
to block) until it tries to send/receive data, and if that fails it knows
something is wrong with the connection, and you can react.

In (little) the client-server stuff I wrote, the connection was pretty
much
constantly used, so when the connection failed the client knew almost
immediately. If you only use the connection sporadically you won't know
until the client needs the connection.

There might be a better way, but that's how I would do it.

I agree with this advice. Generally, you cannot ever be SURE that a
connection has been unexpectedly closed. The best a client can do is to try
to read from the socket, and to timeout after a while if it doesn't receive
anything.

- Oliver
 
D

daniel.w.gelder

Okay, I suppose you're right. That is workable information, thank you.
The reason I thought there ought to be a way is because when I use a
web browser to hit the server, which causes the server to receive HTTP
data it doesn't understand and close the socket, then the web browser
knows when the server closed the socket! (Hope that made sense, english
is only my second language)

Thank you
Dan
 
O

Oliver Wong

Okay, I suppose you're right. That is workable information, thank you.
The reason I thought there ought to be a way is because when I use a
web browser to hit the server, which causes the server to receive HTTP
data it doesn't understand and close the socket, then the web browser
knows when the server closed the socket! (Hope that made sense, english
is only my second language)

In that special case, the server probably sent a message back to the
browser saying "I don't understand the data", and when the browser receives
that, it knows that the server always closes the connection after sending
that message.

This is an example of the advice number 1 that "zero" gave earlier in
this thread:

<quote>
if the server closes the connection for any reason, send a closing message
to the client. This can be anything you want (a string, an object, a
number, ...)
</quote>

- Oliver
 
D

daniel.w.gelder

Ah no, it's my server so I know what happens both with, and without,
the socket.close() command. But that's a good guess, I guess.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top