UDP is really slow over 127.0.0.1?

Started by
1 comment, last by hupsilardee 11 years, 11 months ago
As i'm new to winsock, I'm designing a really simple game which is 1 vs 1 in a dogfight. At first I was using TCP over the loopback interface because I needed to run two instances side by side on the same laptop to test it. That worked fine, so both players can fly around and see each other moving.

However when I tested with a UDP version the response from one instance of the game was very slow - as much as a second passes before you can see the other plane move. Which is strange because over TCP the response is instant. What am I doing wrong? If it helps, here is relevant code minus a lot of error checking etc



int main()
{

bool server;
std::cout << "Run as server? 1/0 ";
std::cin >> server;
std::cout << std::endl;

WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);

SOCKET udpSocket = INVALID_SOCKET;

if (server)
{
udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
SOCKADDR_IN address;
address.sin_family = AF_INET;
address.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
address.sin_port = htons(SERVER_PORT_UDP);
bind(udpSocket, (SOCKADDR*)&address, sizeof(SOCKADDR_IN));
DWORD mode = 1;
ioctlsocket(udpSocket, FIONBIO, &mode);
}
else
{
udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
SOCKADDR_IN address;
address.sin_family = AF_INET;
address.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
address.sin_port = htons(CLIENT_PORT_UDP);
bind(udpSocket, (SOCKADDR*)&address, sizeof(SOCKADDR_IN));
DWORD mode = 1;
ioctlsocket(udpSocket, FIONBIO, &mode);
}

< snip graphics,scene initialisation etc >

while (window->MessagePump())
{
// packet: float4x4 transform

< snip update+rendering>

if (server)
{
SOCKADDR_IN destAddress;
destAddress.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
destAddress.sin_family = AF_INET;
destAddress.sin_port = htons(CLIENT_PORT_UDP);

sendto(udpSocket, (char*)(void*)&plane->Transform, sizeof(Matrix), 0, (SOCKADDR*)&destAddress, sizeof(SOCKADDR));
recvfrom(udpSocket, (char*)(void*)&enemy->Transform, sizeof(Matrix), 0, NULL, 0);
}
else
{
SOCKADDR_IN destAddress;
destAddress.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
destAddress.sin_family = AF_INET;
destAddress.sin_port = htons(SERVER_PORT_UDP);

recvfrom(udpSocket, (char*)(void*)&enemy->Transform, sizeof(Matrix), 0, NULL, 0);
sendto(udpSocket, (char*)(void*)&plane->Transform, sizeof(Matrix), 0, (SOCKADDR*)&destAddress, sizeof(SOCKADDR));
}
Sleep(10);
}

closesocket(udpSocket);

WSACleanup();
}

Advertisement
Weird indeed.
Is the motion just delayed but still smooth? Or is it choppy?
My guess would be that you send data too fast and fill up the networking buffers which causes packets to be dropped.
OK I found the problem. My tcp implementation did not use blocking sockets - this version does, but does not check whether recvfrom aborted to avoid blocking - that meant the receiving of messages was a pure experiment in probability. Without the call to ioctlsocket() the program runs lightning fast as I would expect. Thanks for replying though!

This topic is closed to new replies.

Advertisement