计算从客户端发送到服务器的数据包的数量? [英] Count the number of packets sent to a server from a client?

查看:434
本文介绍了计算从客户端发送到服务器的数据包的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我几乎完成了涉及Win32编程和套接字的任务,但是我必须生成并分析有关传输的一些统计信息.我唯一麻烦的是如何计算从客户端发送到服务器的数据包的数量.

So I'm almost done an assignment involving Win32 programming and sockets, but I have to generate and analyze some statistics about the transfers. The only part I'm having trouble with is how to figure out the number of packets that were sent to the server from the client.

发送的数据可以是可变长度的,所以我不能仅将接收到的总字节除以#define的值.

The data sent can be variable-length, so I can't just divide the total bytes received by a #define'd value.

我们必须使用异步调用来完成所有操作,因此我一直在尝试为服务器套接字获取的每个FD_READ消息都增加一个计数器.但是,由于我必须能够接受可能较大的文件大小,因此必须使用大约64k的缓冲区大小来调用recv/recvfrom.如果我发送一个小数据包(a-z),则没有问题.但是,如果我发送一个1024个字符的字符串10x,则服务器报告接收到2或3个数据包,但是就发送/接收的字节而言,数据丢失率为0%.

We have to use asynchronous calls to do everything, so I've been trying to increment a counter with every FD_READ message I get for the server's socket. However, because I have to be able to accept a potentially large file size, I have to call recv/recvfrom with a buffer size around 64k. If I send a small packet (a-z), there are no problems. But if I send a string of 1024 characters 10x, the server reports 2 or 3 packets received, but 0% data loss in terms of bytes sent/received.

有人知道如何获取数据包数量吗?

Any idea how to get the number of packets?

先谢谢您了:)

推荐答案

这实际上可以归结为您所说的数据包".

This really boils down to what you mean by 'packet.'

您可能已经知道,当在线路上发送TCP/UDP消息时,将使用相应的TCP/UDP头对发送的数据进行包装"或添加前缀.然后将其包装"在IP标头中,然后将其包装"在以太网帧中.如果使用Wireshark之​​类的嗅探包,则可以看到此突破.

As you are probably aware, when a TCP/UDP message is sent on the wire, the data being sent is 'wrapped,' or prepended, with a corresponding TCP/UDP header. This is then 'wrapped' in an IP header, which is in turn 'wrapped' in an Ethernet frame. You can see this breakout if you use a sniffing package like Wireshark.

重点是这一点.当我听到数据包"一词时,我想到的是IP级别的数据. IP数据确实是在网络上打包的,因此在谈论IP时,数据包计数是有意义的.但是,如果您使用常规套接字发送和接收数据,则会剥夺IP标头以及TCP/UDP标头,即,您不会从套接字获取此信息.没有这些信息,就无法确定已传输的数据包"(同样是我在考虑IP)的数量.

The point is this. When I hear the term 'packet,' I think of data at the IP level. IP data is truly packetized on the wire, so packet counts make sense when talking about IP. However, if you're using regular sockets to send and receive your data, the IP headers, as well as the TCP/UDP headers, are stripped off, i.e., you don't get this information from the socket. And without that information, it is impossible to determine the number of 'packets' (again, I'm thinking IP) that were transmitted.

您可以通过添加带有长度和计数器的自己的标题来执行其他建议.这些信息将帮助您准确地确定接收缓冲区的大小,但不会帮助您确定数据包的数量(再次是IP ...),尤其是在使用TCP的情况下.

You could do what others are suggesting by adding your own header with a length and a counter. This information will help you accurately size your receive buffers, but it won't help you determine the number of packets (again, IP...), especially if you're doing TCP.

如果您想使用Winsock套接字准确地确定数据包的数量,我建议按照建议此处.该套接字将收集本地NIC看到的所有IP通信.使用IP和TCP/UDP标头可根据客户端和服务器套接字(即IP地址和端口号)过滤数据.这将准确显示实际使用了多少IP数据包来传输数据.

If you want to accurately determine the number of packets using Winsock sockets, I would suggest creating a 'raw' socket as suggested here. This socket will collect all IP traffic seen by your local NIC. Use the IP and TCP/UDP headers to filter the data based on your client and server sockets, i.e., IP addresses and port numbers. This will give an accurate picture of how many IP packets were actually used to transmit your data.

这篇关于计算从客户端发送到服务器的数据包的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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