There is nothing to talk about in this article. I just want to mention something different from the client.
The biggest difference between a server and a client is that it does not face a single connection, but some connections. For receiving and sending, it is specific Accept So there is Socketstateobject Parameters are always passed through. This parameter is mainly placed on the corresponding client Socket Connection and some status variables, in Accept This object is created after a connection is entered.
Public VoidListen (IntPort)
{
Ipendpoint IPE =NewIpendpoint (IPaddress. Any, Port );
Worksock =NewSocket (IPE. addressfamily, sockettype. Stream, protocoltype. TCP );
Worksock. BIND (IPE );
Work sock. Listen (100 );
Acpt =NewThread (NewThreadstart (acceptthread ));
Acpt. isbackground =True;
Acpt. Start ();
}
Private VoidAcceptthread ()
{
While(True)
{
Socket asock = worksock. Accept ();
Socketstateobject state =NewSocketstateobject (asock );
If(Onconnect! =Null)
Onconnect (State, system. eventargs. Empty );
Threadpool. queueuserworkitem (NewWaitcallback (receivethreadentrypoint), State );
}
}
Like aboveOnconnectEvents andReceivethreadentrypointMethod will follow up with such a parameter.
managing connections is a complex and technical activity. Data receiving and sending of available connections (or even some reception and sending must be related), connection availability monitoring, and removal of invalid connections. All of these should be recorded as appropriate, in addition, you need to UI the interface triggers events, which are crucial, it is also the most tricky, which involves multi-threaded synchronization and thread blocking.
the best solution to this extremely complex problem is to design, implement, or take it with reference to existing mature solutions. However, I have not been involved in development in these areas before, and I cannot find anything that is ideal at the moment. In addition, the project itself does not have high communication requirements, 10 connections last day (if any customer needs to 10 sign-in terminals, that's absolutely god, so far the most. 5 terminals, generally, no more than 3 ), so in this development, I used some simple methods to manage client connections.
because this part has not been carefully designed, it is temporarily unable to make a serious design, and the final implementation is not satisfactory to you, here we will not describe how to manage connections.