15.7 Controlling Socket Lingering Behavior

 <  Day Day Up  >  

You want to cause a socket to finish sending unsent data after its Close method is called.


Technique

Once a socket connection is closed, any data that has not been sent is lost, even if the data transmission is in midstream. You can change this default behavior by changing the linger time of the socket. The linger time is the amount of seconds a socket remains open after its Close method is called so it can finish sending any remaining data. If the timeout occurs or the remaining data has finished transmitting, the socket closes the connection. To set the linger time, call the SetSocketOption method. The first parameter is a value from the SocketOptionLevel enumerated data type and corresponds to the set of options for a given socket type. Some of these values include Tcp for TCP-based sockets, Udp for UDP sockets, or Socket , which applies to all socket types. The value for the second parameter depends on the value specified in the first parameter. The SocketOptionName enumerated data type contains a list of options corresponding to groups of options for a given SocketOptionLevel . To specify the linger option, use the value SocketOptionName.Linger . The last parameter for SetSocketOption is the corresponding value to set the option to. This parameter can be a byte array, integer, or custom object depending on the option being set. For the Linger option, create a new LingerOption object, passing a true value and the number of seconds to set the linger time in the LingerOption :

 
 // set a linger time of 1 minute LingerOption lingerOption = new LingerOption(true, 60); socket.SetSocketOption(SocketOptionLevel.Socket,     SocketOptionName.Linger, lingerOption); 

Comments

The SetSocketOption method allows you to set several different options on a socket to control its behavior. Once a socket is created, it operates in differing ways, depending on the type of protocol. Not all options are valid for any socket type. The amount of possible options can seem overwhelming, and you can't know how each one affects the socket without investigating further. Luckily, the Socket class is built upon the Berkeley Sockets Interface developed in 1978, which means finding information on a certain options should be no trouble.

The linger option is useful if you want to ensure that any remaining data is sent before the socket is closed. Some other options that might prove useful include the KeepAlive option, which tells the socket to periodically send keepalive packets to the connected computer in between sending and receiving periods when the connection becomes latent. The ReceiveTimeOut and SendTimeOut options control the amount of time the socket remains blocked in a call to Receive or Send before returning due to an unsuccessful transfer of data.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net