Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Question about socket programming (sync or async)

I'm writing an instant messaging server in C# for learning purposes. My question is whether I should use synchronous or asynchronous sockets to handle the IM clients. The goal is to handle as many clients as possible.

I'm not quite sure but as far as I know with async sockets the packets don't arrive in order which means when you send 2 chat messages and there is a delay/lag it's possible that the second one arrive before the first one. Is this right and if so, is there a way to solve this issue?

About sync sockets: Is synchronous sockets a good solution for many clients? Do I have to check every socket/connection in a loop if there are new packets? If so, isn't this quite slow?

Last question: Assume I want to implement a way to send files (e.g. images) through the protocol (which is a non-standard binary protocol btw), can I still send messages while uploading?

like image 688
Eliasdx Avatar asked Dec 10 '22 10:12

Eliasdx


1 Answers

The goal is to handle as many clients as possible.

Async then. It scales a lot better.

I'm not quite sure but as far as I know with async sockets the packets don't arrive in order which means when you send 2 chat messages and there is a delay/lag it's possible that the second one arrive before the first one.

TCP guarantees that everything arrives in order.

Assume I want to implement a way to send files (e.g. images) through the protocol (which is a non-standard binary protocol btw), can I still send messages while uploading

I recommend that you use a separate connection for file transfers. Use the first connection to do a handshake (determine which port to use and specify file name etc). Then use Socket.SendFile on the new socket to transfer the file.

like image 163
jgauffin Avatar answered Dec 31 '22 06:12

jgauffin