准时 RTOS - 非阻塞 UDP 接收不起作用

问题描述

我想通过 UDP 从远程设备接收数据,我正在使用 On-Time RTOS 中的 RTIP-32 库

这是我的非 Bocking UDP 套接字的初始化代码

SOCKET UDPCreateSendSocket(DWORD ToIP,WORD Server_port_no)
{
    SOCKET s;
    struct sockaddr_in sin;
    int Result;
    struct timeval select_to;

    // allocate a socket
    s = socket(AF_INET,SOCK_DGRAM,0);
    if (s == INVALID_SOCKET)
        LogFile.updatelog("Failed to allocate socket");

    // connect to the server
    sin.sin_family = AF_INET;
    memcpy(&sin.sin_addr,&ToIP,sizeof(ToIP));
    sin.sin_port = htons(Server_port_no);

    //Enables non-blocking mode
    DWORD iMode = 1;
    Result = ioctlsocket(s,FIONBIO,&iMode);
    if (Result == SOCKET_ERROR)
        LogFile.updatelog("Enable non blocking Failed");

    //Connect
    Result = connect(s,(const struct sockaddr*)&sin,sizeof(sin));
    if (Result == SOCKET_ERROR)
        LogFile.updatelog("connect Failed");
  
    fd_set fd_write;
    fd_set fd_read;

    // setup socket list for select()
    FD_ZERO(&fd_write);
    FD_ZERO(&fd_read);

    FD_SET(s,&fd_write);
    FD_SET(s,&fd_read);

    select_to.tv_sec = 1;
    select_to.tv_usec = 0;

    // wait for connect() to complete
    Result = select(1,&fd_read,&fd_write,NULL,&select_to);
    if (Result != 1)
    {
        printf("Timeout waiting for server\n");
        closesocket(s);
        exit(1);
    }

    return s;
}

这里是调用接收的函数

int UDPReceive(SOCKET s,void * Data,int MaxLen)
{

    int l = recv(s,(char*)Data,MaxLen,MSG_PEEK);
    if (l == SOCKET_ERROR)
    {
        int error_no = xn_getlasterror();
        xn_geterror_string(error_no);
    }

    if (l == SOCKET_ERROR)
    {
        //Error("recv Failed");
        return 0;
    }
    else
        return l;
}

我收到的错误代码112,描述如下

进展 (112)

Socket 是非阻塞的,但 connect 会阻塞。连接操作 是在非阻塞套接字上完成的。这实际上不是错误。采用 选择等待连接建立。

在这里没有做什么

问候 希德

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...