VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • 优化VB.NET中的TCP / IP套接字实现

在两台联网的计算机之间进行连接时,我要尝试查找原因,导致〜40ms延迟。 我怀疑服务器应用程序是原因。 我已经使用其他软件(即Hercules)检查了延迟是否与软件有关,并且我考虑了传输位而不是字符串。 拥塞可能不是原因,但可能是缓冲区和数据包大小-考虑到我在一些早期RFC文档中读到的内容。 我认为延迟是由于服务器而不是客户端,尽管这只是我的猜测。 我在ActionScript中对客户端进行了编程。

我问这个问题,因为在关键字TCP / IP上进行了谷歌搜索,而优化会返回有关TCP / IP配置而不是实现的结果。 我是TCP / IP编程的新手,所以我希望对于更高级的程序员来说,错误很容易发现。

服务器应用程序是在VB.NET中编程的:

Public Class Form1

Public Shared remoteAddress As IPAddress = IPAddress.Any
Public Shared port As Int32 = 8080

Public Shared server As New TcpListener(remoteAddress, port)
Public client As TcpClient = Nothing

'....


While (True)
        Try

            Dim buffer(65536) As Byte

            Dim stuff As Integer = client.ReceiveBufferSize
            Console.WriteLine(stuff)

            networkStream.Read(buffer, 0, CInt(client.ReceiveBufferSize))

            Dim dataFromClient As String = System.Text.Encoding.ASCII.GetString(buffer)

            dataFromClient = dataFromClient.Substring(dataFromClient.IndexOf("/") + 1, dataFromClient.IndexOf("\") - 1)


            If dataFromClient Like "/END\" Or dataFromClient Like "/STOP\" Then
                Console.WriteLine("STOP REQUESTED")

                Exit While
            End If

            SetMarker(dataFromClient)

            Console.WriteLine(dataFromClient)

            Dim serverResponse As String = "Receiving marker #" + Convert.ToString(requestCount) + ":  " + dataFromClient
            Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(serverResponse)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            networkStream.Flush()
            Console.WriteLine(serverResponse)

        Catch illegalSyntax As System.ArgumentException
            Console.WriteLine("ERROR: Use correct syntax: /command\ Example: /S1\, /STOP\")
        Catch socketEx As SocketException
            Console.WriteLine(SocketError.Interrupted)

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        requestCount = requestCount + 1
    End While

相关教程