双向UDP客户端和服务器?

发布于 2024-12-10 21:00:07 字数 957 浏览 0 评论 0原文

我正在尝试使用 UDP 实现基本的文件传输协议。 我使用 Beej 的指南作为参考,我将发布的大部分代码都来自那里。

到目前为止,我的程序让“发送者”将其想要的文件名发送给“接收者”。

从那里,接收器检查文件是否存在,如果存在,则计算出文件的大小。

现在这是我遇到问题的地方。我需要接收者将文件的大小发送给讲话者。您可以在我的代码(下面的链接)中看到我是如何实现它的。然而,说话者只是挂起,就像仍在等待发送某些内容一样。

这让我认为接收方需要一些额外的代码来允许它与发送方进行通信,而不仅仅是从中接收数据(我习惯了 TCP,所以请原谅我缺乏知识)。

有人可以告诉我缺少什么代码,或者我是否使用了错误的函数吗?遵循 Beej 的指南很困难,而且他没有提供双向通信的示例。

谢谢,如果您需要更多信息,请告诉我。


听众:http://pastebin.com/UL1xjDnP

发言者:http://pastebin.com/B2zrXPgZ


编辑:解决了! 感谢 cnicutar,

我在这段代码中向服务器寻址,而我应该向客户端寻址

if ((numbytes = sendto(sockfd,buffer,strlen(buffer), 0, p->ai_addr, p->ai_addrlen)) == -1)

应该更改为

if ((numbytes = sendto(sockfd,buffer,strlen(buffer), 0, (struct sockaddr *)&their_addr, &addr_len)) == -1)

I'm trying to implement a basic file transfer protocol, using UDP.
I'm using Beej's Guide as a reference, and much of the code I will post is from there.

My program so far has the 'talker' send the name of the file it wants to the 'receiver'.

From there, the reciever checks if the file exists, and if it does, it figures out the size of the file.

Now here is where I run into problems. I need the receiver to send the size of the file to the talker. You can see in my code (links below) how I implemented it. However, the talker just hangs, like it's still waiting for something to be sent.

This makes me think the reciever needs some additional code to allow it to communicate back to the talker, and not just recieve data from it (I'm used to TCP, so excuse my lack of knowledge).

Could someone tell me what code I am missing, or if I am using the functions wrong? It's difficult to follow Beej's guide, and he does not provide an example of a two-way communication.

Thanks, and let me know if you need any more info.


Listener: http://pastebin.com/UL1xjDnP

Talker: http://pastebin.com/B2zrXPgZ


EDIT: Solved!
Thanks to cnicutar,

I was addressing the server in this code, when i should have addressed the client

if ((numbytes = sendto(sockfd,buffer,strlen(buffer), 0,
p->ai_addr, p->ai_addrlen)) == -1)

which should be changed to

if ((numbytes = sendto(sockfd,buffer,strlen(buffer), 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见你 2024-12-17 21:00:07

您没有发送给正确的对等方。您将从 getaddrinfo 获取 p,然后发送给它。所以你正在向自己发送消息。

for(p = servinfo; p != NULL; p = p->ai_next)
/* .... */


numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
    (struct sockaddr *)&their_addr, &addr_len)) == -1)

/* .... */
if ((numbytes = sendto(sockfd,buffer,strlen(buffer), 0,
    p->ai_addr, p->ai_addrlen)) == -1)

您可能想将其发送给“他们”:

if ((numbytes = sendto(sockfd,buffer,strlen(buffer), 0,
    (struct sockaddr *)&their_addr, &addr_len)) == -1)

You're not sending to the right peer. You are obtaining p from getaddrinfo and then sending to it. So you are sending the message to yourself.

for(p = servinfo; p != NULL; p = p->ai_next)
/* .... */


numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
    (struct sockaddr *)&their_addr, &addr_len)) == -1)

/* .... */
if ((numbytes = sendto(sockfd,buffer,strlen(buffer), 0,
    p->ai_addr, p->ai_addrlen)) == -1)

You likely want to send it to "them":

if ((numbytes = sendto(sockfd,buffer,strlen(buffer), 0,
    (struct sockaddr *)&their_addr, &addr_len)) == -1)
雪化雨蝶 2024-12-17 21:00:07

最简单的解决方案是让您的“发送者”监听用于向服务器发送数据包的同一端口/IP。服务器可以从数据包标头中提取源 IP/端口,并将其回复发送到那里。如果您需要使用不同的端口/IP 进行回复,那么您的“发件人”必须在发送的内容中包含该信息。例如“这是您的信息,请回复至 xxxx:yyy”

Simplest solution is to have your "sender" listen on the same port/ip it's using to send packets to the server. The server can extract the source ip/port from the packet headers, and send its replies there. If you need to use different ports/ips for the replies, then your 'sender' would have to include that information in what it sends over. e.g. "here's your information, send replies to x.x.x.x:yyy"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文