This article simulates a demonstration of whether a server with data greater than 100 accepts connections from multiple clients and provides services for multiple clients. The example is simple and can only be used to simulate one scenario, it serves as a guide for beginners to step into network programming.
Server:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. net; <br/> using system. threading; <br/> using system. net. sockets; <br/> namespace net. dale. tcpservertest <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/> {<br/> try <br/> {<br/> // convert an IP string to an IP instance <br/> IPaddress IPA = IPaddress. parse ("127.0.0.1"); <br // Listening port 8000 <br/> tcplistener mylsn = new tcplistener (IPA, 8000); <br/> // enabling listening <br/> mylsn. start (); <br/> // output information about successful listening <br/> console. writeline ("Start service on port 8000, wait for connection"); <br/> // wait for processing access connection requests <br/> while (true) <br/> {<br/> socket mysock = mylsn. acceptsocket (); <br/> console. writeline ("connected, connection from" + mysock. remoteendpoint + "" + mysock. gethashcode (); <br/> // construct a new instance <br/> dowork DW = new dowork (mysock, mylsn); <Br/> thread RCV = new thread (New threadstart (DW. rcvmethod); <br/> RCV. start (); <br/>}< br/> catch <br/>{}< br/> finally <br/>{}< br/>}< br/ >}< br/> public class dowork <br/>{< br/> Public socket mysock; <br/> Public tcplistener mylsn; <br/> Public dowork (socket S, tcplistener t) <br/>{< br/> mysock = s; <br/> mylsn = T; <br/>}< br/> Public void rcvmethod () <br/>{< br/> while (True) <br/>{< br/> try <br/> {<br/> // receives client messages <br/> byte [] DATA = new byte [100]; <br/> mysock. receive (data); <br/> string RT = system. text. utf8encoding. utf8.getstring (data); <br/> console. writeline (RT); <br/> If (Int. parse (RT)> 100) <br/>{< br/> // return information <br/> mysock. send (utf8encoding. utf8.getbytes ("your input data is greater than 100 ")); <br/>}< br/> else <br/> {<br/> // return information <br/> mysock. send (utf8encoding. utf8.getby TES ("the data you entered is less than 100"); <br/>}< br/> catch <br/>{< br/> console. writeline (mysock. gethashcode (); <br/> console. writeline ("a client is disconnected! "); <Br/> mysock. close (); <br/> break; <br/>}< br/>
Note: When the server listens to a new client connection, it will create a separate thread to serve the client;
Client:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. io; <br/> using system. net; <br/> using system. net. sockets; <br/> using system. threading; <br/> namespace net. dale. tcpclienttest <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> // create a client socket <br/> tcpclient tclient = new tcpclient (); <br/> // connect to the server <br /> Tclient. connect ("127.0.0.1", 8000); <br/> // obtain the stream <br/> stream STM = tclient. getstream (); <br/> console. writeline ("Enter the message to be sent"); <br/> // construct a new instance <br/> dowork DW = new dowork (STM, tclient ); <br/> thread snd = new thread (New threadstart (DW. sndmethod); <br/> SND. start (); <br/> SND. join (); </P> <p >}< br/> public class dowork <br/>{< br/> Public stream STM; <br/> Public tcpclient tclient; </ P> <p> Public dowork (Stream S, tcpclient t) <br/>{< br/> STM = s; <br/> tclient = T; <br/>}< br/> Public void sndmethod () <br/>{< br/> while (true) <br/>{< br/> try <br/>{< br/> // read the characters to be transmitted <br/> string MSG = console. readline (); <br/> int error = int. parse (MSG); <br/> If (MSG = "exit" | MSG = "exit") <br/>{< br/> console. writeline ("client exited successfully! "); <Br/> break; <br/>}< br/> // sending string <br/> byte [] DATA = system. text. utf8encoding. utf8.getbytes (MSG); <br/> STM. write (data, 0, Data. length); <br/> // receives the message returned by the server <br/> byte [] Back = new byte [1024]; <br/> int K = STM. read (back, 0, 1024); <br/> // output the message returned by the server <br/> console. writeline (system. text. utf8encoding. utf8.getstring (back); <br/>}< br/> catch (exception ERR) <br/>{< br/> console. writeline (err. m Essage + "/n program exited! "); <Br/> break; <br/>}< br/>