Sign in to follow this  
aersixb9

How to pick up async winsock quickly?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this