参考链接
使用UDP发送广播报_udp广播 inaddr_broadcast-CSDN博客
UDP接收端收不到广播的消息问题排查_unity upd广播连接不上是什么情况-CSDN博客
如何禁用自己电脑的虚拟网卡-百度经验 (baidu.com)
但是wifi 会屏蔽255.255.255.255 广播地址,所以 255.255.255.255 需要借助手机 wifi 路由实现真正局域网内广播,两台电脑连上同一个手机热点后,就可以发消息了。
一般的局域网游戏需要 UDP 广播,然后获取到 IP 才能定向发送消息。
而广播 UDP 的方法和 UDP 定向发送的 方法有些出入,这里是 移植到 DEVC++ 的 UDP 局域网广播方法。
参考第二条连接,在两台电脑连上同一个手机热点后,如果 UDP 还收不到消息:
同一个交换机下 是可以收到广播的 还有就是
电脑的虚拟网卡会拦截广播操作,因为你没有指定一个地址,所以代码正确的情况下,发送不出去或者接收不到,只需要考虑以下两种情况。
1.广播端虚拟网卡没有禁用
2.接收端虚拟网卡没有禁用
PS.虚拟网卡一般是装了虚拟机才会有的。
服务端
- // 服务器端
- // Server.cpp : Defines the entry point for the console application.
- //
- #include
- #include
- #pragma comment(lib, "ws2_32.lib")
- #pragma warning(disable : 4996)
- const int MAX_BUF_LEN = 255;
-
- int main(int argc, CHAR* argv[])
- {
- WORD wVersionRequested;
- WSADATA wsaData;
- int err;
-
- // 启动socket api
- wVersionRequested = MAKEWORD( 2, 2 );
- err = WSAStartup( wVersionRequested, &wsaData );
- if ( err != 0 )
- {
- return -1;
- }
-
- if ( LOBYTE( wsaData.wVersion ) != 2 ||
- HIBYTE( wsaData.wVersion ) != 2 )
- {
- WSACleanup( );
- return -1;
- }
-
- // 创建socket
- SOCKET connect_socket;
- connect_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if(INVALID_SOCKET == connect_socket)
- {
- err = WSAGetLastError();
- printf("/""socket/"" error! error code is %d/n", err);
- return -1;
- }
-
- SOCKADDR_IN sin;
- sin.sin_family = AF_INET;
- sin.sin_port = htons(3779);
- sin.sin_addr.s_addr = INADDR_BROADCAST; // 255.255.255.255 但是会被路由器屏蔽,不过借用手机热点可以广播,只要两个电脑连同一个手机热点就能广播
-
- bool bOpt = true;
- //设置该套接字为广播类型
- setsockopt(connect_socket, SOL_SOCKET, SO_BROADCAST, (char*)&bOpt, sizeof(bOpt));
-
- int nAddrLen = sizeof(SOCKADDR);
-
- char buff[MAX_BUF_LEN] = "";
- int nLoop = 0;
- while(1)
- {
- nLoop++;
- sprintf(buff, "%8d", nLoop);
-
- // 发送数据
- int nSendSize = sendto(connect_socket, buff, strlen(buff), 0, (SOCKADDR*)&sin, nAddrLen);
- if(SOCKET_ERROR == nSendSize)
- {
- err = WSAGetLastError();
- printf("/""sendto/"" error!, error code is %d/n", err);
- return -1;
- }
- printf("Send: %s/n", buff);
- Sleep(500);
- }
-
- return 0;
- }
客户端
- // 客户端
- // Client.cpp : Defines the entry point for the console application.
- //
- #include
- #include
- #pragma comment(lib, "ws2_32.lib")
- #pragma warning(disable : 4996)
- const int MAX_BUF_LEN = 255;
-
- int main(int argc, CHAR* argv[])
- {
- WORD wVersionRequested;
- WSADATA wsaData;
- int err;
-
- // 启动socket api
- wVersionRequested = MAKEWORD( 2, 2 );
- err = WSAStartup( wVersionRequested, &wsaData );
- if ( err != 0 )
- {
- return -1;
- }
-
- if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )
- {
- WSACleanup( );
- return -1;
- }
-
- // 创建socket
- SOCKET connect_socket;
- connect_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if(INVALID_SOCKET == connect_socket)
- {
- err = WSAGetLastError();
- printf("/""socket/"" error! error code is %d/n", err);
- return -1;
- }
-
- // 用来绑定套接字
- SOCKADDR_IN sin;
- sin.sin_family = AF_INET;
- sin.sin_port = htons(3779);
- sin.sin_addr.s_addr = 0;
-
- // 用来从网络上的广播地址接收数据
- SOCKADDR_IN sin_from;
- sin_from.sin_family = AF_INET;
- sin_from.sin_port = htons(3779); // 端口和服务端一致
- sin_from.sin_addr.s_addr = INADDR_ANY; // 接收全部广播
-
- //设置该套接字为广播类型,
- bool bOpt = true;
- setsockopt(connect_socket, SOL_SOCKET, SO_BROADCAST, (char*)&bOpt, sizeof(bOpt));
-
- // 绑定套接字
- err = bind(connect_socket, (SOCKADDR*)&sin, sizeof(SOCKADDR));
- if(SOCKET_ERROR == err)
- {
- err = WSAGetLastError();
- printf("/""bind/"" error! error code is %d/n", err);
- return -1;
- }
-
- int nAddrLen = sizeof(SOCKADDR);
- char buff[MAX_BUF_LEN] = "";
-
- while(1)
- {
- // 接收数据
- int nSendSize = recvfrom(connect_socket, buff, MAX_BUF_LEN, 0, (SOCKADDR*)&sin_from, &nAddrLen);
- if(SOCKET_ERROR == nSendSize)
- {
- err = WSAGetLastError();
- printf("/""recvfrom/"" error! error code is %d/n", err);
- return -1;
- }
- buff[nSendSize] = '/0';
- printf("Recv: %s/n", buff);
- }
-
- return 0;
- }