不能发送广播包的电脑和可以发送广播报的主机对比,发现不能发送广播报的主机上都有安装虚拟机,也有虚拟网卡,将所有的虚拟网卡关闭,然后再进行测试,都正常了,无论是Win7,Win10还是Xp。
禁用VMware这两个虚拟网卡,然后就一切正常了。
udp广播服务器程序:
// BoardCast_Server.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <WinSock2.h> #include <Windows.h> #pragma comment(lib, "WS2_32.lib") int _tmain(int argc, _TCHAR* argv[]) { //启动socket api WORD wVersionRequested = MAKEWORD(2, 2); WSADATA wsaData; if( 0 != WSAStartup(wVersionRequested, &wsaData)) { printf("WSAStartup failed with error: %d ", GetLastError()); return EXIT_FAILURE; } if( 2 != HIBYTE(wsaData.wVersion) || 2 != LOBYTE(wsaData.wVersion)) { printf("Socket version not supported. "); WSACleanup(); return EXIT_FAILURE; } SOCKET sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if( INVALID_SOCKET == sockfd) { printf("socket failed with error: %d ", WSAGetLastError()); WSACleanup(); return EXIT_FAILURE; } SOCKADDR_IN addr; memset(&addr, 0, sizeof(addr)); addr.sin_addr.S_un.S_addr = INADDR_BROADCAST; addr.sin_family = AF_INET; addr.sin_port = htons(7777); BOOL bBoardcast = TRUE; if( SOCKET_ERROR == setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (const char*)&bBoardcast, sizeof(bBoardcast))) { printf("setsockopt failed with error: %d ", WSAGetLastError()); if( INVALID_SOCKET != sockfd) { closesocket(sockfd); sockfd = INVALID_SOCKET; } WSACleanup(); return EXIT_FAILURE; } printf("Server start to boardcast... "); char buf[] = "This message from DESKTOP-879IMIS!"; while(1) { if( SOCKET_ERROR == sendto(sockfd, buf, sizeof(buf), 0, (LPSOCKADDR)&addr, sizeof(addr))) { printf("sendto failed with error:%d ", WSAGetLastError()); Sleep(1000); continue; } Sleep(1000); } WSACleanup(); return 0; }
udp广播客户端程序:
// BoardCast_Client.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <WinSock2.h> #include <Windows.h> #pragma comment(lib, "WS2_32.lib") #define MAX_BUFFER 256 int _tmain(int argc, _TCHAR* argv[]) { WORD wVersionRequested = MAKEWORD(2, 2); WSADATA wsaData; if(0 != WSAStartup(wVersionRequested, &wsaData)) { printf("WSAStartup failed with error: %d/n", GetLastError()); return EXIT_FAILURE; } if(2 != HIBYTE(wsaData.wVersion) || 2 != LOBYTE(wsaData.wVersion)) { printf("Socket version not supported./n"); WSACleanup(); return EXIT_FAILURE; } SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(INVALID_SOCKET == sock) { printf("socket failed with error: %d/n", WSAGetLastError()); WSACleanup(); return EXIT_FAILURE; } SOCKADDR_IN addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.S_un.S_addr = INADDR_ANY; addr.sin_port = htons(7777); if(SOCKET_ERROR == bind(sock, (LPSOCKADDR)&addr, sizeof(addr))) { printf("bind failed with error: %d/n", WSAGetLastError()); if(INVALID_SOCKET != sock) { closesocket(sock); sock = INVALID_SOCKET; } WSACleanup(); return EXIT_FAILURE; } char szBuf[MAX_BUFFER] = {0}; SOCKADDR_IN remote; memset(&remote, 0, sizeof(remote)); int len = sizeof(remote); while(1) { if(SOCKET_ERROR == recvfrom(sock, szBuf, MAX_BUFFER-1, 0, (LPSOCKADDR)&remote, &len)) { printf("recvfrom failed with error: %d/n", WSAGetLastError()); continue; } printf("recv data: <%s : %d> %s ", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port), szBuf); } return 0; }