简单的Windows 10 UDP广播发送和接收不起作用

时间:2018-07-30 05:43:20

标签: c++ windows network-programming udp broadcast

我正在尝试在运行Windows 10 64 Pro(1803)的两台PC上实现一个简单的UDP广播器和侦听器。我遇到了许多c / c ++示例,但我无法在本地网络上使用它们。

我在本地网络上将接收方PC设置为192.168.0.1,在发送方PC上设置192.168.0.2,其子网掩码为255.255.255.0,并使用本地广播地址192.168.0.255。

它们在与本地主机,MS回送适配器和我可能选择的任何本地适配器一起在同一台计算机上使用时,它们可以正常工作。它们还可以与两台通过网关连接到wifi路由器的机器一起使用。他们似乎并没有使用常规的基本开关,这正是我所需要的。我可以使用Wireshark看到消息在监听器PC上传入,但是我的监听器代码(recvfrom())没有响应。如果尝试禁用Windows防火墙,但这无效。

*一件有趣的事。如果我已准备好侦听器代码,并从该侦听器PC发送广播,则侦听器代码将在while循环中对该进行响应,以广播该PC的后续广播。如果退出并重新启动,则其响应结果与以前相同。*

下面的代码是我尝试过的许多代码之一,但是基本上都是相同的少数操作。

发件人代码示例:

#include <winsock2.h>
#include <iostream>
#include <conio.h>

using namespace std;
#define MYPORT 9009    // the port users will be connecting to

int main()
{
    WSADATA wsaData;
    WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
    SOCKET sock;
    sock = socket( AF_INET, SOCK_DGRAM, 0 );
    char broadcast = '1';
    if ( setsockopt( sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast) ) < 0 )
    {
        cout<<"Error in setting Broadcast option";
        closesocket(sock);
        return 0;
    }

    struct sockaddr_in Recv_addr;
    struct sockaddr_in Sender_addr;
    int len = sizeof( struct sockaddr_in );
    char sendMSG[] ="Attention: this is a broadcast!!!";
    char recvbuff[50] = "";
    int recvbufflen = 50;
    Recv_addr.sin_family       = AF_INET;
    Recv_addr.sin_port         = htons( MYPORT );

    Recv_addr.sin_addr.s_addr = inet_addr( "192.168.0.255" );  // switch doesnt work
//    Recv_addr.sin_addr.s_addr = inet_addr( "192.168.1.255" );  //wifi router works

    cout << "sending message: " << sendMSG;
    sendto( sock, sendMSG, (int) strlen(sendMSG)+1, 0,(sockaddr*) &Recv_addr, sizeof( Recv_addr ) );

    closesocket( sock );
    WSACleanup();
}

示例接收方代码:

#include "winsock2.h"
#include <iostream>
#include <conio.h>

using namespace std;

#define MYPORT 9009    // the port users will be connecting to

int main()
{
    WSADATA wsaData;
    WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
    SOCKET sock;
    sock = socket( AF_INET, SOCK_DGRAM, 0 );
    char broadcast = '1';
//     This option is needed on the socket in order to be able to receive broadcast messages
//   If not set the receiver will not receive broadcast messages in the local network.
    if ( setsockopt( sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof( broadcast ) ) < 0 )
    {
        cout<<"Error in setting Broadcast option";
        closesocket(sock);
        return 0;
    }

    struct sockaddr_in Recv_addr;
    struct sockaddr_in Sender_addr;
    int len = sizeof( struct sockaddr_in );
    char recvbuff[50];
    int recvbufflen = 50;
    Recv_addr.sin_family       = AF_INET;
    Recv_addr.sin_port         = htons( MYPORT );
    Recv_addr.sin_addr.s_addr  = INADDR_ANY;
    if ( bind( sock, (sockaddr*) &Recv_addr, sizeof (Recv_addr) ) < 0 )
    {
        cout << "Error in BINDING" << WSAGetLastError();
        _getch();
        closesocket(sock);
        return 0;
    }

    do
    {
        cout << "\nWaiting for message...\n";
        recvfrom( sock, recvbuff, recvbufflen, 0, (sockaddr*) &Sender_addr, &len );
        cout << "Received Message is: " << recvbuff;
    } while ( 1 );

    closesocket(sock);
    WSACleanup();
}

0 个答案:

没有答案