PDA

View Full Version : Check if the socket is closed



Faab234
October 23rd, 2010, 11:10
I want to check if the socket is closed; Currently I'm using this:


/**
* The thread's process.
* <p>The serverChannel listens for incoming connections, and accepts them.
* The timeout is set to 1 to prevent i/o blocking.
*/
public void run() {
while (serverRunning) {
try {
socket = serverChannel.accept();
socket.setSoTimeout(soTimeout);
socket.setTcpNoDelay(tcpNoDelay);
SessionData socketData = new SessionData(socket);
channelEvent.sessionCreated(socketData);
int id = getFreeId();
sockets[id] = socket;
SessionHandler socketHandler = new SessionHandler(id);
if (sockets[id].isConnected() == true) {
channelEvent.sessionOpened(socketHandler, socketData);
}

PrintWriter out = new PrintWriter(sockets[id].getOutputStream());
out.println("lol");
out.flush();
if(out.checkError()) {
System.out.println("Session Closed");
}




} catch (Exception e) {
e.fillInStackTrace();
}
}
}


Unfortunately, the code doesn't work, because it doesn't check if the socket is closed. Why?

Trey
October 23rd, 2010, 17:13
Use the isClosed method in the Socket class. Keep in mind also that if you try to acquire an InputStream from a closed socket it will throw an IOException, so that could be used as an indicator as well.

Faab234
October 24th, 2010, 10:28
Use the isClosed method in the Socket class. Keep in mind also that if you try to acquire an InputStream from a closed socket it will throw an IOException, so that could be used as an indicator as well.

I thought the isClosed method will be not true if the socket is closed client-side. Right?

Trey
October 24th, 2010, 16:42
I thought the isClosed method will be not true if the socket is closed client-side. Right?

Well, if you closed it yourself then why would you need to check if it's closed? lol.

Faab234
October 24th, 2010, 17:57
Well, if you closed it yourself then why would you need to check if it's closed? lol.

So I can delete them for the array.

Trey
October 24th, 2010, 20:42
So I can delete them for the array.

Remove them after you close them then. I don't understand what you're saying. If you're closing the socket from the server you can just remove them from your array after you close it. If it's closed from the client, just check every so often if the sockets are still open and if not remove them from the array the same way.

Faab234
October 25th, 2010, 06:37
Remove them after you close them then. I don't understand what you're saying. If you're closing the socket from the server you can just remove them from your array after you close it. If it's closed from the client, just check every so often if the sockets are still open and if not remove them from the array the same way.

If the socket is closed (Server and Client Sided), I want to call a method.

Trey
October 25th, 2010, 20:27
If the socket is closed (Server and Client Sided), I want to call a method.

There's no event trigged that the socket has been closed necessarily, you'll just have to check whenever you attempt to use that socket for anything.

Faab234
October 25th, 2010, 20:29
There's no event trigged that the socket has been closed necessarily, you'll just have to check whenever you attempt to use that socket for anything.

Already do that:


PrintWriter out = new PrintWriter(sockets[id].getOutputStream());
out.println("lol");
out.flush();
if(out.checkError()) {
System.out.println("Session Closed");
}


But it don't work.

Trey
October 25th, 2010, 22:05
Already do that:


PrintWriter out = new PrintWriter(sockets[id].getOutputStream());
out.println("lol");
out.flush();
if(out.checkError()) {
System.out.println("Session Closed");
}


But it don't work.

I wouldn't use a PrintWriter in the first place, considering it writes everything as text rather than as raw bytes, which is more ideal in networking applications. OutputStream.write throws an exception if the stream is closed, just do it that way as intended. InputStream.read will return -1 if the stream is closed as well.

Plus, by the looks of your current set up, it seems to me that NIO would be much more appropriate. SocketChannel's read method also returns -1 if the stream is closed, so you could use that if you go with NIO.

Faab234
October 26th, 2010, 14:11
I wouldn't use a PrintWriter in the first place, considering it writes everything as text rather than as raw bytes, which is more ideal in networking applications. OutputStream.write throws an exception if the stream is closed, just do it that way as intended. InputStream.read will return -1 if the stream is closed as well.

Plus, by the looks of your current set up, it seems to me that NIO would be much more appropriate. SocketChannel's read method also returns -1 if the stream is closed, so you could use that if you go with NIO.

Thanks, I'll also look at NIO.

edit; Not working.

Trey
October 27th, 2010, 02:20
Thanks, I'll also look at NIO.

edit; Not working.

If the socket isn't open it will throw an exception, that's the easiest way to handle it. No reason to make it more complex than it is =P

Faab234
October 27th, 2010, 06:43
If the socket isn't open it will throw an exception, that's the easiest way to handle it. No reason to make it more complex than it is =P

I know, but my code will be not executed.