Proper sending and receiving via C# sockets?

前端 未结 3 911
轮回少年
轮回少年 2021-01-28 20:58

I\'m attempting to create a remote desktop server and client using C#. The server captures the screen and then sends it to the client via a socket. I\'m using the code below alt

3条回答
  •  有刺的猬
    2021-01-28 21:40

    You need to add application level protocol to your socket communications.

    Add a header to all messages sent. The header contains the count of bytes that follow. It is simpler code and performs better to have a byte count than to kludge a termination sequence.

    The client then does two sets of reads: 1) Read for the number of bytes that are known to be in any header.

    2) After extracting the byte count from the header, loop reading until you get the indicated byte count.

    A must-read article for all people who are writing socket communications: http://nitoprograms.blogspot.com/2009/04/message-framing.html From that article: Repeat this mantra three times: "TCP does not operate on packets of data. TCP operates on streams of data."

提交回复
热议问题