recvfrom()没有收到任何UDP消息

时间:2018-11-28 16:17:36

标签: c++ c sockets udp winsock

我正在尝试获取从计算机上不同应用程序发送的UDP消息。

我创建了一个套接字并将其绑定到一个地址。当我使用Packet Sender发送UDP消息时,什么也没有到达。我只是想确保禁用防火墙,但仍然没有任何反应。

我将邮件发送到127.0.0.1端口8034。

我尝试同时绑定到127.0.0.1和INADDR_ANY,两者均无效。

这是我用来监听的代码:

SOCKET SendSocket = INVALID_SOCKET;
sockaddr_in RecvAddr, ClientAddr;
WSADATA wsaData;
struct sockaddr_in serv, client;
int l = sizeof(client);
char buffer[256];

int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
    wprintf(L"WSAStartup failed with error: %d\n", iResult);
}

SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (SendSocket == INVALID_SOCKET) {
    wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
    WSACleanup();
}

ClientAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
ClientAddr.sin_family = AF_INET;
ClientAddr.sin_port = htons(8034);

bind(SendSocket, (struct sockaddr *) &ClientAddr, sizeof(ClientAddr));

bool valid = true;

while( valid )
{
    //cout << "\ngoing to recv\n";
    int rc = recvfrom(SendSocket, buffer, sizeof(buffer), 0, (struct sockaddr *)&client, &l);
    if (rc < 0)
    {
        //cout << "ERROR READING FROM SOCKET";
    }
    else
    {
        cout << "\n the message received is : " << buffer << endl;
    }

    DoPostDrawTask( valid );
}

0 个答案:

没有答案