Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets 为什么在多次读取后套接字速度变慢?_Sockets_Tcpclient_Tcp Ip - Fatal编程技术网

Sockets 为什么在多次读取后套接字速度变慢?

Sockets 为什么在多次读取后套接字速度变慢?,sockets,tcpclient,tcp-ip,Sockets,Tcpclient,Tcp Ip,我编写了一个简单的测试客户端(VB.Net),它使用TcpClient类通过公司网络向硬件设备发送一个小的“命令”字节数组(6或7字节)。设备以字节数组(约48kb)响应,我的客户端应用程序读取该数组。我知道返回的数组的确切长度,因此我在循环中反复读取套接字,直到获得所有内容:- Dim read_data() As Byte Dim stream As NetworkStream = tcpClient.GetStream() ' Send the "command" stream.Wri

我编写了一个简单的测试客户端(VB.Net),它使用TcpClient类通过公司网络向硬件设备发送一个小的“命令”字节数组(6或7字节)。设备以字节数组(约48kb)响应,我的客户端应用程序读取该数组。我知道返回的数组的确切长度,因此我在循环中反复读取套接字,直到获得所有内容:-

Dim read_data() As Byte

Dim stream As NetworkStream = tcpClient.GetStream()

' Send the "command"
stream.Write(data, 0, commandByteArray)

' Read the response
Using ms As New MemoryStream()
    Dim sw As Stopwatch = New Stopwatch()
    sw.Start()

    Dim temp_read(16384) As Byte
    Try
        Do
            Dim bytes_read = stream.Read(temp_read, 0, temp_read.Length)
            ms.Write(temp_read, 0, bytes_read)
        Loop While ms.Length < expectedResponseSize
    Catch ex As IOException
        ' No more data
    End Try

    read_data = ms.ToArray()

    Debug.WriteLine(sw.ElapsedMilliseconds)
End Using
Dim read_data()作为字节
Dim stream As NetworkStream=tcpClient.GetStream()
'发送“命令”
stream.Write(数据,0,commandByteArray)
“阅读回答
将ms用作新内存流()
作为秒表的Dim sw=新秒表()
sw.Start()
Dim temp_读取(16384)为字节
尝试
做
Dim bytes_read=stream.read(temp_read,0,temp_read.Length)
ms.Write(临时读取,0,字节读取)
在ms.Length
此代码连接到一个按钮单击事件。每次我运行它时,秒表显示平均需要5ms才能读回48kb字节数组。但是,如果我每秒重复单击按钮几次,几秒钟后秒表报告的时间开始增加,并最终稳定在大约50ms。如果我断开连接,重新连接并重试,则时间将返回5毫秒(直到我开始再次快速单击按钮)

知道是什么引起的吗?我假设它是网络协议或网络硬件中的某种东西,例如,使连接适应增加的数据吞吐量


编辑:如下面的评论所述,我尝试过禁用唠叨(
socket.NoDelay=true
),但没有效果,尽管我的理解是这更有可能提高非常小的消息的吞吐量,而不是像这样的大消息。

您发送命令的频率是多少?是否启用唠叨?这是我在这里能想到的唯一问题。@usr这些命令目前是通过点击按钮触发的,因此通常不会超过几秒钟,但问题甚至在每秒发送(比如)一个命令时也会显现出来。重复唠叨,如果你的意思是将套接字的
NoDelay
设置为True,那么一开始它确实会加快一些速度(3-4ms,而不是5ms),但是仍然会发生减速,再次降低到50ms左右。