WINSOCK programming preliminary <1> Winsock Programming Principle

zhaozj2021-02-11  194

This course is primarily to speak in Windows, the TCP / IP programming interface, Winsock, version 1.1. High versions of Winsock actually differ much from version 1.1, mainly for extensions, such as overrunning TCP / IP protocols directly with Socket to implement other communication protocols such as IPX, NetBIOS.

This describes the Winsock1.1, which is convenient in this rest of this article, is WinSock1.1.

The point-to-peer or broadcast communication program can be implemented by Winsock. The difference between the two is not large, and the code used in the program process is almost the same, and the different places are different from the target address selection. In this course, the example is in the form of a point-to-point, and the point-to-point communication communication through Winsock is constructed, and two points of the communication process are named Server and Client, respectively.

In order to clearly illustrate the structural principle of Winsock, the following is a comparison object with the ordinary telephone service of the Telecommunications Bureau:

1. The Telecommunications Bureau provides the client of the telephone service similar to the server, and the ordinary telephone user is similar to the client.

2. First, the Telecommunications Bureau must establish a telephone switchboard. This will make the Socket () function by calling the socket () function if the masters must establish a socket (socket) in the Server side.

3. The Telecommunications Bureau must assign a number to the telephone switch to make the user to find the number to get the phone service, and the user who is connected to the telecommunications office must know the number of the header. Similarly, the moderator also specifies a port (port) in the Server side, and the client to connect to the server must know the port. This step is implemented by calling the bind () function.

4, then the Telecommunications Bureau must turn the switchboard and enable the switchboard to monitor the user's dial, if the number of users provided by the Telecom Bureau, you will find that the TME is always busy, usually the phone will cause the Trunker The corresponding telephone number is connected to several processing centers responsible for exchange, and when a processing center is busy with the current user, the new to the user can automatically go to the processing center to get the service. The Server end of the same edition is also set to a listening state. This is the universal listen () function implemented. The second parameter of listen () is waiting for the number of queues, just like you can specify the telecommunications bureau. Establish several processing centers responsible for exchange.

5. Users know that they can make a call request to obtain the service after the header number of the telecommunications office. In the world of Winsock As the Client side, you have to build a socket with the socket () function, then adjust the connect () function to connect. Of course, like the phone, if the number of queues is full, the connection will not succeed when the server is not in line with the server or if the Server does not provide this service.

5, the telephone director of the telecommunications office accepted the phone that the user was dialed and responsible for turning on the user's line, and the total plane itself returned to the waiting state. SERVER is also the same, calling the accept () function to enter the monitoring process, the code of the Server end is suspended in the middle, once the Server ends the application, the system will establish a new socket to serve this connection, and the original set The lanter is returned to the status of the listening waiting.

6, when you are hung, you can hang on the phone, and you will be offline. The same is true for closure between Client and Server; this closed offline action, can be attached to the Client or Server side to close. Some phone query systems are not the same? Close the function of the socket

CloseSocket ()

From the above situation, you can see the process of establishing a socket on the server side and entering the actual monitoring steps: socket () -> bind () -> listen () -> accept ()

So after Accept () is over, the masters say that the server will generate a new socket, and the Server will continue to enter the Accept () state. How do the masters use this new socket to perform with a client? Did communication, this uses the RECV () function, and the client end is sent to the server through the send () function. The client is also a similar process in the client, and the process of calling Winsock is as follows:

Socket () -> Connect () -> Send ()

First create a socket, then use the connect () function to connect to the SOCKET of the Server, and then call Send Send Information.

转载请注明原文地址:https://www.9cbs.com/read-3776.html

New Post(0)