开发者

Closing ServerSocket - how to properly check if client socket is closed?

I create a new thread that runs the following code:

public static void startServer() throws IOException {
    serverSocket = new ServerSocket(55000);
    Socket clientSocket = serverSocket.accept(); 
}

The above code is run in a thread. Now, in my main class, I successfully create a socket connection to the server and I have checked it's integrity which is fine. here is the code:

Socket testServerSocket = new Socket("127.0.0.1", 55000);
assertEquals("/127.0.0.1", testServerSocket.getInetAddress().toString());
assertEquals(55000, testServerSocket.getPort());

This runs perfect. Then, again from my main, I kill the server connection that c开发者_开发问答loses the connection on the server side. However the following code keeps failing: assertEquals(false, testServerSocket.isBound());

It keeps returning true. Likewise, if I check the remote IP address for the connection, it doesn't return null, but rather '/127.0.0.1'. Any ideas why this might be happening? Many thanks for your help


I'm not an expert on sockets (I know what they are, but haven't ever used sockets on Java, only with C on Linux), but like JavaDoc for java.net.Socket states, 'A socket is an endpoint for communication between two machines'. So while closing server-side socket does destroy the connection between the two sockets (server- and client-side), your client-side socket is still bound, hence the isBound() is returning true. Maybe you meant to call isConnected() or isClosed()?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜