A practical background of the procedure
There are more and more friends on the Internet, and more and more problems are encountered, the most common is about Domain name server (DNS), Mail Service (SMTP) and POP3 configuration issues. For example, choose which dns,202.96.0.133 or 202.96.26.243? Do you remember the IP address of your ISP's mail server and domain name server, 202.96.26.243 or 245?
Another problem is that when downloading files, we often face the option of downloading from multiple different hosts (like SunSITE has dozens of mirrored sites). So, which site to choose to download is the fastest?
For this purpose, I have written an application to test the connection between the Internet's common network communication ports (such as Web, Email, FTP, etc.), obtain the service port status and response time metrics, thus providing accurate reference data for us to use the Internet more effectively. The author named the Program Tester (tester), from the following description is not difficult to find, tester can also be used as a simple Internet system performance testing tool.
Although tester also involves other programming methods, in general, tester belongs to the socket application. So we start with the model of socket programming, introduce the design and implementation of tester program.
Second, socket programming model
A socket is an endpoint of network traffic. As we know, network communication usually refers to two hosts or two processes that pass data between them over the network, a process that can be understood as a conversation (session) of a network. Each end of the network conversation becomes the endpoint of the network communication, the smallest unit of the network communication entity. When programming network communication using the socket interface, the socket is an abstract representation of the endpoint during network communication. Most network traffic employs a client/server model, and the client/server model distinguishes between the endpoints of the communication according to the working nature of the endpoint. For example, the client/server model will start the endpoint of a network service request as a client process or client program, and the endpoint that responds to a customer request is either a server process or a server program.
For network communication, the program needs a socket at each end of the network's conversation, that is, the socket of the client and the socket on the server side. The connection between two sockets can be either connection-oriented or connectionless. Although from the current perspective, socket programming is no longer limited to UNIX systems, but the socket interface in the network communication is still using the UNIX system I/O concept, the Socket interface model is still used Open-read-write-close mode. Socket programming consists of several main steps:
1, the establishment of socket;
2, configuration socket;
3, through the socket to send data;
4, through the socket to receive data;
5, close the socket.
Three, CSocket programming model
For beginners of network communication programming, it is difficult and complicated to use the above socket model directly. Visualc++ 's MFC (Basic Class Library) provides a few encapsulated objects that are relatively simple and easy to program. The tester program uses MFC's several socket-related classes to implement.
The process of establishing communication between the server socket and the client socket is described in the following article.
1, create the CSocket object.
2. Use this object to produce a socket handle.
3, if the socket is a customer, call CAsyncSocket::Connect, connect the local socket and server socket;
If the socket is a server, call CAsyncSocket::Listen, start to listen to the access requests from the client, and if you receive a request, call casyncsocket::accept for receiving processing.
4, establish CSocketFile object, and make this object and CSocket object have certain connection;
5, establish the CArchive object, in order to realize uninstall (receive) data and storage (send) the purpose of data. Of course, the CArchive object should be associated with the previous CSocketFile object;
6, using the CArchive object in the client and server socket between the data to achieve the server socket and client socket communication between. It is noteworthy that a given CArchive object can only pass data in a single direction: either receive or send. In general, two CArchive objects may be required to achieve two-way delivery of data.
7, parsing archive, socketfile, socket objects.