UDP套接字编程 [英] UDP Socket Programming

查看:132
本文介绍了UDP套接字编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用通过端口50001接收数据的UDP服务器。我使用wireshark来监控数据。我在UDP服务器缓冲区中收到的数据和wireshark中显示的数据不完全匹配。但是有些地方它完全匹配......这可能是什么原因?这是UDP服务器的接收器代码



I am using UDP Server which receives data through port 50001. I used wireshark to monitor the data. Here the data what i receive in UDP server buffer and what data is showing in wireshark is not matching fully . but some places it is matching exactly ...What could be the reason for this? here is the receiver code for UDP server

char mesg[50];


n=recvfrom(sockfd,mesg,50,0,(struct sockaddr*)&cliaddr,&len);
fwrite(mesg,1,50,fp);







来自客户端的任何值应该在3000到-3000的范围内,但在某些地方它会达到30000到-30000。在wireshark中,我绘制了它正好在3000到-3000范围内的值。但如果我绘制接收缓冲区,它给出的范围是3000到-3000,但更多的值在30000到-30000范围内




the values whatever is coming from client should be in the range of 3000 to -3000 but in some places it is going to 30000 to -30000. In wireshark i plotted that values it is coming exactly in 3000 to -3000 range. but if i plot the received buffer it is giving in the range of 3000 to -3000 but also more values are in the range of 30000 to -30000

推荐答案

你的 fwrite 调用错误,因为假设收到的消息中有50个字符。您应该使用从 recvfrom 的调用返回的值,因此:

Your fwrite call is wrong as it is assuming there are 50 characters in the received message. You should be using the value returned from the call to recvfrom thus:
n = recvfrom(sockfd, mesg, 50, 0, (struct sockaddr*)&cliaddr, &len);
fwrite(mesg, 1, n, fp);


这篇关于UDP套接字编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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