简体   繁体   English

如何收听广播中的服务器回复?

[英]How to listen for a server reply from broadcast?

As part of a simple networking project I'm trying to connect two computers together to send a simple data packet. 作为一个简单的网络项目的一部分,我试图将两台计算机连接在一起以发送一个简单的数据包。 The client uses a broadcast to find servers, and my server successfully detects this broadcast from the client. 客户端使用广播查找服务器,我的服务器成功从客户端检测到此广播。

The server then sends a reply packet, however I am unable to get the client to listen to the packet. 服务器然后发送一个应答数据包,但是我无法让客户端收听该数据包。

The problem seems to lie with the broadcasting method, since if I use a direct server connection, aka instead of INADDR_BROADCAST I specify the IP address 127.0.0.1 or 192.168.0.x then the server connects, sends a reply, and the client receives it. 问题似乎在于广播方法,因为如果我使用直接服务器连接,也就是代替INADDR_BROADCAST,我指定IP地址127.0.0.1或192.168.0.x,则服务器连接,发送答复并客户端接收它。

The listening code in the client: 客户端中的侦听代码:

// Stop the client from waiting for packets if there are none.
fd_set checksockets;
checksockets.fd_count = 1;
checksockets.fd_array[0]=m_listenSocket;
struct timeval t;
t.tv_sec=0;
t.tv_usec=0;
int waiting = select(NULL, &checksockets, NULL, NULL, &t);

// If there is at least one packet receive it.
if(waiting>0) {
    std::cout << "Packet received.\n";
}

From this point I attempt to find the server address with the recvfrom() method. 从这一点开始,我尝试使用recvfrom()方法找到服务器地址。

I've made sure to use the broadcast flag on the client socket, right after creating it. 在创建套接字之后,我已经确保在客户端套接字上使用广播标志。 This returns no errors. 这不会返回任何错误。

int value=true;
int result = setsockopt(m_socket, SOL_SOCKET, SO_BROADCAST, (char*)&value, sizeof( value ) );

I've checked all possible WINSOCK functions that I've used and none return any errors. 我已经检查了所有可能使用的WINSOCK函数,并且没有一个返回任何错误。

I've also tried creating a second socket only for listening on the same port, but this conflicts with the server and therefore fails to open. 我还尝试创建仅用于侦听同一端口的第二个套接字,但这与服务器冲突,因此无法打开。

So essentially, what I'm trying to ask: How can I have a client listen for a reply from a broadcast? 因此,从本质上讲,我想问的是:我如何让客户收听广播的回复? - aka the server address is unknown at first, I'm attempting to create a new socket using the reply address, however I'm not getting a reply address from a broadcast, despite the server receiving the broadcast and definitely sending a reply. -aka服务器的地址最初是未知的,我试图使用回复地址创建一个新的套接字,但是尽管服务器接收到广播并肯定发送了回复,但我没有从广播中获得回复地址。

As you point out you won't receive any back when bound to INADDR_BROADCAST . 如您所指出,绑定到INADDR_BROADCAST不会收到任何INADDR_BROADCAST This is normal: when you are bound to an address you only receive packets from that address. 这是正常现象:绑定到某个地址后,您只会从该地址接收数据包。 You don't need to bind the sockets to any address at all. 您根本不需要将套接字绑定到任何地址。 This operation is usually required for connected sockets (ie TCP). 连接的套接字(即TCP)通常需要执行此操作。 If the receiving socket works I guess it is because you're doing all your tests on the same machine. 如果接收套接字有效,我想那是因为您正在同一台计算机上进行所有测试。

If you want to receive packets from a given address you can try to use connect or sendto and receivefrom . 如果要接收来自给定地址的数据包,则可以尝试使用connectsendtoreceivefrom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Qt:服务器应广播更改的值,客户端应侦听它 - Qt: Server should broadcast changing value and client should listen for it 接收来自N个客户端的响应,以响应UDP上的广播请求 - Receiving response(s) from N number of clients in reply to a broadcast request over UDP 如何设置C ++ Web应用程序服务器侦听来自tomcat / restlet服务器的http请求 - how to setup a C++ web application server listen to http request from a tomcat/restlet server 使用正则表达式从服务器回复中删除 HTML header - Remove HTML header from server reply using Regular Expressions C ++ cURL每次都是“来自服务器的空回复” - C++ cURL “empty reply from server” every time WebSockets从服务器端握手答复,草稿00 - WebSockets handshake reply from server side, draft 00 从stdin读取输入,并准备从服务器接收广播 - Read input from stdin as well as ready to receive broadcast from server 如何从OpenMP单块广播结果 - How to broadcast result from OpenMP single block 使gRPC客户端保持侦听模式,以接收来自服务器的消息 - Keep gRPC client in listen mode for message from server 如何创建监听两个不同端口的 boost::asio 服务器 - how to create boost::asio server that listen to two different ports
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM