1. Host mode of the interaction between the two processes between the client / server mode in the TCP / IP network is the client / server mode (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 an active request mode in the operation:
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, which is willing to receive a customer request on a recognized 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: Specific protocol used
2. Specify the local address --bind () function: 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, INT Namelen); Socket Pascal Far Accept (Socket S , Struct SockAddr Far * Name, Int Far * Addrlen; Parameters:
4, listening connection --Listen () function: Used to facilitate the connection server, indicating that it is willing to receive connections. Format: Int Pascal Far Listen (Socket S, INT Backlog);
5, Data Transmission - Series () and RECV () Features: Data Send and Receiving 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: Pointer to the buffer with transfer data. 6, multiplexed - SELECT () function: used to detect one or more socket status. Format: Int Pascal Far Select (int NFDS, FD_SET FAR * READFDS, FD_SET FAR * WRITEFDS, FD_SET FAR * EXCEPTFDS, CONST STRUCT TIMEVAL FAR * TIMEOUT "; Parameters: Readfds: Pointing to Make Read Detection Writefds: Point to Writing Test pointer ExcePTFDS: Pointer Timeout: Maximum Waiting Time 7, Close Solder Skin --closeSocket () Function: Close Socket S Format: Bool Pascal Far CloseSocket (SOCKET S); Typical Process Figure 2.1 System calling timing diagram 2.2 Socket call timing diagram without connection protocol
2.3 Connected application flowchart