gpt4 book ai didi

c - C 中带有套接字的非阻塞 connect() 问题

转载 作者:行者123 更新时间:2023-11-30 21:23:00 26 4
gpt4 key购买 nike

关闭。这个问题需要更多 focused .它目前不接受答案。












想改进这个问题?更新问题,使其仅关注一个问题 editing this post .

4年前关闭。




Improve this question




我目前正在研究套接字编程中的非阻塞 I/O。在本主题中,我遇到了可以将 connect() 设置为非阻塞的概念,即如果连接仍未建立,它将立即返回 EINPROGRESS 错误。同时连接建立用户也可以执行书中所述的其他一些任务,如下所示:

There are three uses for a nonblocking connect:

  1. We can overlap other processing with the three-way handshake. A connect takes one RTT to complete (Section 2.6) and this can be anywhere from a few milliseconds on a LAN to hundreds of milliseconds or a few seconds on a WAN. There might be other processing we wish to perform during this time.

  2. We can establish multiple connections at the same time using this technique. This has become popular with Web browsers, and we will show an example of this in Section 16.5.

  3. Since we wait for the connection to be established using select, we can specify a time limit for select, allowing us to shorten the timeout for the connect. Many implementations have a timeout for connect that is between 75 seconds and several minutes. There are times when an application wants a shorter timeout, and using a nonblocking connect is one way to accomplish this.



我对本书中提到的支持相同的代码有疑问。代码如下:
 1 #include     "unp.h" // a header file containing all necessary files required to run this program   
2 int
3 connect_nonb(int sockfd, const SA *saptr, socklen_t salen, int nsec)
4 {
5 int flags, n, error;
6 socklen_t len;
7 fd_set rset, wset;
8 struct timeval tval;

9 flags = Fcntl(sockfd, F_GETFL, 0);
10 Fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);

11 error = 0;
12 if ( (n = connect(sockfd, saptr, salen)) < 0)
13 if (errno != EINPROGRESS)
14 return (-1);

15 /* Do whatever we want while the connect is taking place. */

16 if (n == 0)
17 goto done; /* connect completed immediately */

18 FD_ZERO(&rset);
19 FD_SET(sockfd, &rset);
20 wset = rset;
21 tval.tv_sec = nsec;
22 tval.tv_usec = 0;

23 if ( (n = Select(sockfd + 1, &rset, &wset, NULL,
24 nsec ? &tval : NULL)) == 0) {
25 close(sockfd); /* timeout */
26 errno = ETIMEDOUT;
27 return (-1);
28 }

29 if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) {
30 len = sizeof(error);
31 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
32 return (-1); /* Solaris pending error */
33 } else
34 err_quit("select error: sockfd not set");

35 done:
36 Fcntl(sockfd, F_SETFL, flags); /* restore file status flags */

37 if (error) {
38 close(sockfd); /* just in case */
39 errno = error;
40 return (-1);
41 }
42 return (0);
43 }

上面的代码是 sockets API 支持的 connect() 实用程序的包装函数。在这里,我们将套接字设为非阻塞。

疑问 是如果连接尚未建立并且根据第 15 行,我们可以继续执行评论中提到的其他一些任务。但是不应该首先检查直接连接条件。因为没有检查那个,我们继续做任何需要时间的事情,而连接之前可能发生过。

如果我错了,请纠正我。

最佳答案

the first point above is contradictory to what is shown in the image below:



不,不是。 connect()是关于 TCP SYN、SYN-ACK 和 ACK 段的。你的图片大约是 recvfrom()和 UDP 数据报。没有矛盾,因为首先没有相互关联。这个建议很荒谬。

what is that other task?



根据您的报价,是什么促使您首先使用非阻塞连接,例如错误,另一个连接。

Also, it states that we should check for immediate connection in case client and server are on the same host machine, then according to this, shouldn't that be checked in first place before proceeding further.



是的,确实如此。

The latter case is shown in line number 16.



是的,这就是检查的地方。你的问题?

关于c - C 中带有套接字的非阻塞 connect() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49347503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com