Stumped! Why isn't my application sending packets?

Started by
19 comments, last by rip-off 10 years, 12 months ago

While debugging it this code returned false


	r = WSAStartup(MAKEWORD(2,2), &Winsock);
	if(!r)
	{return false;}

which would suggest the socket isnt being made and I have no idea why tho

Advertisement

Upon adding that error checking method to my other networking application using similar code, it also caused my server to no longer receive updates on the client's position, so I have removed that. Does anyone know why this isn't working, its really starting to bug me :/

While debugging it this code returned false


	r = WSAStartup(MAKEWORD(2,2), &Winsock);
	if(!r)
	{return false;}

which would suggest the socket isnt being made and I have no idea why tho

http://msdn.microsoft.com/en-us/library/windows/desktop/ms742213%28v=vs.85%29.aspx

why exactly did you think zero for that function means failure?

I'm looking over your code, and nothing terrible jumps out to me. have you tried changing to tcp to make sure everything's setup correctly, then switch back to udp?

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

Upon adding that error checking method to my other networking application using similar code, it also caused my server to no longer receive updates on the client's position, so I have removed that. Does anyone know why this isn't working, its really starting to bug me :/

Step 1: error checking. Step 2: error handling/logging. For example, the documentation for WSAStartup says:

Return value
If successful, the WSAStartup function returns zero. Otherwise, it returns one of the error codes listed below.
The WSAStartup function directly returns the extended error code in the return value for this function. A call to theWSAGetLastError function is not needed and should not be used.
<list of error codes omitted>

So WSAStartup is not returning false, it is returning its "success" value, which you are misinterpreting as failure. It is important to thoroughly read the documentation for the functions you are using about what kind of error codes can be returned.

You should at the very least write a log message somewhere when a fatal error occurs, and preferably provide a notification to the user rather than crashing to desktop. Most APIs have a way to get a human (or at least, geek) readable error description, which will help enormously in diagnosing such failures when they happen to a random user on the internet, who might want to play your game but might not be particularly technical.

A few things still need some work:

  • You aren't checking for errors creating the socket in Application::Init_Winsock(). In addition, you only perform partial cleanup were something to go wrong in the middle of this function.
  • Your recvfrom() call should check that the number of bytes received was (at least) the number that is expected. It might not be particularly likely, but you could collide with an existing port number, and another application may attempt to send a message to the port you are listening on (for UDP in particular, you can easily pick up broadcasted discovery messages on a local network).
  • You still have not addressed the thread synchronisation problems in your code. A simple fix would be to ensure that all accesses to Application::Player are controlled by a Critical section. There are more sophisticated ways of handling this, particularly if your "Player" variable is currently being accessed all over the place.
  • Your background thread's error handling isn't very useful. If a receive fails, you silently (i.e. without even printing a log message) return "false". Where does this return value go? Well, the operating system will store the return value, but your code does not appear to have anything listening for the thread unexpectedly dying.
  • A relatively simple alternative to multi-threading is to use "non-blocking" sockets. This means you can try to receive in your main loop without stalling the game logic until new data arrives. I would recommend this to someone who is still getting to grips with socket programming. Trying to learn and understand multi-threaded programming is significantly harder, and trying to do both at the same time is a recipe for confusion and error.

    When you have your code working, you might also consider writing some code to detect and handle the case where the client or server dies or is shut down. Something as simple as checking if no messages have been received for a reasonable period of time, and then assuming the remote peer is "gone" and informing the user.

    Can't you offer me some sample code, I don't understand fully what you mean

    We're not here to write your code for you ;-)

    The advice offered in this thread should be ample to solve your problem. If you have specific areas you don't understand, please point them out directly. Just saying you don't understand is not useful, since we have no idea what you actually want explained in more detail.

    Wielder of the Sacred Wands
    [Work - ArenaNet] [Epoch Language] [Scribblings]

    I'm unsure how to create these advance ways of error checking, I've put watches on the variables and they all seem to be taking in the expected values, still, there is no networking. I've used very very similar code before on another project, which was transferring only x,y values.

    So all I've done is added a z value to the struct, and its not working

    It's a very simple process, albeit work-intensive and tedious at times:
    • Find an API that you call that is not currently error-checked
    • Look up the documentation for that API to determine what it does (if anything!) to indicate failure
    • Write some simple checks (usually just if statements) to see if that failure happened at runtime
    • Decide on how to handle failures: throw an exception, display a runtime error message, etc.
    • Implement your chosen mechanism for failure-response
    • Repeat from the top
    It's also worth noting that "it's not working" is not helpful to anyone. Describe what you expect to happen, what actually happens, and try and pinpoint where the two are not identical.

    Wielder of the Sacred Wands
    [Work - ArenaNet] [Epoch Language] [Scribblings]

    Can't you offer me some sample code, I don't understand fully what you mean

    Which part didn't you understand?

    Can't you offer me some sample code, I don't understand fully what you mean

    Which part didn't you understand?

    How to code these error checks you speak of as mine isn't getting the job done

    This topic is closed to new replies.

    Advertisement