So I have been playing with networking for a while. Now I wanted to make a real client-server application using System.Net.Sockets. The thing is, in real life, there will be on client or another that have a crappy internet connection. So the sever will surely lag badly if everything is done on one thread. Threading is not so simple itself and now I am need to combine it with networking :(. And no, I do not want to use those asynchronous methods! So I want to ask should I receive and send data from/to a client on different threads or should I have a main thread that receives and sends data to all the clients.
Sorry if I sounded like a noob, new to this networking!
This doesn’t make sense - you’re talking about two different concepts; network latency and multi-threaded processing…
Yes, yes it would. It would lag even if someone didn’t have a bad connection if you did it on one thread. Because if one client communicated at the same time as another, it’d have to wait its turn… no matter the internet latency.
You appear to be confusing latency and threading, like @KelsoMRK suggests.
Honestly, threading on the server is easier in my opinion, primarily because you’re not having to sync with the unity thread for anything.
But yeah, the server is going to have its own stuff to do because it’s a completely separate application.
Why?
It makes multi-threading really easy.
Well, personally I would consider using some existing framework out there that would help you manage the threads.
But if you really really want to write the socket server yourself, yeah I’d go with threads.
What you do is have a main thread that opens up a socket and listens for communication, when communication is received that gets spun off into its own thread, and the socket starts listening for the next communication back on the main thread.
Here is an example straight from MSDN:
The only noob thing I read was the refusal to use the async methods… if you want to use threading, asynchronous methods are the way to go.
Actually you are right. I tried them and they made things so much simple! Should have used them before.
Don’t worry. I would surely use a library if I build a multiplayer game. I just wanted to learn it so I am doing it from scratch.
Sorry for that! As I said I am new and am currently super confused.
Thanks for that! That’s what I was about to do. But now that I used async methods I will surely use them. I don’t know why I didn’t want to use them earlier.