Windows 7 在Windows 7上传输UDP数据包突发的不一致行为

Windows 7 在Windows 7上传输UDP数据包突发的不一致行为,windows-7,network-programming,udp,Windows 7,Network Programming,Udp,我有两个系统,都运行Windows7。源代码是192.168.0.87,目标代码是192.168.0.22,它们都连接到我桌上的一个小交换机上 源正在使用此程序向目标发送100个UDP数据包的突发数据包- #include <iostream> #include <vector> using namespace std; #include <winsock2.h> int main() { // It's windows, we need th

我有两个系统,都运行Windows7。源代码是192.168.0.87,目标代码是192.168.0.22,它们都连接到我桌上的一个小交换机上

源正在使用此程序向目标发送100个UDP数据包的突发数据包-

#include <iostream>
#include <vector>

using namespace std;

#include <winsock2.h>


int main()
{
    // It's windows, we need this.
    WSAData wsaData;
    int wres = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (wres != 0) { exit(1); }

    SOCKET s = socket(AF_INET, SOCK_DGRAM, 0);
    if (s < 0) { exit(1); }

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(0);

    if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { exit(3); }

    int max = 100;

    // build all the packets to send
    typedef vector<unsigned char> ByteArray;
    vector<ByteArray> v;
    v.reserve(max);
    for(int i=0;i<max;i++) {
        ByteArray bytes(150+(i%25), 'a'+(i%26));
        v.push_back(bytes);
    }

    // send all the packets out, one right after the other.
    addr.sin_addr.s_addr = htonl(0xC0A80016);// 192.168.0.22
    addr.sin_port = htons(24105);

    for(int i=0;i<max;++i) {
        if (sendto(s, (const char *)v[i].data(), v[i].size(), 0,
                   (struct sockaddr *)&addr, sizeof(addr)) < 0) {
            cout << "i: " << i << " error: " << errno;
        }
    }

    closesocket(s);

    cout << "Complete!" << endl;
}
#包括
#包括
使用名称空间std;
#包括
int main()
{
//这是windows,我们需要这个。
WSAData WSAData;
int-wres=WSAStartup(MAKEWORD(2,2)和wsaData);
如果(wres!=0){exit(1);}
插座s=插座(AF INET,SOCK DGRAM,0);
如果(s<0){退出(1);}
地址中的结构sockaddr\u;
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=htonl(INADDR_ANY);
地址sin_端口=htons(0);
if(bind(s,(struct sockaddr*)&addr,sizeof(addr))<0{exit(3);}
int max=100;
//构建要发送的所有数据包
typedef向量ByteArray;
向量v;
v、 储备(最大);

对于(int i=0;i最可能的情况是客户端超出了udp发送缓冲区。可能是在运行ARP协议以获取目标MAC地址时。您说您在第一次运行时丢失了数据报,如果您等待了2分钟或更长时间。为什么不向Wireshark检查第一次运行时会发生什么?(如果发送/接收了ARP帧)

如果这是问题所在,您可以应用以下两种备选方案之一:

1-在运行之前,确保ARP条目在那里


2-发送第一个数据报,等待1秒或更短时间,发送突发消息

我遇到了类似的情况。在我的情况下,一个似乎在Windows XP中运行良好的应用程序在Windows 7中出现问题,原因是UDP数据包/消息丢失。我看到的是客户端向服务器发送请求消息,服务器接收该消息并发送确认在发送实际响应之前完成请求的确认。客户端从未看到确认,但它确实看到了随后的服务器响应消息。您的调查结果如何?@RichardChambers-我从未确定到底发生了什么。我最终对源系统进行了轻微的速率限制,这很有效对于我的应用程序。我刚刚发现它有一个关于在Windows 7下使用
setsockopt()
函数增加网络缓冲区大小的注释。我今天将尝试一下,看看会发生什么。