主题:[讨论]C++创建的UDP无法接收数据
各位大侠,您们好,我在用vs2008开发C++的MFC智能设备应用程序中遇到问题:客户端UDP无法接收到服务器发来的信息。客户端的代码如下:(服务器代码没有错误!!!)
WSADATA wsd;
SOCKET s;
SOCKADDR_IN servAddr;
char buf[64];
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
printf("WSAStartup failed!\n");
}
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == INVALID_SOCKET)
{
printf("socket() failed; %d\n", WSAGetLastError());
WSACleanup();
}
ZeroMemory(buf, 64);
strcpy(buf, "MyUDP");
servAddr.sin_family =AF_INET;
servAddr.sin_addr.s_addr = inet_addr("115.148.183.230");
servAddr.sin_port = htons((short)20000);
int nServAddlen = sizeof(servAddr);
char c[10240];
int k=0,nn=0;
while (1)
{
ZeroMemory(c,10240);
k=recvfrom(s, c, 10240, 0, (SOCKADDR*)&servAddr,&nServAddlen);
AfxMessageBox(k);
}
closesocket(s);
WSACleanup();
请各位大虾速速回帖!!!
WSADATA wsd;
SOCKET s;
SOCKADDR_IN servAddr;
char buf[64];
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
printf("WSAStartup failed!\n");
}
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == INVALID_SOCKET)
{
printf("socket() failed; %d\n", WSAGetLastError());
WSACleanup();
}
ZeroMemory(buf, 64);
strcpy(buf, "MyUDP");
servAddr.sin_family =AF_INET;
servAddr.sin_addr.s_addr = inet_addr("115.148.183.230");
servAddr.sin_port = htons((short)20000);
int nServAddlen = sizeof(servAddr);
char c[10240];
int k=0,nn=0;
while (1)
{
ZeroMemory(c,10240);
k=recvfrom(s, c, 10240, 0, (SOCKADDR*)&servAddr,&nServAddlen);
AfxMessageBox(k);
}
closesocket(s);
WSACleanup();
请各位大虾速速回帖!!!