winsock 点播,有源代码,大家帮分析下。
我自己写UDP点播,可是有个问题,有的时候收得到发送过来的数据,有的时候有收不到,大家帮忙看看问题出在哪里。 非常感谢,仔细代码如下:
SOCKET m_socket;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(VersionHigh,VersionLow);
err=WSAStartup(wVersionRequested, &wsaData);
/* startup failed */
if (err!=0)
{
WSACleanup();
return false;
}
/* version error */
if (LOBYTE(wsaData.wVersion)!= VersionLow ||
HIBYTE(wsaData.wVersion)!= VersionHigh )
{
WSACleanup();
return false;
}
m_socket=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
/* set to nonblocking mode */
u_long arg;
arg = 1;
if (IOCTLSOCKET(m_socket,FIONBIO,&arg)==SOCKET_ERROR) //非阻塞模式
{
return false;
}
else
{
return true;
}
//设置复用
int opt = 1;
unsinged int len = sizeof(opt);
if(setsockopt(m_socket, SOL_SOCKET, SOREUSEADDR, (const char*)&opt, len) == SOCKET_ERROR)
{
return false;
}
//绑定
SOCKADDR_IN addrLocal;
addrLocal.sin_family=AF_INET;
addrLocal.sin_port=htons(port);
addrLocal.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(m_socket,(SOCKADDR *)&addrLocal,sizeof(addrLocal))==SOCKET_ERROR)
{
return false;
}
//接收
SOCKADDR_IN from;
char buf[1024];
int len = sizeof(from);
int ret = 0;
ret = recvfrom(m_socket, buf, sizeof(buf), 0,(SOCKADDR *)addr, (int *)len);
if(ret == -1)
{
}
//cleanUP
WSACleanUp();
希望大家帮忙分析下到底为什么会有的时候接收不到数据呢 ?
(sniffer监测确实每次都有数据发过来)