接收文本文件的udp客户端 [英] udp client in c receiving a text file

查看:101
本文介绍了接收文本文件的udp客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个udp客户端来接收一个简单的文本文件,代码构建正常,但在运行时给我一个空白的控制台,经过一些观察我发现问题是在收到时发生的,所以我粘贴那部分代码,有人可以指导我...



I am making a udp client to receive a simple text file,the code builds fine but gives me a blank console when run,after some observation I found out that the problem is occuring at the time of recetion,so I am pasting that part of my code,could someone please guide me...

size_t data=0;
if(data=recvfrom(sd, file_buffer, sizeof(file_buffer), 0, (struct sockaddr *) &server, &server_length)<0)
{
    printf("Error receiving file.");
    exit(1);
}

if(data==sizeof(file_buffer))
{
    printf("Received Data:[%s]",file_buffer);
}





我收到数据的地方:



the point where I am receiving data:

data=recvfrom(sd, receivedData, sizeof(receivedData)-1, 0, (struct sockaddr *) &server,  &server_length);
receivedData[data]=''\0'';





我将数据写入文件的位置:



the point where I am writing data to the file:

if(fwrite(receivedData, 1, data, fp)!=data);
       {
           printf("Error writing file! \n");
           exit(1);
       }

推荐答案

起初我认为整个问题都在第一个if条件下,因为它是有点忙于调用 recvfrom(),将返回值分配给数据,然后进行小于比较。

但是看了一会儿后,我意识到你有 size_t data = 0; ,但 size_t 是无符号整数类型,因此它的值永远不会小于零。我的猜测是你实际上从 recvfrom()获得了一个负的返回值,但它永远不会因此而被捕获。



看起来您应该将其更改为 int data = 0; ,然后查看您是否登陆 exit(1);



我希望我能做得更多,但是如果没有看到更多的代码就很难,所以请尝试更改并做一些故障排除在代码中。输出 recvfrom()的返回值会有所帮助。



Soren Madsen
At first I thought the whole problem was in the first if condition, because it is somewhat busy with calling recvfrom(), assigning the return value to data and then doing a less than comparison.
But after looking at it for a while, I realized you have size_t data = 0;, but size_t is an unsigned integer type, so it will never have a value less than zero. My guess is that you actually do get a negative return value from recvfrom(), but it never gets caught because of this.

It looks like you should change it to int data = 0; and then see if you land on exit(1);.

I wish I could do more, but that is tough without seeing more of the code, so try that change out and do some troubleshooting in the code. Outputting the return value of recvfrom() when it fails would be helpful.

Soren Madsen


这篇关于接收文本文件的udp客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆