【嵌入式-网络编程】vmware中使用UDP广播失败问题

发布时间:2024年01月09日

问题描述:
自己在vmware中搭建了2台虚拟机,虚拟机A向虚拟机A和虚拟机B发送广播信息,接收端在虚拟机A和虚拟机B,这个时候,由于没配置sin.sin_addr.s_addr = htonl(INADDR_ANY);,而是配置的inet_pton(AF_INET, SERV_IP, &sin.sin_addr.s_addr);,导致虚拟机A的广播信号发出去了,但是虚拟机B和虚拟机A完全收不到UDP广播数据。

要点:

  1. 在发送端配置
    // 设置允许广播数据
    int b_br = 1;
    setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &b_br, sizeof(int));
  2. 在接收端配置
    sin.sin_addr.s_addr = htonl(INADDR_ANY);绑定到任意接口
// receive.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>

#define SERV_PORT 5001
#define SERV_IP "192.168.32.133"

int main(int argc, char *argv[])
{
	int sockfd;
	struct sockaddr_in sin;
	struct in_addr sinp;
	char buf[BUFSIZ];

	// 1. 创建socket
	if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
	{
		perror("socket");
		exit(1);
	}

//	int b_br = 1;
//	setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &b_br, sizeof(int));

	// 2. 绑定服务器
	sin.sin_family = AF_INET;
	sin.sin_port = htons(SERV_PORT);
	// inet_pton(AF_INET, SERV_IP, &sin.sin_addr.s_addr);
	sin.sin_addr.s_addr = htonl(INADDR_ANY);
	bzero(sin.sin_zero, 8);
	if(bind(sockfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
	{
		perror("bind");
		exit(1);
	}

	// 3. 从客户端接收
	while(1)
	{
		int recvlen = -1;
		struct sockaddr_in srcaddrin;
		char srcaddr[16];
		int port = -1, addrlen;
		recvlen = recvfrom(sockfd, buf, BUFSIZ, 0, (struct sockaddr*)&srcaddrin, &addrlen);
		inet_ntop(AF_INET, &srcaddrin.sin_addr.s_addr, srcaddr, sizeof(srcaddrin));
		port = ntohs(srcaddrin.sin_port);
		if(recvlen == 0) printf("Client (%s:%d) is orderly shutdown\n", srcaddr, port);
		else if(recvlen < 0)
		{
			perror("recvfrom");
			break;
		}
		else {
			// 接收到recvlen个数据
			if(strncasecmp("quit", buf, 4) == 0) break;
			printf("Client (%s:%d) recv data len=%d, addrlen=%d from client: %s\n", srcaddr, port, recvlen, addrlen, buf);
		}
	}

	// 4. 处理业务

	// 5. 发送数据

	
	close(sockfd);
	
	return 0;
}


// sender.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>

#define SERV_PORT 5001
#define SERV_IP "192.168.32.255"

int main(int argc, char *argv[])
{
	int sockfd;
	struct sockaddr_in sin;
	struct in_addr sinp;
	char buf[BUFSIZ];

	// 1. 创建socket
	if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
	{
		perror("socket");
		exit(1);
	}

	// 设置允许广播数据
	int b_br = 1;
	setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &b_br, sizeof(int));

	// 2. 发送消息sendto
	sin.sin_family = AF_INET;
	sin.sin_port = htons(SERV_PORT);
	inet_pton(AF_INET, SERV_IP, &sin.sin_addr.s_addr);
	bzero(sin.sin_zero, 8);

	while(1)
	{
		if(fgets(buf, BUFSIZ, stdin) == NULL)
		{
			perror("fgets");
			continue;
		}
		
		sendto(sockfd, buf, BUFSIZ, 0, (struct sockaddr *)&sin, sizeof(sin));
		if(strncasecmp("quit", buf, 4) == 0) break;
	}

	close(sockfd);
	return 0;
}


文章来源:https://blog.csdn.net/qq_34291570/article/details/135468171
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。