TCP is a basic network protocol. Basically, all network services are based on TCP, such as HTTP and FTP, therefore, to understand network programming, you must understand TCP-based programming. However, TCP is a complex system. It is not a day or two to fully understand its implementation. Fortunately, it is. In the. NET Framework environment, we do not have to investigate the implementation of the TCP protocol at the underlying layer. In the same way, we can easily compile network communication based on the TCP protocol.Program.
C # TCP-based Network Communication
To implement C # TCP-based network communication, you must first establish a connection with the remote host. The connection address usually consists of two parts: host name and port, for example, www.yesky.com: 80,
Www.yesky.com is the host name and port 80 of the host. Of course, the host name can also be replaced by an IP address. After the connection is established, you can use this connection to send and receive data packets. The role of the TCP protocol is to ensure that these data packets can reach the end point and be assembled in the correct order.
In. The class library of Net Framework provides two classes for TCP network communication: tcpclient and tcplistener. the English meaning is obvious. The tcpclient class is a TCP-based client class, while the tcplistener is a server client that listens for connection requests from the listen client. The tcpclient class communicates with the server through the TCP protocol and obtains information. It encapsulates an instance of the socket class, which is used to request and obtain data from the server through the TCP protocol. Because the interaction with the remote host occurs in the form of data streams, the transmitted data can be used. Net
Framework stream processing technology read/write. In the following example, you can see how to use the networkstream class to operate data streams.
In the following example, we will create a time server, including the server and client programs. The server listens to client connection requests and sends the current system time to the client after the connection is established.
Run the server program first. The following shows the running status of the server program:
Then run the client program. The client first sends the connection request to the server, and then sends the current time to the client after the server responds. This is the client program:
After sending the message, the server continues to wait for the next connection:
In this example, we can understand the basic usage of the tcpclient class. To use this class, you must use the system. net. Socket namespace. The three namespaces used in this example are as follows:
Using system;
Using system. net. Sockets;
Using system. text;
// Use the class in the namespace when obtaining a string from the byte array
// First, discuss the client program. At first, we must initialize a tcpclient class instance:
Tcpclient client = new tcpclient (hostname, portnum );
// Use the getstream () method of the tcpclient class to obtain the data stream,
And use it to initialize an instance of the networkstream class:
Networkstream NS = client. getstream ();
Note: When you use the host name and port number to initialize the tcpclient class instance, the program can be executed only after the connection is established with the server. If the connection fails because the network is disconnected, the server does not exist, and the server port is not open, the program throws an exception and interrupts execution.
After a data stream is created, we can use the read () method of the networkstream class to read data from the stream and use the write () method to write data to the stream. When reading data, we should first create a buffer. Specifically, we should create a byte array to store the data read from the stream. The prototype of the read () method is described as follows:
Public override int read (in byte [] buffer, int offset, int size)
Buffer is a buffer array, and offset is the starting position of data (byte stream) in the buffer array. size is the number of bytes read, and the return value is the number of bytes read. In this example, we simply use this method to read the server feedback:
byte [] bytes = new byte [1024]; // create a buffer
int bytesread = NS. read (bytes, 0, bytes. length); // read byte streams
// then displayed on the screen:
console. writeline (encoding. ASCII. getstring (bytes, 0, bytesread);
// do not forget to close the connection:
client. close ();