Network programming ----- 6, I / O multiplexing server concurrency

Network programming ----- 6, I / O multiplexing server concurrency


First, the experimental requirements

Server:
    Server client waits to receive a connection request Once connected client address is displayed, then receive and display the name of the client; then receive a character string from the client, the received encrypted character string (packet length of packets by individual Science number, personal number key, fill packets not 0), then the encrypted character back to the client; continue waiting to receive the customer's information, until the client closes the connection, the server will be sent to each user connection All data is stored, when the connection is terminated, the server will display the name of the customer and all corresponding data. It requires the ability to handle multiple server client requests.
Client:
    Client first establish a connection to the corresponding server; client then receives the name entered by the user, and sends it to the server; and continues to receive user input character, then the character string sent to the server, the server sends back while receiving encrypted string and displayed. After that, continue to wait for user input string to guide the user input is a quit, closes the connection and exits.


Second, the experimental environment

  • OS: kali
  • Computer languages: C
  • Compiler: gcc
  • IDE: VsCode

Three, I / O multiplexing function related to

  • ** FD_ZERO (fd_set * fdset); the specified file descriptor sets emptied before the set of file descriptor set, it must be initialized, if not empty, because the system assigns the memory space is generally not as clear processing, so the result is unknown. **
  • ** FD_SET (fd_set * fdset); add a new file descriptor for the file descriptor in the set. **
  • ** FD_CLR (fd_set * fdset); delete a file descriptor for the file descriptor in the set. **
  • ** FD_ISSET (int fd, fd_set * fdset); for testing whether a specified file descriptors in the set. **
  • select int (int maxFd, the fd_set rdset, the fd_set wrset, the fd_set exset, struct timeval timeout); for the non-blocking, inform you when a socket or a socket with a set of signals, the system provides to achieve select function multiplexed input / output model.

Four, I / O multiplexing implementation process

  1, define set of descriptors, use FD_ZERO () function initializes the set of descriptors, then added listening socket descriptor descriptor set.
  2, using a polling cycle never true.
  3, in each cycle, the first call needs to listen to select the function to inform the kernel set of descriptors, then FD_ISSET () function determines whether the listening socket is ready, when ready, call accept function gets connected socket, and the socket into a connected set of descriptors, and update the value of the number of sockets are connected and call the next cycle of the first parameter of the select function. Next in a for loop (cycle parameters as the number of socket connector, each corresponding to a socket connected to all check whether ready) function is then determined FD_ISSET connected socket is ready, if ready, the send and receive data, if the data transceiver, return data size is 0, then closes the socket connection has been used to clean out FD_CLR descriptor in the descriptor set. After completion of the above, to enter the next cycle, then the polling.


Fifth, experiment code

PS: Because MARKDOWN code support sucks, you need to complete code, please download or view your mailbox or leave a private issue also at the bottom line on github link below the author! ! ! !

Code links: https://github.com/windy-purple/IO


Sixth, run shot

Run shot

Guess you like

Origin www.cnblogs.com/aWxvdmVseXc0/p/11782398.html