The question "the host has forced to close a link" has been delayed for a long time. Now, I have fixed it and posted it for reference.
Using system; using system. collections; using system. collections. specialized; using system. text; using system. threading; using system. net. sockets; using system. net; using system. runtime. serialization; using system. runtime. serialization. formatters. binary; using system. io; using system. data; using system. windows. forms; using system. configuration; using Microsoft. win32; using system. diagnostics; using system. timers; Namespace wsgpsgateway {public partial class tcpserver: FORM {public tcpserver () {initializecomponent () ;}# region Custom field public static manualresetevent alldone = new manualresetevent (false ); /// <summary> /// Listener Control enabling status /// </Summary> private bool state = true; /// <summary> /// declare a thread instance /// </Summary> private thread mythread; /// <summary> /// Server IP // </Summary> private int _ Port = convert. to Int32 (configurationmanager. appsettings ["Port"]); /// <summary> /// Save the hash table of all client calls // </Summary> private hashtable _ transmit_tb = new hashtable (); /// <summary> /// thread used to receive messages /// </Summary> private thread _ receviccethread = NULL; public struct tcpparameter {Public String package; public String IPaddress ;}# endregion # region listening code block // form to run private void tcpserver_load (Object sender, eventargs e) {th Is. windowstate = formwindowstate. minimized; this. opacity = 0; // form transparency form. checkforillegalcrossthreadcils = false; initializecomponent (); mythread = new thread (Listen); mythread. start (); system. timers. timer atimer = new system. timers. timer (); atimer. elapsed + = new system. timers. elapsedeventhandler (timeevent); atimer. interval = 1000; atimer. enabled = true; GC. keepalive (atimer);} private object t Hreadlock = new object (); // start listening private void btnstart_click (Object sender, eventargs e) {// multithreading} // start listening, poll the listening client request and store the client socket in the forwarding table private void listen () {try {IPaddress _ IP = IPaddress. any; socket newsoc = new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. TCP); newsoc. setsocketoption (socketoptionlevel. socket, socketoptionname. reuseaddress, true); ipendpoint locaep = new ipendp Oint (IPaddress. any, _ port); // create a connection newsoc. BIND (locaep); newsoc. listen (100); alldone. reset (); newsoc. beginaccept (New asynccallback (oncall), newsoc); // continue to accept connections from other clients. waitone ();} catch (exception ex) {//} // listener callback private void oncall (iasyncresult AR) {alldone. set (); socket serversoc = (socket) ar. asyncstate; socket clent = serversoc. endaccept (AR); try {If (serversoc! = NULL) {byte [] comes = new byte [1024]; endpoint ENP = clent. remoteendpoint; serversoc. beginaccept (New asynccallback (oncall), serversoc); While (true) {int Re = clent. receive (comes, comes. length, 0); clent. send (encoding. ASCII. getbytes ("8"); tcpparameter parm = new tcpparameter (); parm. package = encoding. utf8.getstring (comes, 0, RE ). tostring (). trim (); parm. IPaddress = clent. remoteendpoint. tostrin G (); If (parm. Package. length! = 0) {receive (parm. package, parm. IPaddress) ;}}} catch (socketexception ex) {/// process parsed data private void receive (string MSG, string IP) {//} # endregion # disable and exit region // form disable private void tcpserver_formclosing (Object sender, formclosingeventargs e) {If (mythread! = NULL) {mythread. Interrupt (); mythread. Abort (); GC. Collect () ;}# endregion }}