问题描述
我想通过 UDP 从远程设备接收数据,我正在使用 On-Time RTOS 中的 RTIP-32 库
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)
Socket 是非阻塞的,但 connect 会阻塞。连接操作 是在非阻塞套接字上完成的。这实际上不是错误。采用 选择等待连接建立。
我在这里没有做什么
问候 希德
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)