Jump to content
  • Advertisement
Sign in to follow this  
alexisgreene

WSAECONNRESET error when using Winsock loopback

This topic is 2509 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

The following turned out to not be incorrect. The actual solution is posted below.

[s]Took me about 4 days to find the solution to this. The problem was that I was binding my server socket using INADDR_ANY which returns an ip of 0.0.0.0:<port>. The 0's is basically Winsock's way of saying any interface. I was connecting to "localhost" (which translates to 127.0.0.1) in the client code. I was mistaken in thinking that this would be compatible with INADDR_ANY. The solution was to bind the server socket to "localhost", and have my client connect to "localhost".

This may or may not cause problems with remote clients being able to connect. If it does in fact turn out to be a problem I will bind the server socket using the gethostname() function, and have my local client connect to "gethostname()" instead of "localhost". I will edit this post with the results in a day or two once I am able to try it out.[/s]


Here are a few tips to avoid going through what I went through:
1. Use getsockname() after binding your socket, knowing what the assigned address to a socket is will save you a ton of time debugging
2. Pay close attention to the addresses you pass to bind(), and sendto()
3. Make a very simple client program as a seperate project (thanks rip-off) to use as a debugging tool (I found the problem while writing this)

Share this post


Link to post
Share on other sites
Advertisement
Can you build a smaller example? What you are doing is currently lost amongst lots of unrelated code.

Another option is to build a really simple "dummy" client or server (dummy clients are usually easier). Use this to verify that the other, more complex peer is working. This way you avoid the headache of debugging two complex peers simultaneously. If you are using a text protocol, then leverage an existing client like the built in "telnet" program to again reduce the amount of debugging you have to do.

Your CNetwork class is complicated by its efforts to function as both a client and a server at the same time. It is possible that you are mixing up the wrong sockets and ports somewhere. I don't have time to exhaustively analyse the code in its current state. Consider adding some logging output for successful sends (you might want to add a simple timer to prevent getting spammed by new log messages). You might see that the server is sending itself packets or something.

The Windows loopback is not available to Wireshark. Some light Googling suggests you might have more luck if the program connects to the private address of the server (e.g. 192.168.x.x or 10.x.x.x).

Share this post


Link to post
Share on other sites
Do not edit out your questions just because you have solved the problem. Other people with the same problem may want to search the forum later. Also, your "I solved it" post didn't actually describe what the solution was. This shows a very poor attitude towards the shared learning resource that is the forums, where you expect to only get your own problem solved but are not willing to contribute anything in return.

Share this post


Link to post
Share on other sites
You make a valid point. I edited out the earlier posts for a specific reason. I re-edited to describe the problem and the solution for anyone else who runs into this issue.

Share this post


Link to post
Share on other sites

You make a valid point. I edited out the earlier posts for a specific reason. I re-edited to describe the problem and the solution for anyone else who runs into this issue.


Thank you -- I appreciate it!

Share this post


Link to post
Share on other sites
Well... turns out the solution I described earlier is in fact wrong. After getting rid of the WSAECONNRESET error, I began to code the client/server communication. When I tested it out, the WSAECONNRESET error came back. I was originally correct in thinking that INADDR_ANY would be compatible with any interface. I studied my "dummy" client code closely to see what was different and nothing was obvious so I went into the debugger and paused it at the point that the error occurred.

Now the problem was literally written out for me in big red letters. The port number of the incoming address after the recvfrom() call was not the port number my client was sending from, although it was the correct data received. (In fact this was the strange number I reported seeing occasionally in my original post... really wish I didn't edit it out now) The server I am coding uses this value to know what address to use to communicate with the client(s). I didnt find this problem using my "dummy" client because it simply sent data to the server, then I would check the server log to see that it was received. This functionality was because I had no actual server to client communications coded yet.

The solution was to ntohs() on the port number immediately after the recvfrom(). If you plan on using sending to the address returned by recvfrom(), this is absolutely necessary. This fixed the mangling of port numbers and now my client and server are functioning perfectly. I have edited my original comment only to remove the the inaccurate statement about using INADDR_ANY.

I also use htons() on the port number immediately before the sendto() call. I am not sure if this is why it is necessary for me to use ntohs() or not. I am not testing it out either... I finally got this working and I am not screwing with it anymore.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!