简体   繁体   English

TCP / IP消息帧

[英]TCP/IP Message Framing

I have made a TCP/IP Server/Client, its asynchronous but it concatenates the messages. 我已经建立了一个TCP / IP服务器/客户端,它是异步的,但它连接了消息。 How do I correctly go about adding a header at the start and then use a string builder at the end to un-concatenate complete messages. 我如何正确地在开始时添加标题,然后在最后使用字符串构建器来取消连接完整的消息。

Server Read Message: 服务器读取消息:

  Private Sub ReadCallback(ByVal result As IAsyncResult)
    Try
        allDone.Set()
        Dim success As Boolean = result.AsyncWaitHandle.WaitOne(500, True)
        If success Then
            Dim client As ServerClient = TryCast(result.AsyncState, ServerClient)
            If client Is Nothing Then
                Return
            End If
            Dim networkStream As NetworkStream = client.NetworkStream
            Dim read As Integer = networkStream.EndRead(result)
            If read = 0 Then
                SyncLock Me.Clients
                    Me.Clients.Remove(client.ClientID)
                    Return
                End SyncLock
            End If
            If client.NetworkStream.CanRead Then

                dataString.Append(Me.Encoding.GetString(client.buffer, 0, read))

                networkStream.BeginRead(client.buffer, 0, client.buffer.Length, AddressOf ReadCallback, client)
                allDone.WaitOne(500, True)
            End If
        End If
    Catch ex As IO.IOException
        Dim client As ServerClient = TryCast(result.AsyncState, ServerClient)
        SyncLock Me.Clients
            Me.Clients.Remove(client.ClientID)
            Return
        End SyncLock
    Catch ex As Exception
        If Not Me.tcpListener.Server.Connected Then
            Return
        End If
    End Try
End Sub

Client Write Message: 客户写信息:

    Public Function Write(value As String, encoding As Encoding) As Guid
    Dim buffer As Byte() = encoding.GetBytes(value)
    Return Me.Write(buffer)
End Function

Public Function Write(buffer As Byte()) As Guid
    Dim guid__1 As Guid = Guid.NewGuid()
    Dim networkStream As NetworkStream = Me.client.GetStream()
    Dim result As IAsyncResult = networkStream.BeginWrite(buffer, 0, buffer.Length, Nothing, guid__1)


    result.AsyncWaitHandle.WaitOne()
    networkStream.EndWrite(result)
    Return guid__1
End Function

You need to define a (probably thin and simple) protocol on top of TCP/IP to allow you to know where one message starts and ends. 您需要在TCP / IP之上定义(可能很简单和简单)协议,以便您知道一条消息的开始和结束位置。 TCP/IP can and will fragment the message you send such that the receiver could get part of a message, a whole message, or multiple messages. TCP / IP可以并且将对您发送的消息进行分段,以便接收方可以获取消息,整个消息或多条消息的一部分。 A simple approach is to write a message length, followed by the message. 一种简单的方法是编写消息长度,然后是消息。 The receiver then reads into a byte buffer, and once the appropriate amount of bytes (based on the sent length) has been received the message can be pulled out and encoded into a string. 接收器然后读入字节缓冲器,并且一旦接收到适当的字节量(基于发送的长度),就可以拉出消息并将其编码成字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM