The secure shutdown method for sockets in network programming
/// <summary> ///Close the socket safely. /// </summary> /// <param name= "Socket" >The socket.</param> Public Static voidSafeclose ( Thissocket Socket) { if(Socket = =NULL) return; if(!socket. Connected)return; Try{socket. Shutdown (Socketshutdown.both); } Catch { } Try{socket. Close (); } Catch { } } /// <summary> ///sends the data. /// </summary> /// <param name= "Client" >The client.</param> /// <param name= "Data" >The data.</param> Public Static voidSendData ( ThisSocket Client,byte[] data) {senddata (client, data,0, data. Length); } /// <summary> ///sends the data. /// </summary> /// <param name= "Client" >The client.</param> /// <param name= "Data" >The data.</param> /// <param name= "offset" >The offset.</param> /// <param name= "Length" >The length.</param> Public Static voidSendData ( ThisSocket Client,byte[] Data,intOffsetintlength) { intSent =0; intThissent =0; while((length-sent) >0) {thissent= client. Send (data, offset + sent, length-sent, socketflags.none); Sent+=thissent; } }
Safe shutdown for C # sockets