发送广播消息时的奇怪行为
Posted
技术标签:
【中文标题】发送广播消息时的奇怪行为【英文标题】:Strange behaviour while sending broadcast message 【发布时间】:2022-01-16 23:06:42 【问题描述】:我有两个程序:客户端和服务器。他们正在尝试使用广播在本地网络中找到自己。
客户端使用SERVER_PORT
(之前已知)在广播中发送简单数据包,服务器打印有关连接的信息,但是当我尝试此解决方案时,我发现了一些奇怪的行为,当我取消注释server.c
服务器打印的最后两行时(一个自定义结构)
Connection from: 0.0.0.0 on port: 0
在评论这些行后一切正常,我错过了什么吗?
server.c
int broadcast_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in broadcast_addr;
broadcast_addr.sin_addr.s_addr = INADDR_ANY;
broadcast_addr.sin_port = htons(SERVER_PORT);
broadcast_addr.sin_family = AF_INET;
if (bind(broadcast_socket, (struct sockaddr *)&broadcast_addr,
sizeof(broadcast_addr)))
perror("bind");
struct sockaddr_in recv_addr;
char buf[MAX_PACKET_SIZE];
socklen_t len;
if (recvfrom(broadcast_socket, buf, MAX_PACKET_SIZE, 0,
(struct sockaddr *)&recv_addr, &len) < 0)
perror("recvfrom");
printf("Connection from: %s on port: %d\nMessage: %s\n",
inet_ntoa(recv_addr.sin_addr), ntohs(recv_addr.sin_port), buf);
/* struct network_packet packet; */
/* struct sockaddr_in my_addr; */
client.c
int find_server(struct sockaddr_in *out)
struct sockaddr_in broadcast;
struct network_packet packet;
int yes = 1;
socklen_t len;
broadcast.sin_addr.s_addr = INADDR_ANY;
broadcast.sin_port = htons(CLIENT_PORT);
broadcast.sin_family = AF_INET;
int socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (bind(socket_fd, (struct sockaddr *)&broadcast, sizeof(broadcast)))
perror("bind");
if (get_broadcast_addr(&broadcast.sin_addr))
return -1;
printf("Target address: %s\n", inet_ntoa(broadcast.sin_addr));
broadcast.sin_port = htons(SERVER_PORT);
broadcast.sin_family = AF_INET;
setsockopt(socket_fd, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes));
char buf[10] = "test";
sendto(socket_fd, buf, strlen(buf), 0, (struct sockaddr *)&broadcast,
sizeof(broadcast));
if (recvfrom(socket_fd, &packet, sizeof(packet), 0,
(struct sockaddr *)&broadcast, &len) < 0)
perror("recvfrom");
struct sockaddr_in *sa = (struct sockaddr_in *)packet.data;
memcpy(out, sa, packet.header.packet_length);
return 0;
struct network_packet_header
enum network_packet_type type;
int packet_length;
;
struct network_packet
struct network_packet_header header;
unsigned char data[MAX_DATA_LENGTH];
;
【问题讨论】:
我可以看到您没有将addrlen
(在您的情况下为len
)初始化为地址结构的大小。您需要在调用 recvfrom
之前这样做(addrlen
既是该函数的输入又是该函数的输出)。在您的客户中也是如此。
是的,这就是答案,非常感谢@Hasturkun
【参考方案1】:
您必须将作为recvfrom
的addrlen
传递的变量初始化为地址结构的大小。
【讨论】:
以上是关于发送广播消息时的奇怪行为的主要内容,如果未能解决你的问题,请参考以下文章
PDOStatement :: execute():SQLSTATE [HY093],批量插入时的奇怪行为