Good way of implementing a server in my app?

Started by
5 comments, last by MichaelStieler 12 years, 4 months ago
Hi guys,

Beginner in network programming here, trying to add networking to my simple flight sim game to export avionics to a network computer.

I have read on winsock, tried a few things and faced a wall...

I can start my server all right, and can get my client to connect to it and receive data, but my problem is as soon as the server starts listening for incoming connections, the game window freezes and I can't do anything with it, not even close the window, I have to ctrl-alt-del to stop execution. I'm pretty sure I know what's happening (its actually obvious!) I use an infinite loop to listen for incoming connection, then once one come in and is accepted, execution resumes.

What I want to do is keep listening for incoming connections without hanging the rest of the execution.

Creating the server on a different thread seem to be the answer, but before I dive in multi-threading lessons, I wanted some feedback on what is the best way to do what I want to do. I heard about WSAAsyncSelect, which apparently could be another solution.

Can anyone give me some advice?

Thanks guys!
Advertisement
Research non-blocking sockets. Threading is a very difficult thing to get right, especially when network communication is involved, and the WSA* functions are basically a nightmare straight from the pit of hell.


Also, moving to the multiplayer/networking forum.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

For servers, I usually use IOCP. It's a scalable concept that works as it should, and then some. For the client, I'm not sure what would be the best method as I've never done any client networking. ApochiPiQ's suggestion is (I would guess) commonly used for clients, seeing as not many connections are made (except for P2P). (I see my next project coming up :D) Remember, IOCP is mainly for a server with hundreds or thousands of connections, since it solves the horrible "1 client per thread" drama.

http://www.codeproject.com/KB/IP/iocp_server_client.aspx
http://msdn.microsoft.com/en-us/magazine/cc302334.aspx
http://www.codeproject.com/KB/IP/iocp.aspx
How do you think servers are implemented on OSes that don't have IOCP, i.e. everything not-Windows? ;-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]


How do you think servers are implemented on OSes that don't have IOCP, i.e. everything not-Windows? ;-)


Non-blocking sockets my guess (I'm not a Unix/iOS person), but I assumed he was aiming for Windows since:

I heard about WSAAsyncSelect, which apparently could be another solution.
[/Quote]

:]

[quote name='ApochPiQ' timestamp='1323919708' post='4894062']
How do you think servers are implemented on OSes that don't have IOCP, i.e. everything not-Windows? ;-)


Non-blocking sockets my guess (I'm not a Unix/iOS person), but I assumed he was aiming for Windows since:

I heard about WSAAsyncSelect, which apparently could be another solution.
[/Quote]

:]
[/quote]



On servers from the dinosaur ages, you would poll the accepting socket, and every other socket, each time through your main loop, using select(). If select() shows the accepting socket as readable, then it is possible to call accept() at least once on that socket without blocking.

On modern servers, you use some kind of notify-based I/O -- epoll, for example, on Linux. In the UNIX world, libevent is used to mitigate the differences between Solaris, BSD, Linux, etc.

If you want a library that can do non-blocking networking in a scalable fashion across many operating systems, including Windows, then boost::asio is probably a good fit.
enum Bool { True, False, FileNotFound };
Did you solve your problem?

If you are not implementing network for the fun of it, I would recommend looking for (e.g. event based) network libraries. They are also fun to develop with and are usually better when it comes to speed or scalability.

I don't know if this a good list, but at least a good start:
http://content.gpwiki.org/index.php/Libraries:Networking

This topic is closed to new replies.

Advertisement