How to pick up async winsock quickly?

Started by
7 comments, last by hplus0603 13 years, 11 months ago
I was hoping to write a little networked client-server program, and am familiar with .net remoting, java rpc, and a bit of blocking sockets I did a while ago. What is the fastest way to get async sockets working, and is there a good way to learn, either via online web site text/images/video or if someone wants to post a quick rundown here that we can discuss that's cool too, or if anyone wants to recommend a (hopefully cheap) book from amazon.com or wherever that's also okay. I should note I'm using Microsoft Visual C++ Express 2008, their free c++ compiler, if that matters.
*-----------------------sig------------Visit my web site (Free source code and games!) @ http://SpaceRacer2025.blogspot.com--------------------------------------*
Advertisement
boost::asio

EDIT: Sorry, I stopped reading at "get ... working".
theres a breif intro on them in this tutorial:

http://www.gamedev.net/reference/articles/article1059.asp

However it maybe useless i skipped that part of the tutorial and iv'e never used that type of sockets before
Some documentation that can be really useful:

MSDN Article on Windows Sockets 2

Winsock Programmer's FAQ (got some nice examples too, see section 6 at the bottom):
Try these;

Async Sockets in TCP/IP (The Client)
Async Sockets in TCP/IP (The Server)

Follow these and you will have a fully working async chat server and client.
If you really want to learn all the low-level stuff, then you should keep looking at MSDN, and read the tutorials given to you. And then make your own socket class, perhaps even a separate server and client class, with a base socket class.
This is what most tutorials lack, they don't teach you efficient ways to structure programs.
I picked up many bad habits from tutorials myself, so be careful.

And then make a program that can transfer a file in chunks, or a simple chatting program. But anyhow you should do something that is aligned with your higher goals.

And you should also use a library that has an efficient way to handle buffers and strings. The standard libraries for C++ are no good for game programming.
I made one myself so I have no library to suggest for you.
Quote:This is what most tutorials lack, they don't teach you efficient ways to structure programs.

Tutorials demonstrate an API, the parameters, the invocation order.

Quote:And you should also use a library that has an efficient way to handle buffers and strings. The standard libraries for C++ are no good for game programming.


Then again, DirectX, Berkeley sockets and OS-provided file system and threading is also no good. Might as well start from scratch.

STL is just fine. It just comes down to using them correctly.

Also a fun fact - on any mid-range today's PC with built-in network card, dealing with sockets will consume less than 10% of CPU by the time network is saturated. Or something else will break (drivers, some obscure lock in kernel, non-pagable memory) way sooner than any of STL will become a problem.
I was just explaining this to someone else. The Winsock 2 overlapped I/O stuff is pretty under documented and ideally is what should be used. You'll want to read the WSASocket, WSAAccept, WSASend (assuming TCP), and WSARecv documentation available on MSDN. The main concept is realizing that there are callback functions that can be used.

You'll have to read and understand those pages and write the code. The examples listed aren't what you want. None of them demonstrate overlapped I/O with callback operations. It takes a while to fully understand all of the errors and handle them correctly, but it's well worth it since you can turn that previously mentioned 10% CPU into nearly 0% which is nice. However, it has the added complexity that it assumes you understand multithreading concepts like mutices since you'll end up needing them usually.

Might look into boost asio as mentioned also.

[Edited by - Sirisian on May 7, 2010 2:52:27 PM]
Quote:The Winsock 2 overlapped I/O stuff is pretty under documented


Actually, they aren't under-documented. They just assume you are already familiar with Windows NT I/O completion ports, thread pools and OVERLAPPED I/O. If you don't know those things, then trying to use them with sockets isn't going to work, no matter how well the sockets are described...

If you want a simpler API for "overlapped" sockets, I recommend boost::asio. It ends up using overlapped I/O at the bottom on Windows, but the interface is a bit simpler to use, it has more examples in the documentation, and it's portable to other environments.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement