1
votes

I'm writing a poker-like game and I'm using winodws sockets for the networking. The server is written in C++ and the client in C#. The server uses the and the client the System.Net/System.Net.Sockets. They have their differences from what I saw.

Ok, so for my send/receive messages functions I use non-blocking sockets. With the server all work perfect. I call my non-blocking send/receive function, I'm sending/receiving all my data perfectly and when there's no data to receive I check the WSAGetLastError and if this error has the famous WSAEWOULDBLOCK error code then I ignore it and move on.

Now, on the client side, if there's no data to receive, then, instead of a function similar to the WSAGetLastError, an exception is thrown, a SocketException with the error code 10035/WSAEWOULDBLOCK. For that, I have my receive function inside a try-catch block. From what I've read, the try-catch block is performance-heavy only when an exception is thrown, if not then it has no serious performance drop. For the message receiving I have a DispatcherTimer with time interval 100ms, it has about 50-60 lines of code inside a try-catch block but the receive is in the second line, and there's where the exception is thrown so it moves to the catch part of the code where there's a simple if block which checks if the exception is NOT a WSAEWOULDBLOCK exception and prints it.

I don't have actually a definitive question I just want some recommendations. My problem:

As soon as the timer start a have a huge performance drop. Is this from the exception throwing? Should I use, instead of a DispatcherTimer, a seperated thread checking for incoming messages? I like the idea of the DispatcherTimer because I don't have to deal with cross-threading operations. Also I sure have read about the Asynchronous sockets but I don't know. Non-blocking sockets seems to me that is a good choice also, for the C++ at least. This C# exception thing is changing the whole thing.

I hope you got pretty much the design of the client. At the server side I'm fine. The client is just a window with a DispatcherTimer runs every 100ms and checks for incoming messages. My problem is performance and simple code design( although I'm not afraid long codes, as long as they are necessary). Thoughts??

1

1 Answers

2
votes

I just used Async sockets and the performance issues disappeared.

Thank you all for your help