First, the network program based on TCP protocol
The following figure is a general process for client/server programs based on TCP protocol:
After the server calls the socket (), bind (), listen () completes initialization, calls the accept () blocking wait, is in the listening port state, the client calls the socket () initialization, calls connect () sends the SYN segment and blocks waiting for the server to reply, The server answers a syn-ack segment, the client receives a return from connect (), answers an ACK segment, and returns from accept () after receiving the server.
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 the socket API, 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 of the TCP protocol layer, such as returning from a blocked socket function, indicates that the TCP protocol has received some segments, and then read () returns 0 to indicate that the fin segment was received
To add, in fact, TCP has a total of 11 states, the above figure does not appear in the closing state, when both sides close the connection will appear this state, replace the FIN_WAIT2 state.
Second, basic socket function
1. Socket function
Include header file <sys/socket.h>
Function: Create a socket for communication
Prototype: int socket (int domain, int type, int protocol);
Parameters
Domain: Specifies the Communication protocol family (protocol family), Af_inet, Af_inet6, Af_unix, etc.
Type: Specifies the socket type, streaming socket sock_stream, datagram socket SOCK_DGRAM, original socket SOCK_RAW
Protocol: Protocol type, IPPROTO_TCP, etc. generally, the first two parameters determine the protocol type, set to 0.
Return value: A nonnegative integer successfully returned, similar to a file descriptor, which we refer to as a set interface descriptor, or socket. Failure return-1
2. Bind function
Include header file <sys/socket.h>
Function: Bind a local address to socket
Prototype: int bind (int sockfd, const struct SOCKADDR *addr, socklen_t Addrlen);
Parameters
Socket returned by the Sockfd:socket function
Addr: The address to bind
Addrlen: Address length
Return value: Successfully return 0, failure return-1