The Basics about Winsock
written by Barnski 2006.03.06. (barnski__hat__swissonline.ch)


This tutorial teaches you the basics about Winsock. It is kept general, so that your understanding of Winsock will go beyond the usage of my Plugin.


What is Winsock
Winsock is an API made by Microsoft, that is used by windows applications that use network capabilites. Winsock consists of the words Windows and Socket. Windows because it is meant to be used only under Windows. It is a part of Windows, and the newest version, 2.2 is distributed with WinXP. The Winsock API library was not complety created by Microsoft, it resides on UNIX sockets. Winsock is mostly just a compatible version with Windows, and some extended functionality.


What are Sockets
Socket is the name used to describe a handler of the network connection. Every network connection you use will have a socket that handles it. Each socket uses one port on your machine to communicate. You have 65536 ports on your PC available, so you will have a max of 65536 sockets you can create.


What are Channels
In my Plugin, I hid the word socket from you. I only use the word "channel" to denote what I described above. A channel is yet more than a socket, it stores information about the socket, and provides some functions to work with it. It makes the use of sockets easier. (You probably won't find the word channel elsewhere in conjunction with sockets, as I made it up for my Plugin).

I will now continue to use channels to describe the work of sockets. But all this can easily be mapped to sockets, if you read another Tutorial about Sockets, you will be able to understand it.


How do channels transmit data?
A channel will send packets to another channel over the network/internet, or also locally on your machine. These packets generally consist of a header and a packet body. The header is used to make sure your packet arrives at its meant destination, when it flows through the internet. The body contains the actual information you want to send.

There are several ways to use channels to transmit data to another PC / channel. My Plugin provides support for two Protocols: TCP and UDP. You might already have heard about them. The way how the channels work depends on what Protocol type you decide to use.


TCP Channels
The TCP protocol is probably the easiest one to use, because it will guarantee some basic things and handle packet management for you. For example, it will make sure that every packet arrives at its destination (which may not be guaranteed with other protocols!), and that they arrive in the same order as they were sent (e.g. last packet you send will be the last packet received). To provide this service, you will have to establish a connection with another channel first.

Let's make a simple, figurative example: Alice wants to communicate with Bob.
To connect to Bob, Alice needs Bob's IP address (hostname) and a port number. If Bob has a channel (socket) associated on that port number, then Alice might be able to connect to it. However, if Bob's channel is not set up properly, Alice's connection attempt will fail. First of all, Bob's channel has to use TCP too. Then, it will need to be bound to a port. Every channel can only be bound to one port. After that it has to be set into a specific mode, called listen mode. In this state, the channel listens for any connection requests that may arrive. It only listens, nothing else. A listen channel has a queue of pending connection requests, usually set to max. 10 requests. Now Bob is able to receive the connection request of Alice. He now may decide to accept or reject the connection request. Let's say he is in the mood to communicate and wants to accept it. This is NOT done with the listen channel, but with a new one. The connection will be transfered to the new channel that uses a different port as the listen channel on Bob's computer. This allows Bob's listen channel to continue listening for other requests, maybe from Charlie, on that port! Now the connection is established, the channels connected and Alice and Bob can start to exchange data.


UDP Channels
The UDP protocol is for advanced users, since it will let alot of packet management up to you. Packets may be lost during transmition (packet loss), and may not arrive in the correct order. You may wonder now why there is something like UDP when there is TCP... the reason is speed. It's a trade off between functionality and speed, and you have to choose what protocol type best suits your situation.
You will not need to establish connections with this protocol. You can just create a UDP channel and send a packet to any destination you like... IF there is another UDP channel active at that adress and that port, then your packet might be received (if it didn't get lost). The receiver will get your adress from the packet header, and be able to respond.
Packet loss is usually about some percents, so its not dramatic, but needs to be considered. Usually you will use UDP to transmit positioning data of your objects, since it does not matter alot if one packet of a hundred is missing out.. you will probably not notice. But if you send some chat text, you will want to make sure that it arrives, and in the correct order. You can achieve this by using UDP with your own packet management methods. Or you use a seperate TCP channel for things that do not need to be delivered fast, as for chat text.


Channel Types: Conclusion
Use TCP if speed is not that big issue, and if you want to make sure that your packets arrive correctly (e.g. chat, round based games, but also strategy games, MORPGs...)
Use UDP if you want your packets be delivered as fast as possible, so that there is no high pingtimes/lag (e.g. action games; 3d shooters, but also fast strategy games, action MORPGs).


In the next Tutorial I will show you how to specifically use my Winsock Plugin to achieve the described functionalites!