(lwip)套接字发送()返回“连接”

发布于 2025-02-11 16:44:00 字数 498 浏览 2 评论 0原文

我正在维护使用LWIP的嵌入式系统。相关代码如下(编辑):

iobSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
connectRC = connect(socket, &serverAddress, sizeof(struct sockaddr));
FD_SET(socket, &fdset);
selectRC = select((socket) + 1, NULL, &fdset, NULL, &tv);
sendRC = send(iobSocket, dout, strlen(dout), 0);

这似乎有效。但是,当我删除select()调用时,我在send()上会收到一个错误,该错误将ERRNO设置为119或已在进行中的连接。此错误代码未记录在Send()MAN页面中。

有人可以告诉我为什么在这里甚至需要select()命令,为什么如果没有它,我可能会遇到无证件错误?

谢谢。

I'm maintaining an embedded system that uses LwIP. The relevant code is as follows (edited):

iobSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
connectRC = connect(socket, &serverAddress, sizeof(struct sockaddr));
FD_SET(socket, &fdset);
selectRC = select((socket) + 1, NULL, &fdset, NULL, &tv);
sendRC = send(iobSocket, dout, strlen(dout), 0);

This seems to work. When I remove the select() call, however, I get an error on the send() which sets errno to 119 or Connection already in progress. This error code isn't documented in the send() man pages.

Can someone tell me why the select() command is even necessary here, and why I might be getting an undocumented error without it?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒路情人 2025-02-18 16:44:00

错误代码119是einprogress。这意味着该插座在非阻滞模式 1 中运行,并且以前send()失败。

1 :这意味着,您必须有更多的代码,因为插座通常在最初创建时处于阻止模式,并且需要明确的代码将其放置进入非阻滞模式,例如通过fcntl(f_setfl,o_nonblock);

使用select()来测试套接字的写入性,允许您的代码等待直到插座实际完成并准备好发送。

这在

返回值

如果连接或绑定成功,则返回零。错误,返回-1,errno设置为指示错误。

错误

以下是一般套接字错误。可能还有其他特定域的错误代码。

...

einprogress
插座是非封锁,无法立即完成连接。 (unix域插座而不是Eagain失败。)可以select(2)poll(2)通过选择写作套接字来完成。<<<<<<<<<<<<<<<<< /strong>在之后选择(2)指示写入性,使用getsockopt(2)读取so_error at Level in sol_socket确定connect()成功完成so_error为零)还是未能成功(so_error是之一这里列出的通常错误代码,解释了故障的原因)。


Error code 119 is EINPROGRESS. It means the socket is operating in non-blocking mode 1, and the previous connect() operation hasn't finished yet, which is why the send() fails.

1: Which means, there must be more code that you are not showing, as sockets are normally in blocking mode when they are initially created, and require explicit code to put them into non-blocking mode, such as via fcntl(F_SETFL, O_NONBLOCK);.

Using select() to test the socket for writability allows your code to wait until the socket is actually done connecting and is ready for sending.

This is explained in the man page for connect():

RETURN VALUE

If the connection or binding succeeds, zero is returned. On error, -1 is returned, and errno is set to indicate the error.

ERRORS

The following are general socket errors only. There may be other domain-specific error codes.

...

EINPROGRESS
The socket is nonblocking and the connection cannot be completed immediately. (UNIX domain sockets failed with EAGAIN instead.) It is possible to select(2) or poll(2) for completion by selecting the socket for writing. After select(2) indicates writability, use getsockopt(2) to read the SO_ERROR option at level SOL_SOCKET to determine whether connect() completed successfully (SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the usual error codes listed here, explaining the reason for the failure).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文