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

I am writing an instant messaging server in C # for training. My question is whether to use synchronous or asynchronous sockets to handle IM clients. The goal is to handle as many customers as possible.

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

About synchronization sockets: Are synchronous sockets a good solution for many clients? Do I need to check every socket / connection in a loop if there are new packets? If so, isn't it so slow?

Last question: suppose I want to implement a way to send files (for example, images) via a protocol (which is a non-standard binary protocol), can I send messages at boot time?

+4
source share
3 answers

The goal is to handle as many customers as possible.

Async then. It scales much better.

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

TCP ensures that everything is in order.

Suppose I want to implement a way to send files (e.g. images) over a protocol (which is a non-standard btw binary protocol), can I send messages at boot time

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

+8
source

Everything @jgauffin said (i.e. TCP handles batch order, better async for n(clients) > 1000 ).

Suppose I want to implement a way to send files (for example, images) via a protocol (which is a non-standard btw binary protocol), can I send messages at boot time?

To do this, create your own protocol. If you write an 8 MB packet to Socket, you cannot write anything with this socket until 8 MB is sent. Instead, use smaller loading chunks so that other bags can move through the pipe.

 [UPLOAD id=123 START length=8012389] [UPLOAD id=123 PART chunk=1 length=2048 data=...] [UPLOAD id=123 PART chunk=2 length=2048 data=...] [MESSAGE to=" foo@example.com " text="Hi"] [UPLOAD id=123 PART chunk=3 length=2048 data=...] // ... [UPLOAD id=123 COMPLETE checksum=0xdeadbeef] 
+4
source

The difference between the asynchronous approach and the synchronization approach is more related to the difference between non-locking and io locking. In both approaches, the data is transmitted in the same order in which it was transmitted. However, you are not blocking while you wait for the asynchronous call to complete, so you can start sending to all your clients before any of the individual messages finish writing to the socket (which is why this will usually be the approach followed by the servers).

If you follow the synchronization route, you block until each send / receive operation is completed, which means that you may need to start several threads to process clients.

As for downloading the image at the same time as sending messages, you can handle this other channel connection between the client / server so that it does not cause blocking.

+2
source

Source: https://habr.com/ru/post/1344600/


All Articles