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

喜夏-厌秋 提交于 2021-01-27 06:43:40

问题


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?


回答1:


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.




回答2:


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

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?

Your custom protocol has to be built to support this. If you write a 8MB packet to the Socket, you won't be able to write anything else using that socket until the 8MB are sent. Instead, use upload-chunks of smaller size so that other packets have the chance to go over the pipe as well.

[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]



回答3:


The difference between an async approach and a sync approach is more about the difference between non-blocking and blocking io. With both approaches, the data is delivered in the same order that it has been transmitted. However, you don't block while you wait for an async call to complete, so you can start transmitting to all of your clients, before any of the individual communications has finished writing to the socket (which is why typically it would be the approach followed by servers).

If you go down the sync route, you block until each transmission / receive operation has completed, which means you may require need to run multiple threads to handle the clients.

As far as uploading an image at the same time as sending messages, you may want to handle that down a different pipe connection between the client/server so that it doesn't cause a blockage.



来源:https://stackoverflow.com/questions/5377779/c-question-about-socket-programming-sync-or-async

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!