TCP/IP 消息帧 [英] TCP/IP Message Framing

查看:104
本文介绍了TCP/IP 消息帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个 TCP/IP 服务器/客户端,它是异步的,但它连接了消息.我如何正确地在开头添加标题,然后在结尾使用字符串构建器来取消连接完整的消息.

I have made a TCP/IP Server/Client, its asynchronous but it concatenates the messages. 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.

服务器读取消息:

  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

客户写消息:

    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

推荐答案

您需要在 TCP/IP 之上定义一个(可能是简单的)协议,以允许您知道一条消息的开始和结束位置.TCP/IP 可以并且将会对您发送的消息进行分段,以便接收者可以获取消息的一部分、整个消息或多条消息.一个简单的方法是写一个消息长度,然后是消息.然后接收器读入字节缓冲区,一旦接收到适当数量的字节(基于发送的长度),就可以将消息拉出并编码为字符串.

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 can and will fragment the message you send such that the receiver could get part of a message, a whole message, or multiple messages. 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.

这篇关于TCP/IP 消息帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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