Socket closed after a time with no activity

Hello everyone:

First of all I want to apologize because of my English.

I'm developing a chat application with a C++ server and a Java client. My problem is that when the client stays a while without receiving any data from the server (30 minutes or so) the socket seems to get closed and the client is unable to get any new data anymore.

My reading thread gets blocked at the read() operation, waiting for new messages and when new data finally arrives it throws a "socket closed" exception.

So my question is: who does the closing? is it normal? I mean, is it for example something that the operating system does (my client is running on Windows XP)? I'm new at socket programming and although I have looked for information about this issue I have found nothing.

To solve the problem if thought about using ping-pong messages but I wonder if there is a better way.

Can anyone help me? Thanks in advance

[949 byte] By [abc._a] at [2007-11-27 2:23:20]
# 1
'Socket closed' means you have closed it yourself somewhere else in your app. If it was just a close by the other end you would get some kind of EOS indicator such as an EOFException, a null from readLine(), or a -1 from read().
ejpa at 2007-7-12 2:28:39 > top of Java-index,Core,Core APIs...
# 2
sounds like the server, after sending its data, is closing the socket before you finish reading
developer_jbsa at 2007-7-12 2:28:39 > top of Java-index,Core,Core APIs...
# 3
No it doesn't, see reply #1.
ejpa at 2007-7-12 2:28:39 > top of Java-index,Core,Core APIs...
# 4

Thanks for your answers.

I've been tracing the execution and I've found out that I don't get that exception (sorry, when I said I got it, it was due to a different matter). In fact I don't get any exception at all, but it doesn't work out.

I've been checking my code and don't close the socket myself at any point and the other end doesn't close it, either. I have no clue about what the reason of the problem is: it just stop answering.

I'm working on sending PING messages to the client so it isn't inactive so long. Perhaps this will solve the problem, but I still wonder what the cause is.

Can you give any advice?

abc._a at 2007-7-12 2:28:39 > top of Java-index,Core,Core APIs...
# 5

If there's a firewall in the way they are liable to drop what they consider to be idle connections. If you have a thread blocked in a read it may not have any way to discover the drop. IMHO when reading from a network you should *always* use a read timeout to defend against all this kind of thing.

ejpa at 2007-7-12 2:28:39 > top of Java-index,Core,Core APIs...
# 6
Thanks for your answer.I've solved the problem by implementing timeouts and a ping-pong protocol to check accesibility to the other end. Now, no one but closes my sockets ;-)
abc._a at 2007-7-12 2:28:39 > top of Java-index,Core,Core APIs...