i have an web app. it opens a socket to a server. sends a message and waits for a response. The user may then perform another socket request to the server or may give it 5, 10, 15 mins(etc) and then send another message to the server. Or may close the web app.
Should i close the socket after each send/receive request or keep it open?
Thanks
You can close socket and make new connection if some addition delay (connect time is about round trip time (ping time)) is not a problem. If you will use SSL in the future it is better to keep session alive because the SSL connection establish much more difficult from the CPU resource point of view. Consider SO_KEEPALIVE socket option for permanent connections.
Related
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.
I know TCP won't break an established TCP connection during a period of time. But is it possible to increase this time?
I am developing a client-server application used to stream continuous data from one server to the client application using C#. I want to keep collecting data if the cable connection reconnected within one minute.
There are three timeouts that you can use in the tcp client:https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient?view=netcore-3.1#properties
Connect timeout. The time it takes to establish the connection
Receive timeout. The timeout on the receiving side once the reading is initiated: https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient.receivetimeout?view=netcore-3.1#System_Net_Sockets_TcpClient_ReceiveTimeout
Send timeout. The timeout on the sender side once the sending is initiated: https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient.sendtimeout?view=netcore-3.1#System_Net_Sockets_TcpClient_SendTimeout
Those are the out of the box settings you can use. If you didn't mean one of these, please clarify in your question.
I have a Client(s)/Server TCP scenario with TLS connections written in C#.
The clients are failing to deliver messages after they go quiet and resume (inactivity time ~25 minutes). But If I keep the clients chatting (every 30 seconds) there is no problem.
Neither the client nor the server are seeing a disconnection, but traffic stops flowing (TLS Session break??? I am just guessing here).
I need to keep the connection in place because the server needs to response back at any time.
The server has KeepAlives every 5 minutes once the TCP socket connects and TLS authenticates
Q1) Is there a way to configure the SslStream or the socket in C# to use TicketKeys and reuse the session?
Q2) If the problem is not Session reuse, if I use a WireShark or NetMonitor, what should I be looking for, to determine why the traffic is no longer flowing even if the parties believe they are connected?
Thx
Don't set timeout on server and client
sslStream.ReadTimeout = 5000;
sslStream.WriteTimeout = 5000;
Set big timeout
Implement something like: Ping/Pong heartbeat command and in client, in separate thread, send Ping to server every X seconds
i have a TCP server software. I open a thread for every TCP client. (This is not important why i open a thread for every TCP client.) I want to kick a TCP client any time and want to kill its thread. How can i find the thread of TCP client that i want to kick out of server?
Thank you.
You need to keep track of all TcpClient's in existence. For example, use a list to store them. That way you have access to them.
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).