The demo is divided into two parts. The first part is the server, and the second part is the client.
First, the server sideCode:
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. text; using system. windows. forms; using system. net; using system. threading; using system. net. sockets; namespace server {public partial class form1: FORM {public form1 () {initializecomponent ();}/** Program Introduction: I want to complete a demo that simulates the communication between 20000 communication nodes and the server. In the test, the server sends a request to the client every five seconds * request data: obtain the current time, convert the time difference between the current time and the current time to seconds, and return to the server * development time: July 15, * Author: Sun Xiaocong **/private void form1_load (Object sender, eventargs e) {thread t = new thread (New threadstart (backgroundlisten); T. isbackground = true; T. start ();} public void backgroundlisten () {IPaddress = IPaddress. any; tcplistener = new tcplistener (IPaddress, 13000); tcplistener. start (); While (true) {tcpclient = tcplistener. accepttcpclient (); myclient newclient = new myclient (tcpclient); thread t = new thread (New threadstart (newclient. communicate); T. start ();}}}}
A class myclient is required.
Using system; using system. collections. generic; using system. text; using system. net. sockets; namespace server {class myclient {tcpclient m_tcpclient; Public myclient (tcpclient TC) {m_tcpclient = tc;} public void communicate () {networkstream NS = m_tcpclient.getstream (); int Recv; string mess; while (true) {byte [] bytes = new byte [1024]; try {If (Recv = NS. read (bytes, 0, 1024) = 0) {console. W Riteline ("the connection has been disconnected ...... "); Break ;}} catch {console. writeline (" Connection interrupted ...... "); Break;} mess = system. text. encoding. ASCII. getstring (bytes, 0, Recv); console. writeline (m_tcpclient.client.addressfamily.tostring () + ":" + mess); bytes = system. text. encoding. ASCII. getbytes (mess. toupper (); try {ns. write (bytes, 0, bytes. length);} catch {console. writeline ("the connection has been interrupted ...... "); Break ;}} ns. Close (); m_tcpclient.close ();}}}
client
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. text; using system. windows. forms; using system. threading; using system. net. sockets; using system. net; namespace client {public partial class form1: FORM {public form1 () {initializecomponent ();} private void form1_load (Object sender, eventargs E) {// create tcpclinet tcpclient = new tcpclient (); tcpclient for communication. connect (IPaddress. parse ("127.0.0.1"), 13000); networkstream NS = tcpclient. getstream (); char B = 'a'; byte [] bytes = new byte [1024]; int Recv; string mess; while (true) {mess = B. tostring (); // convert 'A' to string if (B> 3 + 'A') mess = "bye "; // send a B c d bye bytes = System in sequence. text. encoding. ASCII. getbytes (MESS); try {ns. write (bytes, 0, bytes. length);} catch {console. writeline ("missing"); break;} bytes = new byte [1024]; try {If (Recv = NS. read (bytes, 0, 1024) = 0) {console. writeline ("disconnected"); break ;}} catch {console. writeline ("missing"); break;} mess = system. text. encoding. ASCII. getstring (bytes, 0, Recv); MessageBox. show (MESS); thread. sleep (1000); If (B> 3 + 'A') break; ++ B;} ns. close (); tcpclient. close ();}}}