Scenario
Does anyone have any good examples of peer-to-peer (p2p) networking in C++ using Winsock?
It’s a requirement I have for a client who specifically needs to use this technology (god knows why).
I need to determine whether this is feasible.
Any help would be greatly appreciated.
EDIT
And I would like to avoid using libraries so that I can understand the underlying source code and further my knoweldge.
Since I don’t know what information you are looking for, I’ll try to describe how to set up a socket program and what pitfalls I’ve run into.
To start with, *read the Winsock tutorial at MSDN. This is a basic program to connect, send a message and disconnect. It’s great for getting a feel for socket programming.
With that, lets start:
Considerations:
Blocking or non-blocking
First off, you need to decide whether you want a blocking or non-blocking program. If you have a GUI you would need to use non-blocking or threading in order to not freeze the program. The way I did it was to use the blocking calls, but always calling
selectbefore calling the blocking functions (more on select later). This way I avoid threading and mutex’s and whatnot but still use the basicaccept,sendandreceivecalls.You cannot rely that your packets will arrive the way you send them!
You have no control over this either. This was the biggest issue I ran into, basically because the network card can decide what information to send and when to send it. The way I solved it was to make a
networkPackageStruct, containing asizeanddata, where size is the total amound of data in that packet. Note that a message that you send can be split into 2 or more packets and can also be merged with another message you send.Consider the following:
You send two messages
When you send these two messages with the
sendfunction yourrecvfunction might not get them like this. It could look like this:or perhaps
whatever the underlying network feels like.
Log (almost) everything!
Debugging a network program is hard because you don’t have full control over it (since it’s on two computers). If you run into a blocking operation you can’t see it either. This could as well be called "Know your blocking code". When one side sends something, you don’t know if it will arrive on the other side, so keep track of what is sent and what is received.
Pay attention to socket errors
Winsock functions return a lot of information. Know your
WSAGetLastError()function. I’ll won’t keep it in the examples below, but note that they tend to return alot of information. Every time you get aSOCKET_ERRORorINVALID_SOCKETcheck the Winsock Error Messages to look it up.Setting up the connection:
Since you don’t want a server, all clients would need a listening socket to accept new connections. The easiest is:
INADDR_ANY is great – it makes your socket listen on all your IP addresses instead of just one IP address.
Here comes the interesting part.
bindandlistenwon’t block butacceptwill. The trick is to useselectto check if there is an incoming connection. So the above code is just to set the socket up. in your program loop you check for new data in socket.Exchanging data
The way I solved it is was to use
selectalot. Basically you see if there are anything you need to respond to on any of your sockets. This is done with theFD_xxxfunctions.In the
newSocketyou now have a new peer. So that was for receiving data. But note!sendis also blocking! One of the head-scratching errors I got was thatsendblocked me. This was however also solved withselect.Shutting down
Finally, there are two ways to shutdown. You either just disconnect by closing your program, or you call the
shutdownfunction.selecttrigger.recvwill however not receive any data, but will instead return 0. I have not noticed any other case whererecvreturns 0, so it is (somewhat) safe to say that this can be considered a shutdown-code. callingshutdownis the nicest thing to do.shutdownjust is cold-hearted, but of course works. You still need to handle the error even if you useshutdown, since it might not be your program that closes the connection. A good error code to remember is 10054 which isWSAECONNRESET: Connection reset by peer.