The process of data transfer:
After the connection is established, the TCP protocol provides FULL-DUPLEX communication service, but the general client/server process is initiated by the client, the server passively processes the request, and asks a reply. As a result, the server calls read () immediately after returning from accept (), reads the socket like a read pipe, and if no data arrives blocking the wait, the client invokes write () to send the request to the server, and the server receives the request from read () and processes the client's requests. During this time the client invokes read () to block waiting for a reply from the server, and the server calls write () to send the processing result back to the client, call read () again to block for the next request, and the client receives a return from read (), sends the next request, and loops through it.
If the client does not have more requests, call Close () to turn off the connection, as if the write-end pipe is closed, the server's read () returns 0, so the server knows that the client has closed the connection and also calls Close () to turn off the connection. Note that after either party calls Close (), the two transmission directions for the connection are closed and no more data can be sent. If a party calls shutdown (), the connection is half closed and can still receive data from the other side.
When learning Socketapi, be aware of how the application and TCP protocol layers interact:
* What does the TCP protocol layer do when the application invokes a socket function, such as calling Connect () to emit a SYN segment
* How the application knows the state changes in the TCP protocol layer, such as returning from a blocked socket function, indicates that the TCP protocol received some segments, such as read () Returning 0 to indicate that the fin segment was received.
The simplest TCP network program
The role of SERVICE.C is to read characters from the client, and then convert each character to uppercase and send it back to the client.
int socket (int family, inttype, int protocol);
Socket () to open a network communication port, if successful, like Open () return a file descriptor, the application can be like read and write files with Read/write on the network to send and receive data, if the socket () call error return 1. Specified as af_inet for the ipv4,family parameter. For the TCP protocol, the type parameter is specified as Sock_stream, which represents a stream-oriented transport protocol. In the case of a UDP protocol, the type parameter is specified as Sock_dgram, which represents a datagram-oriented transport protocol. An introduction to the protocol parameter Conlio, specified as 0.
int bind (int sockfd, conststruct sockaddr *myaddr, socklen_t Addrlen);
The network address and port number that the server program listens to is usually fixed, and the client program can initiate a connection to the server after learning the server program's address and port number, so the server needs to invoke bind to bind a fixed network address and port number. Bind () successfully returned 0, failure returned-1.
The role of BIND () is to bind the parameter sockfd and myaddr together so that sockfd, the file descriptor used for network traffic, listens to the address and port number described by MYADDR. As I mentioned earlier, struct sockaddr * is a generic pointer type, and MYADDR parameters can actually accept SOCKADDR structures of multiple protocols, and they vary in length, so a third argument is required addrlen the length of the specified struct.
int listen (int sockfd, intbacklog);
A typical server program can serve multiple clients at the same time, when a client initiates a connection, the server calls accept () returns and accepts the connection, and if a large number of clients initiate the connection and the server is too late to process, the accept client is in the connection waiting state, listen () Declares that the SOCKFD is in a listening state and that a maximum of backlog clients are allowed to be in the receiving State, if more connection requests are received, they are ignored. Listen () successfully returned 0, failure returned-1.
int accept (int sockfd, structsockaddr *cliaddr, socklen_t *addrlen);
After the three-party handshake is complete, the server calls accept () to accept the connection and, if the server calls accept () without a client connection request, blocks the wait until a client is connected. CLIADDR is an outgoing parameter that accept () the address and port number of the outgoing client when it returns. The Addrlen parameter is an incoming outgoing parameter (Value-result argument) that is passed the length of the buffer cliaddr provided by the caller to avoid a buffer overflow problem, which is the actual length of the client address structure (which may not be filled with the buffer area provided by the caller). If the CLIADDR parameter is passed NULL, it indicates that the client's address is not cared for.
Because the client does not need a fixed port number, you do not have to call bind (), and the client's port number is automatically allocated by the kernel. Note that the client is not not allowed to call bind (), but there is no need to call bind () to fix a port number, nor does the server have to call bind (), but if the server does not call bind (), the kernel automatically assigns listening ports to the server, and the port number is different each time Clients will have trouble connecting to the server.
int connect (int sockfd, conststruct sockaddr *servaddr, socklen_t Addrlen);
The client needs to call Connect () connection server, connect and bind parameters in the same form, the difference is that the parameters of bind is their own address, and connect parameters are the other side of the address. Connect () successfully returned 0, error return-1.
SOCKADDR Data structure
Note : More wonderful tutorials Please pay attention to the triple computer tutorial section, triple Computer office group: 189034526 welcome you to join