UDP problems in server/client app

Started by
13 comments, last by hplus0603 15 years, 10 months ago
Hi We're making a server application in C++ and SDL_Net and a client in VB.NET. Right now the client can connect and send values to the server like we want it to do. It successfully sends the message "p1" to the server first, which symbolizes the starting point of the application (not really relevant to my question though). The problem occurs when the actual application starts though. The purpose with our application, is to send the mouse y-coordinate to the server from the client. This works perfectly if I send a datagram with less than 4 characters. IF the datagram contains the value "p1512" for example, the server which receives the message only shows garbage data and cannot use the value. However, if I send a datagram which contains less than 4 characters, for example "p15", it works perfectly. This is the main problem we have stumbled on with our network project. We cannot get any further because of this and have no ideas what's wrong. We've double-checked the code and tried everything. Help is much appreciated.
Advertisement
Check your code again - seems to be a problem with your code.
Buggy Racer & Stuffhttp://programmingcorner.freaksolutions.com
Show the code you use for sending and receiving (sendto(), recvfrom() calls).
If you send 5 bytes then you should receive 5 bytes. You should post some code so we can offer a bit more insight. Specifically the code that does the send and the code that does the receive would be helpful :-)
Sounds like you're using sizeof() instead of strlen() to figure out how big your string is.
enum Bool { True, False, FileNotFound };
Well then, here's some code for the server (C++/SDL_net)

// allocating packet is done before the main loopif (!(p = SDLNet_AllocPacket(10))) {    fprintf(stderr, "SDLNet_AllocPacket: %s\n", SDLNet_GetError());    exit(EXIT_FAILURE);}// this is called inside the main loop, it simply shows all the messages that is receivedmemset(p->data,0,sizeof(p->data));if (SDLNet_UDP_Recv(sd, p)) {    std::cout << (char *)p->data << std::endl;}


The garbage data always exceeds the allocated size 10 FYI. I don't have access to the VB.NET-code right now, but after sniffing the datagrams that were sent, we're fairly sure it's the receiving side that's doing it wrong.

I can post the code for my other client, just going to check if the same thing occurs using that.

edit:
It worked when I made a C++ client to send the datagrams. Is there some known conflict between VB.NET and C++ when it comes to networking? I think my friend only uses the UDP Send function when sending the messages.

[Edited by - password on May 28, 2008 7:45:39 AM]
As hplus0603 said above, you're using sizeof to measure the length of your data, which is wrong. sizeof is a compile-time measure to tell you the size of a given type, and can't know how much data has been placed there. You need to query the packet's 'len' property to see how much to read. Docs here.
Quote:Is there some known conflict between VB.NET and C++ when it comes to networking?


No. It's quite likely it's a bug in your sending code.
enum Bool { True, False, FileNotFound };
Quote:Original post by Kylotan
As hplus0603 said above, you're using sizeof to measure the length of your data, which is wrong. sizeof is a compile-time measure to tell you the size of a given type, and can't know how much data has been placed there. You need to query the packet's 'len' property to see how much to read. Docs here.


I only use sizeof in the memset and memset is something that wasn't originally there either. I don't use sizeof to measure the length of my data at all.
You're still not posting the sending code.
Also, if you sniff the data using Wireshark, what data is in the packet on the wire?
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement