使用UDP套接字接收数据
我正在写一个应用程序,但我不明白一点。我正在尝试从特定客户端接收数据。在 TCP 套接字中,accept 返回一个 fd
编号,以便您可以通过该 fd
编号与特定客户端进行通信。
在 recvfrom
中,任何内容都不能指定从该客户端接收数据。它接收来自每个发送者的数据。我正在尝试解决这个问题,但我不知道该怎么做。
第二个问题是,打开一个套接字后,我打开另一个套接字。我将第二个套接字号发送给客户端,客户端在第二个套接字上发送第二个数据。在服务器端,服务器侦听第一个套接字(而不是第二个套接字)。但是,尽管客户端从第二个套接字发送数据并且服务器侦听第一个套接字,但服务器仍获取数据。我将两个套接字打印为整数,发现两个不同。服务器如何读取第一个套接字上第二个套接字上发送的数据?
是的,我的问题有点复杂。基本上我说:
recvfrom(sock, &client, sizeof(Client), MSG_WAITALL, (struct sockaddr *)&clientAddr2, &size);
如何告诉特定客户端(例如客户端 2)收听上面的行? (在TCP套接字中,它是通过fd
号完成的,但在UDP套接字中是如何实现的?)。
I am writing an application and I don't understand a point. I am trying to receive data from a specific client. In a TCP socket, accept returns to you an fd
number, so you can communicate over this fd
number with a specific client.
In recvfrom
, anything cannot specify that receive data from that client. It receives data from everyone who sent. I am trying to solve this, but I am not sure how to do this.
The second problem is that, after opening a socket, I open other socket. I send second socket number to client, and client sends second data on this second socket. In server side, the server listens to the first socket (not the second socket). But although client sends data from the second socket and the server listens to the first socket, server takes data. I print two socket as integer and see that two are different. How can the server read data that is sent on second socket, on the first socket?
Yeah, my question is a bit complicated. Basically I say:
recvfrom(sock, &client, sizeof(Client), MSG_WAITALL, (struct sockaddr *)&clientAddr2, &size);
How can tell a specific client (say client 2) to listen in the line above? (in TCP sockets, it is done by fd
number, but how it is in UDP socket?).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Accept 不与 udp 一起使用。你调用socket,你调用bind来建立端口,你调用sendto和recvfrom。
恐怕我不能很好地理解你的描述,但我希望这会有所帮助。
TCP 和 UDP 是不同且不兼容的协议。 UDP 客户端和服务器相互通信,TCP 客户端和服务器相互通信。假设,您可以使用 RAW 套接字与另一端的 TCP 通信,但您必须拥有完整的 TCP 协议实现。
UDP 没有连接。如果您有不同客户端的概念,则必须使用放入数据包内的数据对它们进行分类。
Accept isn't used with udp. You call socket, you call bind to establish the port, you call sendto and recvfrom.
I'm afraid that I can't follow your description very well, but I hope this helps.
TCP and UDP are distinct and not compatible protocols. UDP clients and servers talk to each other, TCP clients and servers talk to each other. Hypothetically, you could use a RAW socket to talk to TCP on the other side, but you'd have to have a complete TCP protocol implementation.
UDP has no connections. If you have a concept of different clients, you have to sort them out with data you put inside the packets.
如果我明白您在寻找什么,您将需要将服务器上的每个套接字绑定到不同的端口号。然后客户端需要发送到所需的端口。
然而,可能没有必要这样做。服务器可以检查通过recvfrom()调用提供的地址,并将其与适当的客户端相关联,并根据发送它的客户端来处理它(例如,通过工作线程)。
If I understand what you are looking for, you would need to bind each socket on the server to a different port number. Then the client would need to send to the desired port.
However, it may not be necessary to do that. The server could examine the address that is supplied via the recvfrom() call and associate it with the appropriate client and process it (e.g., via a worker thread) according to which client sent it.
我不认为你可以使用UDP与TCP进行通信。 UDP 不是 TCP 的超集。它们是同一层的两个不同协议。欲了解更多信息尝试此页面< /a>
当您向计算机中的同一端口打开两个套接字时,不能保证这两个套接字都会收到数据。事实上,根据我的经验,只有第一个套接字可以。
I don't think you can use UDP to communicate with TCP. UDP is not a super set of TCP. They are two different protocols of the same layer. For more info try this page
When you open two sockets to the same port in a computer, you are not guaranteed that both sockets will receive the data. In fact, in my experience only the first socket will.