遇到问题连续发出三个或三个以上的数据包 [英] Having trouble sending three or more packets consecutively

查看:315
本文介绍了遇到问题连续发出三个或三个以上的数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用来实现客户机/服务器模式的TcpClient ,其 Networkstream.Write()/读取()函数发送/接收的字节数组。

I am trying to implement a Client/Server model using TCpClient, with its Networkstream.Write()/Read() functions sending/receiving a byte array.

它的工作原理大部分的时间,但如果我尝试另一个之后发送三个或更多的字节数组成一排。客户说,它发送所有这些,但服务器只接收到第2位。

It works most the time, except if I try to send three or more byte arrays in a row right after one another. The client says it sends them all, but the server only receives the first two.

下面是code我用它来从客户端写入服务器。

Below is the code I use to write from client to server.

byte[] buffer = p.toByteArray(level);

stream.Write(buffer, 0, buffer.Length);
stream.Flush();

难道是巩固他们的东西?我只是不明白是怎么服务器可以接收不同的阵列时,我送2,而不是3个或更多。如果我分出3写,它的作品不错,但我真的不想这样做。

Is it consolidating them or something? I just don't understand how the server can receive distinct arrays when I send 2, but not 3 or more. If I separate the 3 writes, it works OK, but I really don't want to do that.

任何帮助将是非常美联社preciated。

Any help would be much appreciated.

编辑:

解决:) 感谢您的球员的帮助。它是推动在一个时间2-3个包,我的系统在想1爆= 1包。我只是改写了我与的TcpClient现有的架构来:)再次检测多个数据包,感谢您的帮助!

SOLVED :) Thanks for all your guys help. It was pushing 2-3 packets at a time, and my system was thinking 1 burst = 1 packet. I just rewrote my existing architecture with TCPClient to detect multiple packets :) Again, thanks for the help!

推荐答案

有一个非常重要的核心规则,重要的是要考虑当你做套接字编程:

There is one very important core rule that is important to account for when you do Sockets programming:

不保证,无论客户在X中发送写道,服务器将完全接收相同数量的读取。它可以是一个写在客户端和10读取上的服务器。它可以是10写入和客户端和仅仅一个读服务器

It is not guaranteed that whatever client sent in X writes, server will receive exactly in the same amount of reads. It can be one write on client and 10 reads on server. It can be 10 writes and client and just one read on server.

假设客户端发送3条信息,每条100个字节。服务器可能会收到150字节,然后还有150个字节。或100字节,200字节。

Let's say the client sends 3 messages, 100 bytes each. Server might receive 150 bytes and then another 150 bytes. Or 100 bytes and 200 bytes.

如果您使用TCP保证的唯一的事情是,为了将preserved,因为无论你发送将首先在服务器上初来乍到换句话说。

The only thing that is guaranteed if you work with TCP is that the order will be preserved, in other words that whatever you sent first will arrive first on the server.

您可以使用下面的基本技术之一分隔数据:

You can use one of the following basic techniques to separate the data:

  • 标记(某种字节序列中界定的消息)
  • 在每封邮件的一定长度
  • 长度在邮件标题
  • 上述
  • 组合

这篇关于遇到问题连续发出三个或三个以上的数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆