windows upd广播包无法发送到局域网解决方法

Posted 新月时刻

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows upd广播包无法发送到局域网解决方法相关的知识,希望对你有一定的参考价值。

 

不能发送广播包的电脑和可以发送广播报的主机对比,发现不能发送广播报的主机上都有安装虚拟机,也有虚拟网卡,将所有的虚拟网卡关闭,然后再进行测试,都正常了,无论是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\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 sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if( INVALID_SOCKET == sockfd)
	{
		printf("socket failed with error: %d\n", 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\n", WSAGetLastError());
		if( INVALID_SOCKET != sockfd) 
		{
			closesocket(sockfd);
			sockfd = INVALID_SOCKET;
		}
		WSACleanup();
		return EXIT_FAILURE;
	}
	printf("Server start to boardcast...\n");
	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\n", 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\n", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port), szBuf);
	}
	return 0;
}

  

以上是关于windows upd广播包无法发送到局域网解决方法的主要内容,如果未能解决你的问题,请参考以下文章

VB6.0的winsock控件用UDP局域网广播数据,当数据很大时接收方报10040--数据包太大,请问怎么处理。

在 Pixel 2 和 Pixel 2 XL 接收 UDP 广播数据包

广播域

Linux 上 C 中的 UDP 发送方和 Windows 上 Qt 中的接收方不起作用? [关闭]

读<Wireshark数据包分析实战-第3版;之流量分类

ping 无法访问目标主机?