If, as a server, it calls listen () after calling socket (),bind() to listen to the socket, if the client calls connect () to make a connection request, This request is received by the server side.
Connect (int sockfd, const struct SOCKADDR *addr, socklen_t Addrlen);
The first parameter of the Listen function is the socket descriptor to listen to, and the second parameter is the maximum number of connections that the corresponding socket can queue. The socket created by the socket () function defaults to an active type, and the Listen function changes the socket to a passive type, waiting for the client's connection request.
The first parameter of the Connect function is the client's socket descriptor, the second parameter is the server's socket address, and the third parameter is the length of the socket address. The client establishes a connection to the TCP server by calling the Connect function.
After the TCP server invokes the socket (),bind(),listen (), it listens for the specified socket address. The TCP client calls the socket (),Connect () in turn, and then wants the TCP server to send a connection request. After the TCP server hears this request, it calls the accept() function to take the receive request, so the connection is established. You can then start network I/O operations, which are similar to read/write I/O operations for normal files.
Accept (int sockfd, struct sockaddr *addr, socklen_t *addrlen);
The first parameter of the Accept function is the server's socket descriptor, the second parameter is a pointer to the struct SOCKADDR *, which returns the client's protocol address, and the third parameter is the length of the protocol address. If Accpet succeeds, then its return value is a completely new description Word generated automatically by the kernel, representing the TCP connection to the returned client.
Note: The first parameter of accept is the socket descriptor of the server, which is generated by the server calling the socket () function, which is called the listener socket descriptor, while the Accept function returns the connected socket description Word. A server typically creates only one listener descriptor, which persists throughout the lifetime of the server. The kernel creates a connected socket descriptor for each client connection accepted by the server process, and when the server has completed a service to a customer, the corresponding connected socket descriptor is closed.
Everything has only the East wind, the server and the customer has established a good connection. can call network I/O to read and write operations, that is, the implementation of the network of different processes between the communication! Network I/O operations have the following groups:
- Read ()/write ()
- Recv ()/send ()
- Readv ()/writev ()
- Recvmsg ()/sendmsg ()
- Recvfrom ()/sendto ()
I recommend using the recvmsg ()/sendmsg () function, which is the most general I/O function and can actually replace the other functions above. Their declarations are as follows:
#include <unistd.h> ssize_t Read (int fd, void *buf, size_t count); ssize_t Write (int fd, const void *buf, size_t count); #include <sys/types.h> #include <sys/socket.h> ssize_t Send (int sockfd, const void *buf, size_t len , int flags); ssize_t recv (int sockfd, void *buf, size_t len, int flags); ssize_t sendto (int sockfd, const void *buf, size_t len, int flags, const struct SOCKADDR *dest_addr, socklen_t Addrlen ); ssize_t recvfrom (int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); ssize_t sendmsg (int sockfd, const struct MSGHDR *msg, int flags); ssize_t recvmsg (int sockfd, struct MSGHDR *msg, int flags);
The read function is responsible for reading the content from FD. When read succeeds, read returns the actual number of bytes read, if the returned value is 0 to indicate that the end of the file has been read, and less than 0 indicates an error occurred. If the error is eintr, the read is caused by an interrupt, if econnrest indicates a problem with the network connection.
The Write function writes the nbytes byte content in buf to the file descriptor FD. Returns the number of bytes written when successful. Returns 1 on failure and sets the errno variable. In a network program, there are two possibilities when we write to the socket file descriptor. 1) The return value of write is greater than 0, indicating that some or all of the data is written. 2) The returned value is less than 0, and an error occurs. We are going to deal with the error type. If the error is EINTR, an interrupt error occurred while writing. If the epipe indicates a problem with the network connection (the other party has closed the connection).
Other I do not introduce these several I/O functions, see the Man document or Baidu, Google, the following example will be used to SEND/RECV.
After the server has established a connection with the client, some read and write operations are performed, and the corresponding socket descriptor is closed when the read and write operation is completed, like closing the open file by calling Fclose when the file is opened.
Close (int fd);
Close the default behavior of a TCP socket by marking the socket as closed and then immediately returning to the calling process. The descriptor can no longer be used by the calling process, that is, no longer as the first parameter of read or write.
Note: The close operation simply makes the reference count of the corresponding socket descriptor-1, which triggers the TCP client to send a terminating connection request to the server only if the reference count is 0.