Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a socket be both connected and closed?

Tags:

java

sockets

I'm using a Java socket client. In a case where the server is still connected to my client but it does not send a response to my message - I eventually get a read time out exception.

In that case I want to test to see if I should reconnect my socket or just keep it an re-use it.

I use this condition:

if (!socket.isConnected() || socket.isClosed() || !socket.isBound()) {     try {         socket.close();     } catch (IOException e1) {     }     // Wait on a new connection     socket = connectSocket(.....); } 

But I always seem to reconnect. When I log the values of the Boolean properties I see this:

connected: true closed: true bound: true

How can it be connected and closed?

TIA

like image 794
sproketboy Avatar asked Sep 13 '10 13:09

sproketboy


People also ask

How do you close a socket connection?

close() call shuts down the socket associated with the socket descriptor socket, and frees resources allocated to the socket. If socket refers to an open TCP connection, the connection is closed. If a stream socket is closed when there is input data queued, the TCP connection is reset rather than being cleanly closed.

Why is my socket closed?

A "socket closed" error means that the connection has been terminated, however, no specific reason was provided/returned. The "socket closed" error indicates that the connection was lost outside of the control or awareness of the Driver. There can be a number of reasons for that, for example: network failure.

What is an unbound socket?

An unbound ServerSocket cannot be used to accept connections before it is bound. The no-argument constructor that doesn't yet bind the ServerSocket exists so that you can call other methods on the ServerSocket before binding it using the bind method.

How do you know if a socket is closed?

The only way to tell if the socket has been disconnected is to send data over it. set the timeout on the server side socket and then have the client check in every so often within the timeout so if you set the client to check in every 10 seconds then set the timeout to say 30 seconds on the server socket.


1 Answers

This thread has some useful discussions on this topic. It turns out that Socket.isConnected returns true if it has (ever) been successfully connected.

From the above thread:

When you use Socket(), which you seem to have overlooked, Socket.isConnected() tells you whether Socket.connect() has been called or not. Similarly for isClosed() and close().

Confusion over these methods results from confusing the state of the socket, which is under the control of the application, with the state of the overall connection, which is under the control of the protocol. isConnected() and isClosed() tell what you have done to the socket. There are no APIs other than read and write for determining the state of the connection.

The docs says:

Returns true if the socket successfuly connected to a server

and not as one perhaps would expect "returns true if the socket is connected to a server".

The behavior can be confirmed by looking at the source of Socket:

public boolean isConnected() {     // Before 1.3 Sockets were always connected during creation     return connected || oldImpl; } 

You could also run this little test snippet:

Socket s = new Socket();  System.out.println("isConnected: " + s.isConnected() +                   " isBound: "     + s.isBound() +                   " isClosed: "    + s.isClosed());  s.connect(new InetSocketAddress("google.com", 80));  System.out.println("isConnected: " + s.isConnected() +                    " isBound: "    + s.isBound() +                    " isClosed: "   + s.isClosed());  s.close();  System.out.println("isConnected: " + s.isConnected() +                    " isBound: "    + s.isBound() +                    " isClosed: "   + s.isClosed()); 

Which prints:

isConnected: false isBound: false isClosed: false isConnected: true isBound: true isClosed: false isConnected: true isBound: true isClosed: true 

I must say that the documentation is quite unclear on this point, and that the method-name is a bit misleading.

like image 121
aioobe Avatar answered Sep 21 '22 00:09

aioobe