TCP 服务器在 .NET 中发送速度太快

发布于 2024-10-08 14:01:12 字数 404 浏览 0 评论 0原文

我正在 .NET 中制作一个客户端-服务器应用程序,但现在我遇到了一个问题:我尝试发送某些内容,客户端正在读取它,而在读取时我正在发送其他内容,因此它使发送的数据变得很糟糕。

示例:

  1. 我发送 test
  2. 客户端开始读取
  3. 我发送 endtest
  4. 客户端读取 test@$>@est

有没有办法解决这个问题?现在我在发送后执行 Thread.Sleep(10) 但我认为有更好的方法:P。

PS我使用的是NetworkStream.Write()/Read(),而不是NetworkStream.BeginWrite()/BeginRead()。这两个哪个更好,为什么?

I'm making a client-server app in .NET, but now I got the problem that I try to send something, the client is reading it and while reading I'm sending something else so it makes crap of the sent data.

Example:

  1. I send test
  2. Client starts reading
  3. I send endtest
  4. Client reads test@$>@est

Is there a way to solve this? Now I'm doing a Thread.Sleep(10) after a send but I think there is a better way :P.

P.S. I'm using NetworkStream.Write()/Read(), not NetworkStream.BeginWrite()/BeginRead(). Which of these two is better and why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神妖 2024-10-15 14:01:12

问题可能是您正在使用同一个套接字发送多个数据“事物”。您可能知道您已经发送了一个字符串,然后发送了一个图像,但是,TCP 不会看到单独的对象,只能看到单个字节流。

您也不知道需要进行多少次读取调用才能获取“第一个”数据对象。描述多个对象超出了 TCP 的范围。关键是,这个问题最好通过每个事务使用一个套接字来解决。创建一个套接字,通过一次写入调用将全部数据写入其中,然后关闭套接字。让客户阅读,直到获得完整的数据。如果您需要发送其他内容,请创建一个新套接字。

我知道这不是 NetworkStream 类特有的,但我希望它有帮助。

The problem may be that you are using the same socket to send multiple "things" of data. You may know that you have sent a string and then an image, but, TCP doesn't see separate objects, only a single stream of bytes.

You also cannot know how many read calls you'll need to make to get your "first" data object. To delineate multiple objects is outside the scope of TCP. The point being, this problem is best solved by using one single socket per transaction. Create a socket, write your entire data to it in a single write call, then close the socket. Have the client read until you have the entirety of the data. If you need to send something else, make a new socket.

I know this isn't specific to the NetworkStream class, but I hope it helps.

安稳善良 2024-10-15 14:01:12

您应该考虑将 WCF 与 nettcpbinding 一起使用来处理此类内容,特别是如果两端(客户端/服务器)都使用 C#。

我以前在编写自定义 tcp 代码时遇到过类似的问题,但后来转向了 wcf,它明显更容易、更稳定,因此您可以专注于编写应用程序代码。

you should look into using WCF with nettcpbinding for this sort of stuff especially if both ends (client / server) are in C#.

I previously had similar problems writing custom tcp code but moved over to wcf and its significantly easier, and more stable so you can just focus on writing your application code.

柒夜笙歌凉 2024-10-15 14:01:12

如果我是你,我会在发送和接收数据时创建一条规则。假设您有一个像 $ 这样的起始字符,而 * 是结束字符。这将帮助您的客户端/服务器系统知道何时读取或何时写入,并最终避免在此过程中产生垃圾。
所以就步骤而言:

  • 客户端发送消息 $hello world*
  • 服务器在看到 $ 时开始读取并在 * 之后停止

然后再一次,也许您正在做的事情的代码示例可以帮助我们准确地查明您做错了什么。

If I were you I would create a rule, when sending and receiving data. As to say you have a starting char like $ for example and * is the ending char. That will help your client/server system to know when to read or when to write, and eventually avoid garbage in the process.
so in terms of steps:

  • Client sends message $hello world*
  • Server starts to read when it sees the $ and stops after the *

then again, maybe a code example of what you are doing could help us pinpoint exactly what you are doing wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文