Server:
Class client {tcpclient clientsocket; streamwriter writer; Public client (tcpclient client) {clientsocket = client; networkstream stream = clientsocket. getstream (); writer = new streamwriter (stream, encoding. ASCII);} public void write (string Str) {// flush is also very important. If you want to send it, the client will receive it immediately. Please use this option. Otherwise, the client will put the data in the buffer zone and wait for the writer. writeline (STR); writer. flush ();} public void close () {// if close is called and the client waits for a period of time before reading, the client will not receive the data or prompt that the server has been disconnected. // No, the socket will not be closed only after the operation is completed immediately. close directly Program . At this time, the client read operation will report a connection disconnection exception // before this, the client will continue to read the data sent by the server. // note: as long as the server sends data first and closes the socket and listens in sequence through normal procedures, and then the program exits // The client will not be faulty. // it seems that the client has not received data yet. // The write method is actually running. this data has been written to the client buffer (this small data is actually passed in an instant) // TCP is to ensure that the data has correctly arrived at the target machine and will continue sending the subsequent data only after receiving a confirmation. // TCP maintains the data integrity through this mechanism // server normally close, the client's only judgment method is that the read method returns 0, indicating that the other end of the connection has exited // as long as there is a connection, the client will be blocked if it calls the synchronous read method and does not read the data thread. is always stuck there // if the data is not flushed immediately, he will not receive the data temporarily and will also be stuck there // if the server is not properly closed, the client will generate a connection when reading disconnection exception // Based on the above, the two ends of the program should "discuss" and exit the program together, usually send a message to the other side to receive the message and then close the writer. close (); clientsocket. close () ;}} class program {static void main (string [] ARGs) {tcplistener Server = new tcplistener (6000); server. start (); // while (true) // {client c = new client (server. accepttcpclient (); console. writeline ("New Connection"); // thread. sleep (2000); For (INT I = 0; I <3; I ++) {C. write ("Hello, welcome ^_^" + I. tostring (); // thread. sleep (1000);} thread. sleep (5000); C. write ("finally"); console. writeline ("sent successfully"); thread. sleep (10000); C. close (); // thread. sleep (100000); //} server. stop ();}}
client:
Class program {static void main (string [] ARGs) {tcpclient client = new tcpclient (); client. connect (IPaddress. parse (@ "127.0.0.1"), 6000); console. writeline ("connected"); streamreader reader = new streamreader (client. getstream (); // thread. sleep (6000); While (true) {try {byte [] DATA = new byte [1024]; // If (client. available> 0) // {// read method // The third parameter is not necessarily client. available is unknown because of the amount of data sent by others. // you can decode the number of bytes you want. The number of bytes to read is correct. // you may think that the number of reads returned by the read method is 0. TCP is connected and stateful. Therefore, each read operation must be able to read something. // If the synchronous read method is called, no reading what the other Party sends will suspend until data is read. // If 0 is returned immediately after the call, the connection is disconnected. // thread. sleep (2000); int Reds = reader. basestream. read (data, 0, Data. length); If (Reds = 0) break; If (encoding. ASCII. getstring (data ). indexof ("finally ")! =-1) {console. writeline (encoding. ASCII. getstring (data, 0, Reds); break;} If (Reds <data. length) console. writeline (encoding. ASCII. getstring (data, 0, Reds); else console. writeline (encoding. ASCII. getstring (data )); // when there is no data in the buffer zone, it is inappropriate to interrupt reading immediately. // it is possible that the network delay data has not been sent yet. //} // else // break ;} catch (exception ex) {console. writeline ("server disconnected:" + ex. message); break ;}} console. writeline ("over"); client. close (); console. readkey ();}}