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 from 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 of, you would need to determine if you want a blocking or non-blocking program. The big difference is that 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
select
before calling the blocking functions (more on select later). This way I avoid threading and mutex's and whatnot but still use the basicaccept
,send
andreceive
calls.You cannot rely on that your packages will arrive the way you send them!
You have no impact on 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 asize
anddata
, where size is the total amound of data in that package. Note that a message that you send can be split into 2- or more messages 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
send
function yourrecv
function 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 alot 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. Everytime you get aSOCKET_ERROR
orINVALID_SOCKET
check the Winsock Error Messages to look it upSetting up the connection:
Since you don't want a server, all clients would need a listening socket to accept new connections. The easiest is:
The INADDR_ANY is great - it actually makes your socket listen on all your networks instead of just one ipaddress.
here comes the interesting part.
bind
andlisten
won't block butaccept
will. The trick is to useselect
to 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
select
alot. Basically you see if there are anything you need to respond to on any of your sockets. This is done with theFD_xxx
functions.in the
newSocket
you now have a new peer. So that was for receiving data. But note!send
is also blocking! One of the "head scratching errors" I got was thatsend
blocked 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
shutdown
function.select
trigger.recv
will however not receive any data, but will instead return 0. I have not noticed any other case whererecv
returns 0, so it is (somewhat) safe to say that this can be considered a shutdown-code. callingshutdown
is the nicest thing to do..shutdown
, since it might not be your program that closes the connection. A good error code to remember is 10054 which is WSAECONNRESET: Connection reset by peer..If you just want to implement a P2P application on Microsoft Windows, you can try with Windows Peer-to-Peer Networking
If you want to implement a new P2P protocol of your own, you can study the eMule protocol, and eMule source code. You could do further if you look into Shareaza source code, it do eMule/Guntella/Gnutella/BitTorrent.