Windows Socket Network Programming (2)

xiaoxiao2021-03-06  42

source:

http://www.vckbase.com/document/viewdoc/?id=484

Windows Socket network programming (two) - socket programming principles of: freezing a studio Kitty, client / server mode TCP / IP network host mode of interaction between the two processes is a client / server model Client / Server Model. The establishment of this mode is based on the following two points: 1, non-equal role; 2, communication is completely asynchronous. The client / server mode takes a proactive request mode: First, the server side must start first, and provide the appropriate service according to the request: (the process is as follows) 1. Open a communication channel and inform the local host, it is willing to be in one The customer request is received on the recognition address. 2. Wait for the customer request to reach this port. 3. Receive the repeated service request, process the request and send a response signal. 4, return to the second step, waiting for another customer request 5, turn off the server. Client: 1. Open a communication channel and connect to the specific port of the host where the server is located. 2. Send a service request message to the server, wait and receive a response; continue to ask for request ... 3. Turn off the communication channel after the request is completed. Second, the basic socket is given to better illustrate the socket programming principle, give several basic sockets, which will give a more detailed instructions in future spaces. 1. Create a socket - Socket () function: Create a new socket format before use: Socket Pascal Far Socket (int AF, int type, int procotol); parameter: AF: Communication zone type: Established Socket Type Procotol: Specified Local Address - Bind () feature: Connect the socket address with the created socket. Format: Int Pascal Far Bind (socket S, Const Struct SockAddr Far * Name, INT Namelen); Parameters: S: is a socket descriptor (socket) that is returned and not connected by Socket (). Others: No error, bind () returns 0, otherwise Socket_ERROR address structure description: struct sockaddr_in {short sin_family; // AF_INETU_SHORT SIN_PORT; / / 16-bit port number, network byte order Struct in_addr sin_addr; // 32 bit IP address, Network byte order char SIN_ZERO [8]; // Reserved} 3, establish a socket connection --connect () and accept () function: Complete connection work format: Int Pascal Far Connect (Socket S, Const Struct Sockaddr Far * Name, int namelen; Socket Pascal Far Accept (Socket S, Struct Sockaddr Far * Name, INT FAR * ADDRLEN); Parameters: Same I: Listening - Listen () Features: Used to connect the server, indicate it is willing Receive connections. Format: Int Pascal Far Listen (Socket S, Int Backlog); 5, Data Transmission - Ex () and RECV () Features: Data Send and Receive Format: Int Pascal Far Send (Socket S, Const Char Far * BUF, INT LEN, INT FLAGS; Int Pascal Far Recv (Socket S, Const Char Far * BUF, INT LEN, INT FLAGS); Parameters: BUF: Pointers to buffers with transfer data. 6, multiplexed - SELECT () function: used to detect one or more socket status.

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

New Post(0)