更快的 WinSock sendto() [英] Faster WinSock sendto()
问题描述
我使用的是 Windows Server 2008,我的程序是用 C++ 编写的.我在 while(true) 循环中使用 WinSock2 和 sendto() 来发送我的数据包.
I'm using Windows Server 2008, and my program is in C++. I'm using WinSock2 and sendto() in a while(true) loop to send my packets.
代码如下:
while(true)
{
if(c == snd->max)
c = snd->min;
dest.sin_addr.S_un.S_addr = hosts[c];
iphead->destaddr = hosts[c];
sendto(s, castpacket, pktsz, 0, castdest, szsad);
++c;
}
我需要尽可能快地向主机 std::vector 中的尽可能多的 IP 发送尽可能多的数据.
I need to send as much data to as many IPs in my hosts std::vector as possible, as quickly as possible.
我目前在 i7 930 服务器上运行,我只能达到 350Mbps 左右.
I'm currently running on an i7 930 server, and I can only achieve 350Mbps or so.
我目前将我的程序分成 4 个线程,所有线程都使用分配给每个线程的不同服务器运行 while 循环.添加更多线程或运行更多程序副本会导致吞吐量降低.
I currently split my program into 4 threads, all running the while loop with different servers assigned to each thread. Adding more threads or running more copies of the program results in lower throughput.
我有另一个程序在运行,监听来自服务器的回复.我从主列表中获取服务器并将它们添加到我的阵列中.目前的问题是全部看完需要很长时间,我想定期检查一下.
I have another program running listening for replies from the servers. I get the servers from a master list and add them to my array. The problem at the moment is that it takes too long to go through all of them, and I want to check them regularly.
我如何在此处优化我的程序/循环/发送?
How exactly can I optimize my program/loop/sending here?
推荐答案
我建议改用异步 I/O 以加快速度.一次发送一个数据包的主要问题是当 TCP 堆栈正在处理当前数据包时,您无法将下一个数据包排队.
I would recommend moving to asynchronous I/O to speed things up a bit here. The main problem with sending them one at a time is that you are unable to queue up the next packet while the TCP stack is processing the current one.
或者,您可以采用线程池方法:您触发一定数量的工作线程,每个线程从 FIFO 中选取一个客户端并将数据发送到客户端.当一个线程处理完它的客户端后,它会将客户端放回 FIFO 并选择一个新的.您可以通过调整工作线程的数量来填满管道 - 但不能淹没它.
Alterntively, you can go for a thread pool approach: you fire a certain number of worker threads, and each one picks up a client from a FIFO and sends data to the client. When a thread is done with its client, it puts the client back in the FIFO and picks up a new one. You can fill up the pipeline - but not swamp it - by tuning the number of worker threads.
这篇关于更快的 WinSock sendto()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!