Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# .Net Socket Keep Alive time?

Newbie question here.. Just trying to be sure here..

This is not a HTTP Keep-Alive question.

Is there a way to make sure a socket gets closed() after a certain amount of time.

Why i'm asking this because if I just close socket right away.. the client may not receive the packet to indicate like incorrect password etc..

Is there any timeout like built in command inside Socket?

I'm trying to avoid building a timer or event to close it off thinking of a alternate solution

like image 697
SSpoke Avatar asked Mar 23 '26 19:03

SSpoke


1 Answers

I never tried it myself, but there is LingerOption.

Take a look at Socket.LingerState Property.

This property controls the length of time that a connection-oriented connection will remain open after a call to Close when data remains to be sent. When you call methods to send data to a peer, this data is placed in the outgoing network buffer. This property can be used to ensure that this data is sent to the remote host before the Close method drops the connection.

like image 139
Alex Aza Avatar answered Mar 25 '26 09:03

Alex Aza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!