[Article Information]Author: petery time: Source: forum responsible editor: Ark[Document Introduction]In the TCP/IP network, the host mode for interaction between two processes is the Client/Server mode ......
I. Client/Server mode
In a TCP/IP network, the host mode in which two processes interact is the Client/Server model ). The establishment of this mode is based on the following two points: 1. Non-peer effect; 2. Communication is completely asynchronous. In the Client/Server mode, the following request methods are used:
First, the server must start and provide corresponding services according to the request: (the process is as follows)
1. Open a channel and inform the local host that it is willing to receive customer requests at a recognized address.
2. Wait for the customer's request to reach the port.
3. Receive a duplicate service request, process the request, and send a response signal.
4. Return step 2, waiting for another customer's request
5. Disable the server.
Customer:
1. Open a 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 for and receive the response; Continue to make the request ......
3. Close and terminate the communication channel after the request ends.
Ii. Basic socket
In order to better illustrate the socket programming principles, we will provide several basic sockets, which will be described in more detail in the future.
1. Create a socket-socket ()
Function: Create a new socket
Format: socket pascal far socket (int af, int type, int procotol );
Parameter: af: Communication Region
Type: socket type to be created
Procotol: specific protocol used
2. Specify the local address -- bind ()
Function: Associate the socket address with the created socket number.
Format: int pascal far bind (SOCKET s, const struct sockaddr FAR * name, int namelen );
Parameter: s: the socket Descriptor (socket number) returned by the socket () call and not connected ).
Others: no error. bind () returns 0; otherwise, SOCKET_ERROR
Address structure description:
Struct sockaddr_in
{
Short sin_family; // AF_INET
U_short sin_port; // 16-bit port number, network byte sequence
Struct in_addr sin_addr; // 32-bit IP address, in bytes
Char sin_zero [8]; // Reserved
}
3. Establish a socket connection-connect () and accept ()
Function: complete the connection together.
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 as above
4. Listener connection-listen ()
Function: it is used for connection servers and indicates that it is willing to receive connections.
Format: int pascal far listen (SOCKET s, int backlog );
5. Data Transmission-send () and recv ()
Function: send and receive data
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 );
Parameter: buf: pointer to a buffer with transmitted data.
6. multiplexing -- select ()
Function: used to detect the status of one or more sockets.
Format: int pascal far select (int nfds, fd_set FAR * readfds, fd_set FAR * writefds,
Fd_set FAR * contains TFDs, const struct timeval FAR * timeout );
Parameter: readfds: pointer to read Detection
Writefds: pointer to write detection
Invalid TFDs: pointer to the vulnerability to be detected.
Timeout: Maximum waiting time
7. Disable socket-closesocket ()
Function: Disable socket s.
Format: bool pascal far closesocket (SOCKET s );
3. Typical Process Diagram
2.1 System Call Sequence Diagram for connection-oriented sockets
2.2 sequence diagram of SOCKET call without connection protocol
2.3 connection-oriented application Flowchart