The following are the server'sCodeRecently, I have been studying network programming and have been thinking about how to develop highly concurrent servers. I often hear that the server is described as 10000 connections at the same time. I wonder if I can use C # To write such a server. The Synchronous Programming Model is not taken into consideration. Let's see if the asynchronous programming model of tcplistener can meet the requirements.
Public partial class comservice {private int maxlink = 100000; private int currentlinked; private manualresetevent tcpclientconnected = new manualresetevent (false); Public void start () {thread = new thread (New parameterizedthreadstart (showstat); thread. isbackground = true; thread. start (); tcplistener Server = new tcplistener (new system. net. ipendpoint (0, 8090); server. start (100); tcpclientconnecte D. reset (); iasyncresult result = server. beginaccepttcpclient (New asynccallback (acceptor), server); tcpclientconnected. waitone ();} private void showstat (Object O) {While (true) {lock (typeof (comservice) {console. writeline ("current connections:" + currentlinked + "/" + maxlink);} thread. sleep (2000) ;}} private void acceptor (iasyncresult O) {tcplistener Server = O. asyncstate as tcplistener; debug. assert (server! = NULL); tcpclient client = NULL; try {client = server. endaccepttcpclient (o); system. threading. interlocked. increment (ref currentlinked);} catch {} iasyncresult result = server. beginaccepttcpclient (New asynccallback (acceptor), server); If (client = NULL) {return;} else {thread. currentthread. join () ;}close (client) ;}private void close (tcpclient client) {If (client. connected) {client. client. shutdown (socketshutdown. both);} client. client. close (); client. close (); system. threading. interlocked. decrement (ref currentlinked );}}}
The following is the client code:
Public class clientpool {Private Static list <tcpwork> clients = new list <tcpwork> (); Private Static int freecount; Private Static int workcount; Private Static int maxallowed = 2; private Static int minclients = 2; // <summary> // create new instance // </Summary> private clientpool () {} Private Static clientpool instance; private Static readonly object syncinstanceobj = new object (); public static clientpool Singleton {get {If (instance = NULL) {lock (syncinstanceobj) {If (instance = NULL) {instance = new clientpool () ;}} return instance ;}} Private Static readonly object syncobj = new object (); Public tcpwork getclient () {try {tcpwork work = new tcpwork (); Work. connect ("127.0.0.1", 8090); Work. lingerstate = new lingeroption (false, 3); Work. iswork = true; work. expired = false; workcount ++; lock (syncobj) {clients. add (work);} console. writeline (workcount); return work;} catch (exception ex) {console. writeline (ex. message); return NULL ;}}}
The client simulates multi-thread concurrency:
Class program {static void main (string [] ARGs) {for (INT I = 0; I <1000; I ++) {threadpool. queueuserworkitem (New waitcallback (work), null);} console. readkey ();} Private Static void work (Object O) {clientpool. singleton. getclient ();}}
From this programming model, we can see that a high-concurrency server not only needs to meet the number of concurrent connections, but also the number of connections created per second is an important indicator ~~
In actual operation, we can see that tcplistener can create two connections per second. Other connections will be rejected and the persistent connections will be 1000. Obviously, tcplistener will be squashed out ~~
Note that the above Code is only used for testing and trial connection. This write method will cause the connection on the server end to fail to be released ~~~~