First, what are the requirements for being a TCP server?
1. Having a known address (bind(): the equivalent of knowing the number of mobile customer service clearly before we can call them;
2. Let the operating system know that it is a server, not a client (listen()): equivalent to mobile customer service, their main responsibility is to passively answer user calls, rather than actively call to harass users;3. Waiting for the arrival of the connection (accept(): Mobile customer service is always waiting for a customer to answer one.
For TCP server programming process, it is somewhat similar to the process of answering a phone:
1. Find a cell phone to talk to (socket()
2. Insert a telephone card to fix a number (bind())
3. Responsibility for passive answering. Set up a ring tone to listen for incoming calls.
4. When there is an incoming call and the relationship between the two parties is confirmed, the acceptance () call is actually put through.
5. Receive the other party's complaint (recv()
6. Respond appropriately (send()
7. After the communication, both sides said goodbye and hung up.
int bind( int sockfd, const struct sockaddr *myaddr,socklen_t addrlen );
Functions:
Bind the local protocol address to sockfd so that ip and port are fixed Parameters:
sockfd: socket socket myaddr: Address structure pointer to a specific protocol addrlen: The length of the address structure Return value:
Success: Return to 0
Failure: -1
Note: bind can only bind its own address and port
Use examples:
int listen(int sockfd, int backlog);
Functions:
Change socket from active to passive operating system Set up a connection queue for the socket to record all connections to the socket. For more details, see The relationship among connect(), listen() and accept()>.
Parameters:
sockfd: socket listening socket
backlog: length of connection queue
Return value:
Success: Return to 0
Failure: Others
int accept( int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen );
Functions:
Remove an established connection from the connected queue and go to sleep waiting (blocking) if no connection is available. For more details, see< The relationship among connect(), listen() and accept()>.
Parameters:
sockfd: socket listening socket
cliaddr: Used to store client socket address structure
addrlen: Address of the length of the socket address structure
Return value:
Success: Connected socket. Note: The returned socket is a connected socket, which represents the current connection.
Failure: < 0
tcp_server code:
Operation results:
Close the connection: close()
The socket can be closed by using the close() function. Closing a socket representing a connected socket will cause the other end to receive a 0-length packet. For details, please see< TCP Waves Four Times.
Server
- Closing listening sockets (sockets after socket() and listen() will cause the server to fail to receive new connections, but will not affect the established connections;
- Closing the connected socket returned by accept() will cause the connection it represents to be closed, but will not affect the socket after the server listens (socket() and listen().
When doing client
Closing a connection means closing a connection, not something else.
If the connection between client and server is successful, usually, the client is shut down first, then the server is shut down. If the server is shut down first, the server is started immediately. The port bound by the server will not be released immediately (as shown below). It takes about a minute to release. Why? See "TCP Four Waves" . Is there a way to make the server succeed every time it starts? See Port Multiplexing.