Jump to content
  • Advertisement
Sign in to follow this  
hupsilardee

UDP is really slow over 127.0.0.1?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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();
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!