Winsock Networking Tutorial (C++)

PowercordPlease take the time to read this introduction, so you will know why things are the way they are.

It's been a long time since I've written a tutorial but finally I've started again. Unlike the previous tutorials I've written, I will publish this one chapter by chapter, as I write them. The obvious advantage of this is that you can read it before I've finished the full tutor, something I hope many people will appreciate. The downside is that I have to carefully pick the right topics in the right order, as changing previous ones is difficult since they already have been published and probably read as well.

As networking is a topic not only limited to win32asm (luckily :) and most of the tutor can also be applied to other programming languages, I've decided to give the reader the option to choose between win32asm and C++. Most of the tutor will be the same for both languages, but the code samples and explanations will be in the language of your choice. Win32asm will be MASM specific as usual, users of other assemblers should be able to convert it easily. Originally my intent was to create a plain vanilla C version, but I switched to C++. However the ++ in C++ (ie. the OOP part) will be used mainly to provide clean data structures in the examples. The code that actually deals with the winsock programming is not really affected by the differences between C and C++, C programmers can benefit from the winsock tutorial as well. I'll test the code with msvc, bcc and gcc so you shouldn't have much trouble using it with your favorite compiler. One side note for the diehard asm programmers: the asm examples will be written for clarity and focused on networking so I will lay optimizations aside.

More than meets the eye

More than meets the eyeNetwork programming seems easier than it is. The winsock functions you can use to communicate over a network are pretty straightforward and easy to use, but many people forget that just calling functions will not do the trick. First attempts in winsock programming often end in programs that work 'sometimes', not at all or produce seemingly inexplicable errors.

There's more to it than just receiving or sending data. For example, you will have to deal with synchronization: is everything sent or received? is the program ready to send it's next piece of data? Other problems are validation of the received data, parsing it, prevention of security risks and much more.

Because of this, you will find that the first tutorials will not contain much code. This is to get you started with network basics and everything concerned with networking programming before starting to write programs. Do not skip these tutorials inpatiently, it's very important to read them.

So what do I need?

  • A good winsock API reference. At my tool archive page you can find a winsock 2 API reference in the old help format. While this file will do for most purposes, the latest platform SDK would be even better. If you have a fast connection, get the latest PSDK. It includes the latest documentation on winsock 2.
  • If you're running win2K or XP, download the 'Winsock Debug Tool' at my tool archive page. This little tool shows all open socket handles and the data that is sent or received via the sockets.
  • Experience with the win32asm or C++. This tutorial only focusses on the networking aspect of programming. I assume you have enough knowledge of the language of your choice to follow this tutorial.