Markus
Markus

Reputation: 123

C# game client with asyncronous sockets

I'm developing a small online game in C#. Currently I am using simple sync TCP sockets. But now (because this is some kind of "learning project") I want to convert to asynchronous sockets. In the client I have the method: byte[] SendAndReceive(Opcode op, byte[] data). But when I use async sockets this isn't possible anymore. For example my MapManager class first checks if a map is locally in a folder (checksum) and if it isn't, the map will be downloaded from the server.

So my question: Is there any good way to send some data and get the answer without saving the received data to some kind of buffer and polling till this buffer isn't null?

Upvotes: 1

Views: 2087

Answers (2)

deepee1
deepee1

Reputation: 13216

Check out IO Completion Ports and the SocketAsyncEventArgs that goes with it. It raises events when data has been transferred, but you still need a buffer. Just no polling. It's fast and pretty efficient.

http://www.codeproject.com/Articles/83102/C-SocketAsyncEventArgs-High-Performance-Socket-Cod

and another example on MSDN

http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx

Upvotes: 2

Christopher Bales
Christopher Bales

Reputation: 1071

A code example of what you have would help, but I'd suggest using a new thread for each socket connection with a thread manager. Lmk if that makes sense or if that' applicable here. :)

Upvotes: 0

Related Questions