2路udp广播问题,borland Turbo C++
我正在尝试在两台计算机上建立一个双向广播系统。这是我第一次使用 C++ 进行网络编程。假设我有计算机 A 和计算机 B,各声明 2 个套接字,每台计算机上有套接字 sd 和 sd1,每台计算机上有客户端、服务器声明。在计算机 A 上,计算机 A 上的套接字 sd 绑定到计算机 A 上的客户端 IP 192.168.2.100:50000。然后它从广播计算机 B 接收信息,使用:
recvfrom(sd, (char *)received_buffer, 100, 0, NULL, 0)
在计算机 B 上,套接字 sd 设置为广播,使用:
setsockopt(sd, SOL_SOCKET, SO_BROADCAST, (char *)&broadcastpermission, sizeof(struct sockaddr_in))
但 sd 不是由于广播使用而受到约束。然后,计算机 B 使用以下命令向计算机 A 广播信息:
sendto(sd, (char *)send_buffer, 100, 0, (struct sockaddr *)&server, (int)sizeof(struct sockaddr_in))
其中计算机 B 上声明的服务器的 IP 为 192.168.2.100:50000(与计算机 A 上的客户端 IP 相同,以便它广播到该 IP)。
当计算机B向计算机A广播时,上述方法工作正常。但是,当我尝试反向执行相同操作时,计算机A向计算机B广播,它不起作用。在计算机 A 上,套接字 sd1 被声明并设置为广播,使用:
setsockopt(sd1, SOL_SOCKET, SO_BROADCAST, (char *)&broadcastpermission, sizeof(struct sockaddr_in))
sd1 未绑定,计算机 A 使用以下方式向计算机 B 广播:
sendto(sd1, (char *)send_buffer, 100, 0, (struct sockaddr *)&server, (int)sizeof(struct sockaddr_in))
其中服务器被声明为 IP 192.168.2.2:40000 (它是计算机 B 的客户端 IP 地址,因此它在那里广播)。在计算机 B 上,socket sd1 被声明并绑定到客户端,并自动获取 IP,IP 为 192.168.2.2:40000。计算机 B 接收广播时使用:
recvfrom(sd, (char *)received_buffer, 100, 0, NULL, 0)
我对这个很长的故事表示歉意,但我需要尽可能清楚。谁能告诉我为什么计算机 B 无法接收计算机 A 的广播吗?
i am trying to do a 2 way broadcast system on 2 computers. Its my first time doing network programming and in c++. lets say i have computer A and computer B, 2 sockets declared each, sockets sd and sd1 on each computer and client,server declaration on each computer. on computer A, socket sd on computer A is binded to client IP of 192.168.2.100:50000 on computer A. Then it receives information from broadcasting computer B using:
recvfrom(sd, (char *)received_buffer, 100, 0, NULL, 0)
on computer B, socket sd is set for broadcast using:
setsockopt(sd, SOL_SOCKET, SO_BROADCAST, (char *)&broadcastpermission, sizeof(struct sockaddr_in))
but sd is not binded because of broadcasting usage. computer B then broadcasts info to computer A using :
sendto(sd, (char *)send_buffer, 100, 0, (struct sockaddr *)&server, (int)sizeof(struct sockaddr_in))
where the declared server on computer B is of IP 192.168.2.100:50000 (same as client's IP on computer A so that it broadcasts to that IP).
This above methods works fine when computer B broadcasts to computer A. However, when I tried to do the same in reverse, Computer A broadcasts to computer B, it does not work. on computer A, socket sd1 is declared and set for broadcast using:
setsockopt(sd1, SOL_SOCKET, SO_BROADCAST, (char *)&broadcastpermission, sizeof(struct sockaddr_in))
and sd1 is not binded and computer A broadcasts to computer B using:
sendto(sd1, (char *)send_buffer, 100, 0, (struct sockaddr *)&server, (int)sizeof(struct sockaddr_in))
where server is declared as IP 192.168.2.2:40000 (its the client IP address of computer B so that it broadcasts there). on computer B,socket sd1 is declared and binded to client and IP obtained automatically at IP 192.168.2.2:40000. Computer B receives the broadcast using:
recvfrom(sd, (char *)received_buffer, 100, 0, NULL, 0)
My apolgies for the very long story, but i need to be as clear as possible. Could anyone tell any posibility of why computer B can't receive broadcasts from computer A?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所描述的似乎是一种沟通方式有效,但另一种则无效。这通常与代码中的问题无关,而是与网络配置(最常见的是防火墙)有关。
使用嗅探器(例如 Wireshark)验证计算机 B 是否收到来自计算机 A 的广播 UDP 数据报。如果不是在这种情况下,请验证计算机 A 正在发送正确的 UDP 数据报。如果 A 正在发送但 B 没有接收,则存在网络问题(路由器、防火墙等)。如果 B 正在接收,但您的应用程序没有接收,请验证端口号是否正确。您可以在计算机 B 上使用 netstat (使用
-a -n
)查看是否在正确的端口上打开了 UDP 套接字。如果所有这些都不能让您更接近,请尝试在两台计算机上运行完全相同的程序 - 这意味着不要交换
sd
和sd1
角色。相反,在两台计算机上运行相同的程序(以及相同的二进制文件,如果可能),使用嗅探器验证数据报是否已正确接收。然后就可以看到程序之间是否可以互相接收。如果是这种情况,则程序 B 代码中可能存在一些疏忽,导致其发生故障。最后,使用
ping
验证基本的双向通信,方法是从另一台计算机 ping 每台计算机(请注意,ping
不能确认有效的双向路径 - 防火墙通常阻止传入的回显请求,但不阻止回复)。然后使用常规(非广播)UDP 套接字来验证 UDP 通信。可能允许单播,但不允许广播。It seems that what you are describing is that one way of communication works, but not the other. This is most often not related to issues in the code, but to configuration of the network, most often firewalls.
Using a sniffer (like Wireshark) verify that computer B receives the broadcast UDP datagrams from computer A. If this is not the case, verify the computer A is sending correct UDP datagrams. If A is sending but B is not receiving, there is a network issue (router, firewall, etc.). If B is receiving, but your application is not, verify that the port numbers are correct. You can use netstat (with
-a -n
) on computer B to see if you have the UDP socket open on the right port.If all this doesn't get you closer, try to run exactly the same program on both computers - meaning don't swap the
sd
andsd1
roles. Rather, run the same program (and same binary, if possible) on both computers, verifying with a sniffer that the datagrams are received correctly. Then you can see if the programs can receive each other. If this is the case, then there might be some over-sight in program B code that causes it to malfunction.Finally, use
ping
to verify basic two way communication, by pinging each computer from the other one (note thatping
doesn't confirm a valid two-way path - firewalls often block incoming echo requests, but not replies). Then use regular (non-broadcast) UDP sockets to verify UDP communication. It might be that unicast is allowed, but broadcast is not.