Interview Soket programming related (i) (How to determine the connection state disconnect, Linux:c/socket multiplexing Select (), blocking __HTML5

Source: Internet
Author: User
Tags readable

1, blocking mode and non-blocking mode in the return value of the recv what the meaning of each representative. There is no difference. (As far as I am aware of the blocking and non-blocking recv return values are not differentiated, are <0: error, = 0: Connection closed, >0 received data size, Special: return value <0 and (errno = = Eintr | | errno = = Ewouldblock | | errno = = Eagain) In the case of the connection is considered normal, continue to receive. In blocking mode, recv is blocked from receiving data, and if no data is returned in non-blocking mode, it will not block read and therefore need to be read in a circular way.

2. The return value of the write in blocking mode and non-blocking mode means what each represents. There is no difference. (As far as I know the blocking and non-blocking write return values are not differentiated, all are <0: error, = 0: Connection shutdown, >0 send data size, Special: return value <0 and (errno = = Eintr | | | errno = = Ewouldblock | | errno = = Eagain) In the case of the connection is considered normal, continue to send. In blocking mode, write blocks the sending of data, and if the data is temporarily unavailable in non-blocking mode, it will return without blocking the write and therefore need to be circulated.

3, blocking mode read return value < 0 && errno!= eintr && errno!= ewouldblock && errno!= Eagain, connection exception, need to close, read Return value < 0 && (errno = = Eintr | | errno = = Ewouldblock | | errno = = eagain) indicates no data and needs to continue receiving, if the return value is greater than 0 to transfer to the data.
The read return value < 0 in non-blocking mode indicates no data, = 0 indicates a disconnected,> 0 indicates that the data was received.
The return value in these 2 modes is not so understood, and there is no detailed understanding or accurate explanation.

4, blocking mode and non-blocking mode is send return value < 0 && (errno = = Eintr | | errno = = Ewouldblock | | errno = = eagain) indicates a temporary send failure, need to retry, if send returned Value <= 0, && errno!= eintr && errno!= ewouldblock && errno!= Eagain, connection exception, need to close if Send return value > 0 indicates that the data was sent. Whether the return value of send is so understood, whether the send return value in blocking mode and in non-blocking mode is =0, or that the mode indicates that it is temporarily unavailable, and needs to be resend.

5, a lot of people say that in blocking mode read will block reading, whether this. My colleagues and I tried not to block read.

6, the network to find a lot of information, said are very general, on the score is greater than 0, less than 0, equal to 0, and did not distinguish between blocking and non-blocking, but also did not distinguish a wrong number, I hope that the master can press the above question-by-article answer, the more detailed the better, usually less on the csdn, less, forgive me.
Select ():

The mechanism of Select () provides a FD_SET data structure, actually a long array of elements, each with an open

The file handle, whether it is a socket handle or another file or a named pipe or device handle, is linked by the programmer

Complete, when the Select () is invoked, the kernel modifies the contents of the Fd_set based on Io State, thereby notifying the process of executing the Select () which

The socket or file is readable, as explained in the following detail:

int Select (Nfds, Readfds, Writefds, Exceptfds, timeout)

int Nfds;

Fd_set *readfds, *writefds, *exceptfds;

struct Timeval *timeout;

Ndfs:select the number of file handles that are monitored, depending on the number of files open in the process, typically set to monitor the maximum number of files in each file

Add one.

Readfds:select a collection of readable file handles for monitoring.

Writefds:select A collection of writable file handles that are monitored.

Exceptfds:select-Monitored exception file handle collection

Timeout: Timeout End time for this select (). (see/usr/include/sys/select.h, can be accurate to one out of 10,000

Seconds. )

When a file in Readfds or Writefds is readable or writable or timed out, this select () ends with the return. Programmers use a set of departments

The macro provides the macros to determine which file is readable or writable at the end of select (). What is especially useful for socket programming is READFDS.

The relevant macro explanations are as follows:

Fd_zero (Fd_set *fdset): Clears Fdset connection to all file handles.

Fd_set (int fd, Fd_set *fdset): Establishes the connection between the file handle FD and the Fdset.

FD_CLR (int fd, Fd_set *fdset): Clears the connection between the file handle FD and Fdset.

Fd_isset (int fd, Fdset *fdset): Checks whether the fdset associated file handle FD is readable or writable, and >0 indicates that it is writable.
(For definitions of fd_set and related macros see/USR/INCLUDE/SYS/TYPES.H)

This way, your socket only reads when there is something to read, roughly as follows:
...
int sockfd;
Fd_set FdR;
struct Timeval timeout =.
...
for (;;) {
Fd_zero (&AMP;FDR);
Fd_set (SOCKFD, &AMP;FDR);
Switch (SELECT (SOCKFD + 1, &AMP;FDR, NULL, &timeout)) {
Case-1:
Error handled by U;
Case 0:
Timeout hanled by U;
Default
if (Fd_isset (SOCKFD)) {
Now u read or recv something;
/* If SOCKFD is father and server sockets, U can now accept () * *
}
}
}

So a fd_isset (SOCKFD) is quite informed sockfd readable.

As for struct timeval this function, please man select. The different Timeval settings end when the Select () performance is a surplus,

No timeout blocking and polling three characteristics. Because the timeval can be accurate to one out of 10,000 seconds, Windows SetTimer () is nothing at all. You can make a super clock with select ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.