# WSASendTo takes a while to complete

This topic is 2494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm having issues with my network layer in my game where WSASendTo will wait for ~10 seconds before completion after calling it in a loop with a total of approximately 8k of data.
So I made a little test executable that goes like this:
(i'm using boost socket which ultimately calls WSASendTo)
 boost::asio::ip::udp::socket* mpSocket; mpSocket = new boost::asio::ip::udp::socket(IoService::GetService(), boost::asio::ip::udp::v4()); boost::system::error_code err; boost::asio::ip::udp::endpoint epTo = ResolveAddr("192.168.0.4", 2772, err); if(err){ printf("ResolveAddr err : %d (%s)", err.value(), err.message().c_str()); return; } char buff[1024]; int i = 0; do{ printf("Send %d, ",i); boost::asio::ip::udp::socket::message_flags flags = 0; *((uint32 * )buff) = i; size_t si = mpSocket->send_to(boost::asio::buffer(buff,1024), epTo, flags, err); printf("%d\n", si); if(err){ printf("send err : %d (%s)", err.value(), err.message().c_str()); return; } ++i; } while(i<1024); 
192.168.0.4 is another computer on my local network.
It will call mpSocket->send_to 8 times. The last call will send the data (which I successfully receive on 192.168.0.4 ) and wait for an even for ~10 seconds in function WSASendTo.

Note When issuing a blocking Winsock call such as WSASendTo with the lpOverlapped parameter set to NULL, Winsock may need to wait for a network event before the call can complete
If it waits for that reason, I'm not sure what network event it is waiting for or why, but most importantly why does it takes ~10 seconds?
It doesn't make much sense to me to wait that long for every 8k of data transferred. So what am I doing wrong here?

Thanks

##### Share on other sites

I'm having issues with my network layer in my game where WSASendTo will wait for ~10 seconds before completion after calling it in a loop with a total of approximately 8k of data.
 int i = 0; do{ .... ++i; } while(i<1024); 

It looks to me as if you're sending 1024 bytes 1024 times -- thus, a total of 1 MB of data in > 1000 packets.

##### Share on other sites
1MB shouldn't take that long under LAN conditions, especially with UDP.

The most likely explanation is that your NIC is refusing to send the packets in a timely manner, and your buffers are filling up. When a blocking-mode socket calls WSASendTo, if will block until it can copy the data from your process' buffer into the operating system/network card's buffer. Usually this is nearly instantaneous, but if that buffer doesn't have enough space, you'll block until it starts emptying out. The buffer might fill up due to any number of reasons: Other processes on your computer may be hogging the network. It might mean something's wrong with your LAN hardware. You might have a particularly intrusive virus scanner, firewall, or packet inspector running.

One unlikely thing you can try if you're on windows, using a gigabit ethernet NIC, is to disable all of the 'checksum offload' settings in the network card's driver properties. I've had cases where something in this checksum behavior causes unexplainable blocking or packet drops.

Another possibility is that your socket is unbound and trying to send on an unresponsive local endpoint, before it times out and falls back to the one that actually works. You might try enumerating your local endpoints and explicitly bind your socket to the one you know works. This usually won't happen unless you've got some kind of VPN or proxy software installed, or have been reconfiguring your network in unorthodox ways.

##### Share on other sites
At first I thought it might be a buffer filling up and waiting to be freed since blocking after 8k of data sound oddly like a buffer issue. But the strange thing is that it block in the WSASendTo after the data gets transferred. The client executable receive the data sent by that last blocking WSASendTo. So that theory doesn't seem to hold.

So my last resort when facing such a strange behavior is to reboot and try again. Which I did. It fixed the issues. That confirm my second thoery, there really is something weird going on.

Thanks for the advices anyway, I'll try them if the problem arises again.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 14
• 9
• 10
• 12
• 17
• ### Forum Statistics

• Total Topics
632904
• Total Posts
3009150
• ### Who's Online (See full list)

There are no registered users currently online

×