How exactly does Winsock work?

I had been working with a game engine in vb6 called Eclispe Origin. It's use winsock and vb6 together. But now Im trying to make an engine in c++ using winsock, but I simply doesnt know how it work.

My idea of a networking gaming system is the handle packet system. It's like having an array for a socket. For example: socket Winsock[MAX_PLAYER], each player when connect to the server will receive their own index. And thus make it easy to send the data to player by sending it to their index.

And inside that socket there is a packet enumeration, which number all the packet that will be send at one point through a function. And there will be a handle packet function too, I don't know how to explain them as my English isn't good. But, do you have any tutorial for winsock that cover specificly for game programming?

I already checked out Beej and MadWizard's tutorial. I still don't understand anything about winsock
The sockets library is a C library that allows network connections to be used like files. It was developed on BSD Unix in the 80s and is the most common way of using TCP/IP. The Microsoft port of the sockets library is called WinSock.

There are two ways of communicating over most network protocols, an unguaranteed delivery method and and guaranteed delivery method. In IP these are UDP and TCP. If you're running on a LAN, you can get away with UDP. If you're need a guaranteed connection out to the big bad world, you'll need to use TCP.

UDP deals with "datagrams". You send them to any address without establishing a connection.

TCP creates streams between two end points and you read/write across the connection. i.e. you treat it like a file.

TCP streams are unformatted. That is, you need to create your own formatting, decide your end of line, end of record, ... all that stuff. For example HTTP requests use \r\n\r\n as the end of request.

There are a few differences between Winsock and sockets.
1. You need to initialise the Winsock library with WSAStartup.
2. A socket in Winsock is SOCKET and is not a file. In Unix, a socket is a file descriptor and so can be used by file routines (read/write/close).
3. Winsock has a bunch of functions that make it easier to work with Windows' messaging callbacks. Just say no, it's an unnecessary complication.

That's the background and the battery on my laptop's about to die. If you have specific questions, please ask.
So, let just say, I want multiple connection.

And let just say, I followed this guy's code on youtube:

Is this a blocking server? and is this 1 person connected or many people can connected at the same time?

If not, then how can I make it?

I was thinking about something like

sSocket (assume this is a SOCKET) = new SOCKET

and to ensure that I know which data to which people, I would have something like:

int Connection = 0;

whenever there is a connection
I would have the winsock send the number of int Connection over, then have Connection += 1

Is it how it should work?

sorry for my english im not a native english speaker
To do what you want to do, you also need to learn about multithreading (at least from what I've learned about sockets). When your socket is "listening" it causes your program to wait until that socket receives something, then your program returns to where it was. The issue with connections now a days is that it's not a turn based system (you could make it like that if you want) it's a real time system (which is where multithreading comes into play).

Take this scenario for example:
You have a simple chat program going and you decided against using multithreading. You connect to your other client and the client sends you a message back letting you know they got your message. You type up a sentence or two asking them how they are, and your program sends it out and waits for a reply. Moments later, you get a response from the other program (letting you know it got your message) so your program sends one back and waits again. Several minutes go by and you still haven't received a message back. Your program is still waiting. You have no idea what happened so you let it sit while you go out for lunch.

An hour goes by and your return and look at your program, there was a response. Your program had already sent back the received notice and also got it's notice back as well. You send another message apologizing for the delay because you went for food. No answer. Two minutes go by, no answer. After 30 minutes, you never receive the notification back from the other program that it got your message.

You call your friend up and ask him what happened. He then informs you he didn't want to wait for a response so he quit the program.

There are a few things to avoid a scenario like this, but the idea is that you have to wait your turn to send a message out, and that's just over TCP/IP. To do anything with a server, you either need to buy a server, rent a server, know someone with a server who is nice enough to let you use it, or let your personal computer become a server. Everything for your game must then be routed through your personal computer (read: it becomes open to all sorts of possible virus attacks without the proper software) and if something happens to your computer, the game would be down until your computer reboots and loads the server application or until you get another computer (in case you didn't have the proper software).

My point being, there is a lot of things to consider before jumping into sockets and gaming over them. Luckily, C++11 now has support for multithreading and you can use standard sockets via the boost libraries. I'm still trying to learn the best way to handle each.
So I should learn about multithreading? Is there a good tutorial that you would recommend? Thank for reply

Edit 1:

And oh yes, Im planning to use my own personal computer to be the server.
Last edited on
I have yet to find anything that really explains using the multithreading features yet. I just got "Professional C++ (2nd Edition)" which covers some C++11 features, but I haven't read through to see if it covers it yet.

I can give you a tip though, you'll need atleast three threads. 1 for the "server" connection, 1 for the "client" connection, and 1 for the game. I doubt your computer would be fast enough to run multiple games simultaneous on it, so it might be wise to decide who's computer should run the game between the two (unless of course you're going for more of a MMORPG style game, then you should host the game).
There are libraries out there for C++ that make using threads and networking a lot easier, take a look at them some time.
The difference between a blocking call and a non-blocking call is the blocking call will wait until the operation is complete before returning, the non-blocking call will return immediately.

For example, if I call send() to write a large block of data to another computer, with blocking sockets, the call will return when a chunk of data has been sent or the call failed. With non-blocking sockets, the call will return immediately and you can do something else, then check later to see what happened.

Processing Multiple Connections
TCP uses the client/server model. You should note that a server is a piece of software, not hardware. The server initialises and begins accepting connections, so that's how you'd get a collection of connected clients in your original question.

What to do with them. You can deal with clients in parallel using multiple concurrent threads of execution or you can deal with them in one thread by multiplexing socket connections in a single thread. Of course you can mix these.

Multiplexing is the method that allows you to check if a client has something to send, and do a read only when that socket has something to send. So the loop looks like:
loop for ever
    if (timeout)
        do something else for a while
        for each client that we can read from
            read from client
            do some processing if needed

The actual call that does this check is select().

What I Recomend
You need to understand the basics of socket communication. So stop what you're doing and take a look at some simple examples. Douglas Comer has published some minimal sample C programs that are very concise that I find are the best examples.

Once you understand in general what's going on, use a wrapper library to deal with the networking for you. Understanding the principles and writing robust networking software are two different things.

Once you get going, we can provide example code and possible network libraries on request.
Last edited on
1. Yes I am going for MMORPG style game.
2. I do know what blocking and non blocking is
3. I had read through MadWizard and Beej's guide, is it enough? If it isnt then do you have any other tutorial that you would recommend? I will reread them today.
Just run a couple of Douglas Comer's examples. They're basic UDP and TCP services that use the correct methods to look up protocol types, service types and IPv4 hosts. You can extend these to pass your own data to get a feel for what's involved.

For example, it's surprising to discover that the number of sends doesn't necessarily match the number of receives. And that fragmented data requires multiple recv() calls. Dealing with multiple record transfers is tricky unless you've dabbled with it.

Once you've understood this sort of thing, you may need a 3rd party network lib.

EDIT: You can find Comer's stuff here. BTW, his three book series, Internetworking with TCP/IP, are the books in my opinion.
Last edited on
Topic archived. No new replies allowed.