Tcp socket suddenly closing connection - c#

I have a chat site (http://www.pitput.com) that connects user via socket connections.
I have in the client side a flash object that opens a connection to a port in my server.
In the server i have a service that is listening to that port in an async matter.
All is working fine except when i talk to someone after an unknown period of time(about couple of minutes) the server is closing my connection and i get an error in the server :
" A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond".
I dont know how exactly the tcp socket works. does it checking for "live" connection every couple of seconds? how does it decide when to close the connection? Im pretty sure that the close operation is not coming from the client side.
Thanks.

Sounds like the server is handling the connection but not responding. This is the point where I usually pull out WireShark to find out what's going on.

TCP/IP does have an option for checking for live connections; it's called "keepalive." Keepalives are hardly ever used. They're not enabled by default. They can be enabled on a system-wide basis by tweaking the Registry, but IIRC the lowest timeout is 1 hour. They can also be enabled on a single socket (with a timeout in minutes), but you would know if your application does that.
If you are using a web service and your client is connecting to an HTTP/HTTPS port, then it may be getting closed by the HTTP server (which usually close their connections after a couple minutes of idle time). It is also possible that an intermediate router may be closing it on your behalf after an amount of idle time (this is not default behavior, but corporate routers are sometimes configured with such "helpful" settings).
If you are using a Win32 service, then it does in fact sound like the client side is dropping the connection or losing their network (e.g., moving outside the range of a wireless router). In the latter case, it's possible that the client remains oblivious to the fact that the connection has been closed (this situation is called "half-open"); the server sees the close but the client thinks the connection is still there.

Is this an ASP web service hosted with some company? If so, the server generally recycles apps every 10 to 20 minutes. You cannot have a web service running indefinitely, unless it's your own server (I believe).

Related

C# Socket exception when Shutdown in called on the client

I am writing a C# client to connect with an embedded system (server). Initially i am able to connect to the server and send data. Then i reboot the server (with the client not being shutdown) and on the server coming to ready state, I first try to disconnect (shutdown) client and reconnect the same. Now during client shutdown i am getting the socket exception 10053 - An established connection was aborted by the software in your host machine.
Can you help me in understanding what could be the issue?
Note: if i try to reconnect without trying to shutdown (after the server reboot) then the connect is working fine and i am able to transmit data.
The socket probably uses a TCP protocol. TCP is designed to keep a constant line of communication between the two. This means that in order to close the connection, both client and server say to each other that the connection will be ended. But as you only get the error after a restart I believe that when you shutdown the server, the server did not get a chance to properly shut down the connection too. So, when trying to restart the connection on the client, it already 'lost' its connection without knowing it as it didn't hear it from the server. Thus, it cannot officially close the socket as the server does not communicate to the socket anymore.
You have two options: accept the exception and use a try catch, which might be less neat. The other option is to try and force the server to officially close the socket before or during shutdown, so that the client is informed. Then, the client will retry starting a connection every few minutes.
I can't go into specifics as I haven't worked much with the code yet, but I hope this seems clear to you.

Outgoing client TCP ports get blocked when there are too many connections

I have a distribution system for doing file operation with and running shell commands on target client machines on windows. and I use a custom TCP endpoint for connecting to the windows service which is resident on the server.
Now I've created this tool to create numerous instances of that agent(client) on one machine and run a certain job-set against all of them from the server. The problem is that all outgoing TCP ports on the client machine gets blocked after launching more than a few hundred agents. each agent is using a dynamic port and is listening to a single server port.
Say, i got 2000 agents running on ports 2000-3999 and all are listening to port 5111 on the server.
The error message i'm receiving in windows event log goes like this:
TCP/IP failed to establish an outgoing connection because the selected
local endpoint was recently used to connect to the same remote
endpoint. This error typically occurs when outgoing connections are
opened and closed at a high rate, causing all available local ports to
be used and forcing TCP/IP to reuse a local port for an outgoing
connection. To minimize the risk of data corruption, the TCP/IP
standard requires a minimum time period to elapse between successive
connections from a given local endpoint to a given remote endpoint.
When this occurs this machine cannot use any TCP port anymore. I did try changing some of the TCP default behavior in registry but to no avail. The interval between opening connections is between 1 to 5 seconds.
Any workaround for managing the optimal delay and/or somehow make windows trust the application irrespective of the aggressive network activity required for the test?
Turns out if you open connections without a proper gap in between, all ports on the client will get blocked due to aggressive behavior. Finally i got to connect all my agents by increasing the delay between each connection from 1000 ms to 3000 ms. I am still to figure out the dynamics of this though, and probably opening agents in parallel threads rather than processes would be a better solution after all. High number of processes with the same name seemingly doesn't appeal to the OS.

How to maintain SignalR Groups and Connections after SignalR server restart?

My system is composed of
SignalR server
Multiple C# clients connecting to said SignalR server.
As I understand, once connected each of these clients would have its own associated ConnectionId.
Now, I want to implement a resiliency strategy where after the SignalR server is restarted, it should still retain the Groups and Connections it used to have in the Hub.
I was thinking of achieving this by storing the Groups and ConnectionIds in an external storage (e.g. database), and restore it when the application starts up.
When the server goes down, the clients' connection might have dropped. But this can be mitigated somewhat by making the client always attempt to reconnect on disconnection. Once the server is up, the client would reconnect.
However, this solution feels rather flaky. In particular, I'm not sure whether once the client reconnects it will retain the same ConnectionId.
Does this approach make sense? Is there a better way to do it?
Yes, client-reconnects ALWAYS happen with the same connectionID. The connectionID is renewed ONLY in case:
the connection.stop() method is called from the client,
the server has detected client disconnection and does a disconnect on the server + sends a disconnect message to clients that happen to reconnect too late. Then those clients will close connection. So client-server are synced again.
If the client is connected to a server that is about to reboot, the clients will notice upon disconnection and try to reconnect to the server with the same connectionID, all within a given timeframe, defined by the connection-timeout. If the server, then, reboots within the connection timeout frame, the client reconnects to the server, with the existing ID.
In this case the Reconnect() event is fired on the server, without the OnConnected() event is happened. This is an exceptional signalr case.
Code your Reconnect() events very defensively.
link to official documentation explaining this issue
chapter: Server disconnection scenarios

Client usually disconnected from server after a few dozen minutes

I've created a server-client communicate program in .NET (c# or vb.net) using TCPListener - Socket on port 8080. In simple words, the program work like a chat software, client connect to server, and both wait for message from each other and then process it.
To retrieve packet from client, i using are using a "While" method like this :
While true
Dim Buffer(4096) As Byte
s.Receive(Buffer)
Dim strDataReceived As String = System.Text.Encoding.ASCII.GetString(Buffer)
ProcessData(strDataReceived) 'Process data received...........
End while
When testing both server.exe-client.exe in local, the software work fine for several hours without any problem.
But when i start to run the server.exe in my real server, the connection between server-client usually become lost each other when client connected after a few dozen minutes. The symptom is client send packet to server but server does not receive the packet from client when server is still standing in 'sck.receive(Buffer)' command. I have tested many times but i still have no lucky to keep the connection run over 1 hour.
I have investigated about this problem but it still very strange :
The server did not installed any firewall software.
The client did not using any proxy and antivirus, firewall software
I using a bandwidth logging software on server to make sure the internet in my server is stable.
I make a 'ping -t' from my client computer to the server and keep looking on it to sure there are no connection lost between client and server . The ping command indicate that the ping time is usually range from 5ms to 50ms and no connection time out occur.
Even I try to unplug the network cable in the client computer for a few seconds, and then replug again to simulation the disconnect event. I've awesome that my connection between server-client is still maintain and it's not the problem that cause my symptom.
I was thinking to write a code for auto reconnect if received timeout. But it could make my software usually delay when reconnecting if the above symptom still there. I really want to know what wrong with my code and which is the solution for me to fix the above symptom?
Likely the server is behind some sort of firewall (Cisco ASA, etc.) which has idle connection timeouts. When you "punch" through a firewall / NAT device, a "session" is created within the firewall kernel. There is an associated resource that has to be reclaimed, so firewalls do not usually allow unlimited connection timeout, but firewalls do support things like dead connection detection.
Adding a keepalive packet / activity every 5 minutes, or disconnecting / reconnecting is the only way around that. Few network admins are going to change their configs to accomodate this. It is pretty simple to implement a "ping" or "keepalive" command in custom TCP application protocols. Just send the string and consume it, you don't even have to respond to the packet to accomplish resetting the idle timer within the firewall, though that would probably be best practice.
When I say keepalive, I don't mean the TCP keepalive socket option. That is a zero-length packet, and is detectable by a good firewall, like Cisco. Cisco admins can setup rules to quietly deny your keepalive packet, so the solution is to implement it above the TCP layer, in the Application layer, by sending a small string of data like "KEEPALIVE\r\n".

Socket Connection Error

I am facing a wierd socket connection problem in .net windows application.I am using socket from .net to asynchonously communicate to a legacy intersystems cache database.I have a specific timeout value in the application, when the timeout occurs, user is prompted to stay connected to the application. When I say stay connected, socket is not being reset. I set timeout to 30 mins and say stay connected for first idle time.Then when I navigate the application it works fine.
If with out navigating in the application and say stay connected second time, and navigate in the app I am getting socket "host refused" connection error. This I can assume may be socket is terminated. But the wierd part is if I set the application timeout to 10 mins, then also I am getting socket error second time. When I check the sockets connected property, it is still true. I am not catching exception when I call sockets Send method. But the data is not passed from the socket.I have checked the other .net code. it is fine. This problem also occurs rarely, only 1 in 10 times. Any suggestions will be greatly helpful.
This sounds like a typical issue resulting from firewalls or other TCP settings.
Firewalls might silently disconnect the connection if it is idle more than x seconds.
As the TCP protocol does not generate an event in such a case (similar like just removing the network cable), it is highly recommended to send ping message every x seconds, so that the firewall stays open and that you can be sure to be connected. if the ping is missed, the server disconnects the client.

Categories

Resources