Sockets In Games

Started by
3 comments, last by ZedFx 19 years, 2 months ago
What is the best way to implement socket sending / receiving in code? Is through polling it in an event loop calling recv / recvfrom along with polling for all your other events or is it with a second thread. Note: I am looking for a cross platform solution. Also which is the better of the following: connection-orientated / connectionless or does that depend on the circumstances of usage, in which case, is the favorable solution: using a connection-orientated socket for main game play, and a connectionless socket for chat commands, etc? In addition, how many sockets does your game use, what are they used for and what information do you deem important to send across the connection? Finally, is it worth using a multicast or broadcast when creating a LAN solution, and how would you go about doing webcast if it is necessary for the internet (please include any information about webcasts - eg. what they are - because I have not actually done any research into what they are, im just curious) Your opinions are appriciated.
Advertisement
The Forum FAQ answers most of your questions.

Broadcast on LAN works, if you like that model.

Multicast is a fundamentally broken idea.

"Webcasts" are just a regular web or media server, serving some large number of streams that all take their data from the same source.
enum Bool { True, False, FileNotFound };
Yes I did read the forum FAQ afterwards and realised that I'd mostly asked non-questions as far as we're concerned.

Although, I would still like to know what ways there are of receiving data, eg. a second thread or not (primarily)
Yes, you can thread if you want, or use select(), or, on Windows, use completion ports or asynchronous select events. All of these are possible, and have different trade-offs.

I find that the best overall solution often is select(). Threading adds a lot of locking cost to most programs (unless each socket is truly independent). select() is portable, reasonable well performing (except in extreme cases), and a very simple model that integrates well with game loops, event loops, or pretty much anything else.
enum Bool { True, False, FileNotFound };
ok thanks, I haven't come across the select() function before, so ill read up on it.

This topic is closed to new replies.

Advertisement