V
V
Vermut7562016-09-07 01:18:07
Computer networks
Vermut756, 2016-09-07 01:18:07

Why can a TCP connection fail for no apparent reason?

The server (TcpListener) is listening to the client (infinite loop, on a thread).
A single client (TcpClient) is listening to the server (infinite loop, on a thread).
Both loops hang on the Read() call, since neither the server writes anything to the client nor the client to the server.
Both applications are console. Nothing else is done.
The server is on a VPS, the clients were different, nothing changes from this.
The problem, in fact, is that after half an hour of downtime, the Read server suddenly crashes into an Exception, usually this happens if the client breaks the connection.
This happens if you kill the client process, but it is intact.
I thought that it might be a short-term loss of Internet connection. On the client, disconnected from the network, then connected. Nothing like that - everything works.
What other versions?
I didn’t look at Exception itself yet, it wasn’t possible, I stupidly made a try catch that “swallowed” it and only displayed a dry message in the log. Haven't tried it on localhost yet. I will do all this, but I hope that you will help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2016-09-07
@Rsa97

Most likely takes off on a timeout. Try enabling keepalive

socket.SetSocketOption(SocketOptionLevel.Socket, 
                       SocketOptionName.KeepAlive, true );

A
Alexey, 2016-09-07
@alsopub

It is not for nothing that in some programs such as messengers there was an option to "keep the connection" which periodically sent packets that were not meaningful within the protocol.
Upd https://habrahabr.ru/company/intersystems/blog/155565/

V
Vladimir Dubrovin, 2016-09-07
@z3apa3a

The problem may be that between the client and the NAT server and on the router, due to inactivity, the entry in the NAT table "goes out". But the solution that was suggested above (keep-alive) will also help in this case, only in addition to the keep-alive itself, you need to set the keep-alive timeout to at least 300 seconds, because the default value (usually 7200 seconds) will not save you. Those. in addition to SO_KEEPALIVE, TCP_KEEPIDLE/SIO_KEEPALIVE_VALS must be set depending on the system. Alternatively, you can consider sending keep-alive packets at the application protocol level. Typically, protocols designed for long connections with little traffic, such as instant messengers, use protocol keep-alives.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question