Server code
[C-sharp]
View plaincopy
- Using system;
- Using system. Collections. Generic;
- Using system. text;
- Using system. net;
- Using system. net. Sockets;
- Using system. Threading;
- Namespace TCP Communication
- {
- Class Program
- {
- Static void main (string [] ARGs)
- {
- Try
- {
- // Convert an IP address to an instance
- IPaddress IPA = IPaddress. parse ("127.0.0.1 ");
- // Listening port 8001
- Tcplistener mylsn = new tcplistener (IPA, 8001 );
- // Enable the listener
- Mylsn. Start ();
- // Output successful listening Information
- Console. writeline ("starting service in 8001, waiting for connection ");
- // Wait for processing the Access Connection Request
- While (true)
- {
- Socket mysock = mylsn. acceptsocket ();
- Console. writeline ("connected, connected from" + mysock. remoteendpoint );
- Work W = new work ();
- W. mysock = mysock;
- W. mylsn = mylsn;
- Thread t = new thread (New threadstart (W. Main ));
- T. Start ();
- }
- }
- Catch
- {}
- Finally
- {}
- }
- }
- Public class work
- {
- Public socket mysock;
- Public tcplistener mylsn;
- Public void main ()
- {
- // Receives client messages
- Byte [] DATA = new byte [100];
- Mysock. Receive (data );
- String RT = system. Text. utf8encoding. utf8.getstring (data );
- Console. writeline (RT );
- // Send a message to the client
- Mysock. Send (utf8encoding. utf8.getbytes ("send back to client "));
- // Release resources
- Mysock. Close ();
- Mylsn. Stop ();
- }
- }
- }
Client code
[C-sharp]
View plaincopy
- Using system;
- Using system. Collections. Generic;
- Using system. text;
- Using system. IO;
- Using system. net;
- Using system. net. Sockets;
- Namespace TCP communication Client
- {
- Public class class1
- {
- Public static void main ()
- {
- // Create a client socket
- Tcpclient tclient = new tcpclient ();
- // Connect to the server
- Tclient. Connect ("127.0.0.1", 8001 );
- Console. writeline ("Enter the message to be sent ");
- // Read the characters to be transmitted
- String STR = console. Readline ();
- // Get the stream
- Stream STM = tclient. getstream ();
- // Send a string
- Asciiencoding ASEN = new asciiencoding ();
- Byte [] DATA = Asen. getbytes (STR );
- STM. Write (data, 0, Data. Length );
- // Accept the message returned by the server
- Byte [] Back = new byte [100];
- Int K = STM. Read (back, 0,100 );
- // Output the message returned by the server
- For (INT I = 0; I <K; I ++)
- {
- Console. Write (convert. tochar (back [I]);
- }
- // Close the connection
- Tclient. Close ();
- }
- }
- }
According to the original design intention of the TCP/IP designer, when mysock. Close () is used to view the local connection with the command netstat-a-n, the corresponding link status is time_wait. The system automatically closes the connection.
Server code
[C-sharp]
View plaincopy
- Using system;
- Using system. Collections. Generic;
- Using system. text;
- Using system. net;
- Using system. net. Sockets;
- Using system. Threading;
- Namespace TCP Communication
- {
- Class Program
- {
- Static void main (string [] ARGs)
- {
- Try
- {
- // Convert an IP address to an instance
- IPaddress IPA = IPaddress. parse ("127.0.0.1 ");
- // Listening port 8001
- Tcplistener mylsn = new tcplistener (IPA, 8001 );
- // Enable the listener
- Mylsn. Start ();
- // Output successful listening Information
- Console. writeline ("starting service in 8001, waiting for connection ");
- // Wait for processing the Access Connection Request
- While (true)
- {
- Socket mysock = mylsn. acceptsocket ();
- Console. writeline ("connected, connected from" + mysock. remoteendpoint );
- Work W = new work ();
- W. mysock = mysock;
- W. mylsn = mylsn;
- Thread t = new thread (New threadstart (W. Main ));
- T. Start ();
- }
- }
- Catch
- {}
- Finally
- {}
- }
- }
- Public class work
- {
- Public socket mysock;
- Public tcplistener mylsn;
- Public void main ()
- {
- // Receives client messages
- Byte [] DATA = new byte [100];
- Mysock. Receive (data );
- String RT = system. Text. utf8encoding. utf8.getstring (data );
- Console. writeline (RT );
- // Send a message to the client
- Mysock. Send (utf8encoding. utf8.getbytes ("send back to client "));
- // Release resources
- Mysock. Close ();
- Mylsn. Stop ();
- }
- }
- }
Client code
[C-sharp]
View plaincopy
- Using system;
- Using system. Collections. Generic;
- Using system. text;
- Using system. IO;
- Using system. net;
- Using system. net. Sockets;
- Namespace TCP communication Client
- {
- Public class class1
- {
- Public static void main ()
- {
- // Create a client socket
- Tcpclient tclient = new tcpclient ();
- // Connect to the server
- Tclient. Connect ("127.0.0.1", 8001 );
- Console. writeline ("Enter the message to be sent ");
- // Read the characters to be transmitted
- String STR = console. Readline ();
- // Get the stream
- Stream STM = tclient. getstream ();
- // Send a string
- Asciiencoding ASEN = new asciiencoding ();
- Byte [] DATA = Asen. getbytes (STR );
- STM. Write (data, 0, Data. Length );
- // Accept the message returned by the server
- Byte [] Back = new byte [100];
- Int K = STM. Read (back, 0,100 );
- // Output the message returned by the server
- For (INT I = 0; I <K; I ++)
- {
- Console. Write (convert. tochar (back [I]);
- }
- // Close the connection
- Tclient. Close ();
- }
- }
- }
According to the original design intention of the TCP/IP designer, when mysock. Close () is used to view the local connection with the command netstat-a-n, the corresponding link status is time_wait. The system automatically closes the connection.