5 Replies - 1889 Views - Last Post: 30 August 2013 - 01:16 PM Rate Topic: -----

#1 Rhino1111  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 259
  • Joined: 28-August 13

async socket behaviour question.

Posted 28 August 2013 - 01:15 AM

This is more of a general question, so I won't be including any code, since I don't believe that is the actual problem.
I recently started "getting my feet wet" in socket programming. I'm coding using C#, using the async socket operations(BeginReceive, EndReceive, etc), and TCP protocol.

I'm developing a chat application, and ran into a weird error, which doesn't seem to be documented all that well anywhere.
basically, I run my server, the chat application connects to me. but if data hasn't been sent between that socket connection in the last ~5min, it seems like the socket connection silently closes behind the scenes.

I say this because if you haven't sent data in the past ~5min, the next attempt to do so returns a 10060(timeout) socket error, and any attempt to send data after that returns a 10054 socket error(this behavior happens on both server and client after ~5min of no activity).

Is this regular behavior? I can easily implement a "Pulse" system from client to server to stop the connection from closing after 5min of no activity, but I only want to do so if this is actually regular behavior.

If there's a possibility this is an error with my code, I'll post the entire project here upon request.

Is This A Good Question/Topic? 0
  • +

Replies To: async socket behaviour question.

#2 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4238
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: async socket behaviour question.

Posted 28 August 2013 - 01:44 AM

I ran into a similar issue ages ago. What I found was that configuring the idle timeout for a socket is more or less impossible from within C#. The best thing to do in order to keep a socket connection alive for any length of time is to keep pinging the connection every few minutes by sending some small amount of data to the client.

The most reasonable explanation I found for the sockets being silently discarded was that any routers in between the two connection points could potentially be throwing away their mapping information for the connection after a few minutes of it being idle. After that a new connection would have to be created in order for them to properly route the packets again. - With that said, I am no expert on these things, so I may be entirely wrong here. The only other halfway reasonable explanation I found was that there was some sort of global timeout set in Windows for it's TCP connection handling, but this never made much sense to me, because surely Windows would not disconnect a connection silently; there would be some sort of feedback indicating the timeout.

In any case, whatever is causing this, having the code send something every now and then seems to solve it.
Was This Post Helpful? 3
  • +
  • -

#3 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: async socket behaviour question.

Posted 28 August 2013 - 07:19 AM

And it's just smart to build in a pinging mechanism. Not only will it keep your application alive, but it will give you a heartbeat. If the heartbeat stops, you know you need to re-initialize your connection (or that you've lost network connectivity, or that your client/server has).

While you're at making a chat application, mind if I offer some direction? Check out WebSockets. More specifically, check out SignalR. It's ideal for that kind of thing. Plus, it'll let you mess around with persistent parallel connections on the web, which is really cool. It's different than what you're used to, but it's pretty cool new technology. Plus there's a server, JS client, and .NET client library, so you're covered however you want to work.
Was This Post Helpful? 2
  • +
  • -

#4 Rhino1111  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 259
  • Joined: 28-August 13

Re: async socket behaviour question.

Posted 28 August 2013 - 12:26 PM

View PostAtli, on 28 August 2013 - 01:44 AM, said:

I ran into a similar issue ages ago. What I found was that configuring the idle timeout for a socket is more or less impossible from within C#. The best thing to do in order to keep a socket connection alive for any length of time is to keep pinging the connection every few minutes by sending some small amount of data to the client.

The most reasonable explanation I found for the sockets being silently discarded was that any routers in between the two connection points could potentially be throwing away their mapping information for the connection after a few minutes of it being idle. After that a new connection would have to be created in order for them to properly route the packets again. - With that said, I am no expert on these things, so I may be entirely wrong here. The only other halfway reasonable explanation I found was that there was some sort of global timeout set in Windows for it's TCP connection handling, but this never made much sense to me, because surely Windows would not disconnect a connection silently; there would be some sort of feedback indicating the timeout.

In any case, whatever is causing this, having the code send something every now and then seems to solve it.


Thanks a lot for the insight. I've been looking into this for the past day or two and haven't been able to find any decent information pertaining to the problem. Your post pretty much cleared up everything I was wondering!
(I also believe the cause of the issue is probably the router throwing away the mapping information).


View PostCurtis Rutland, on 28 August 2013 - 07:19 AM, said:

And it's just smart to build in a pinging mechanism. Not only will it keep your application alive, but it will give you a heartbeat. If the heartbeat stops, you know you need to re-initialize your connection (or that you've lost network connectivity, or that your client/server has).

While you're at making a chat application, mind if I offer some direction? Check out WebSockets. More specifically, check out SignalR. It's ideal for that kind of thing. Plus, it'll let you mess around with persistent parallel connections on the web, which is really cool. It's different than what you're used to, but it's pretty cool new technology. Plus there's a server, JS client, and .NET client library, so you're covered however you want to work.


Thanks for the advice, I'll look into building a pinging mechanism asap.
I'll also check out those links when I get the time, but currently I want to work directly with the async sockets, once I have them figured out i'll look into trying out different things :).
Was This Post Helpful? 0
  • +
  • -

#5 Rhino1111  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 259
  • Joined: 28-August 13

Re: async socket behaviour question.

Posted 29 August 2013 - 04:07 PM

New question.

I know the buffer supplied to the .BeginReceive(buffer...) pins it, but does the buffer supplied to the .BeginSend(buffer...) pin that buffer also?
Was This Post Helpful? 0
  • +
  • -

#6 Rhino1111  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 259
  • Joined: 28-August 13

Re: async socket behaviour question.

Posted 30 August 2013 - 01:16 PM

View PostRhino1111, on 29 August 2013 - 04:07 PM, said:

New question.

I know the buffer supplied to the .BeginReceive(buffer...) pins it, but does the buffer supplied to the .BeginSend(buffer...) pin that buffer also?


bump.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1