且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过C#套接字正确发送和接收?

更新时间:2022-03-02 08:39:39

您需要在套接字通信中添加应用程序级协议。

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.

然后客户端执行两组读取:
1)读取已知在任何头中的字节数。

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

2)从标题中提取字节数后,循环读取,直到得到指定的字节数。

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

所有正在编写套接字通信的人都必读的文章: http:// nitoprograms.blogspot.com/2009/04/message-framing.html
从那篇文章:重复这个咒语三次:TCP不对数据包进行操作.TCP对数据流进行操作。

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."