将网络流中的第一个字节存储为字符串

发布于 2024-07-11 16:57:54 字数 1457 浏览 42 评论 0原文

我需要将从网络流读取的数据的第一个字节存储为字符串,以便稍后调用它。

prinf("     While 1
        Dim tcpListener As New TcpListener(IPAddress.Any, 80) ' Listen to port given
        Console.WriteLine("Waiting for connection...")
        tcpListener.Start()
        'Accept the pending client connection and return  'a TcpClient initialized for communication. 
        Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
        Console.WriteLine("Connection accepted.")
        ' Get the stream
        Dim networkStream As NetworkStream = tcpClient.GetStream()
        ' Read the stream into a byte array
        Dim bytes(tcpClient.ReceiveBufferSize) As Byte
        networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
        ' Return the data received from the client to the console.
        Dim clientdata As String = Encoding.ASCII.GetString(bytes)
        Console.WriteLine(("Client Sent: " + clientdata))
        ' Return the data received from the client to the console.
        Dim responseString As String = "Hello"
        'Dim chat_name As String = "Name"
        Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
        networkStream.Write(sendBytes, 0, sendBytes.Length)
        Console.WriteLine(("Response: " + responseString))
        tcpClient.Close() 'Close TcpListener and TcpClient
        tcpListener.Stop()
    End While");

这就是我的服务器^一切正常,但我需要存储读取的第一条数据,例如如果我得到 Name ​,它应该存储在数组中

I need to store the first byte of data read from the network stream as a string, so I can call it back later.

prinf("     While 1
        Dim tcpListener As New TcpListener(IPAddress.Any, 80) ' Listen to port given
        Console.WriteLine("Waiting for connection...")
        tcpListener.Start()
        'Accept the pending client connection and return  'a TcpClient initialized for communication. 
        Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
        Console.WriteLine("Connection accepted.")
        ' Get the stream
        Dim networkStream As NetworkStream = tcpClient.GetStream()
        ' Read the stream into a byte array
        Dim bytes(tcpClient.ReceiveBufferSize) As Byte
        networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
        ' Return the data received from the client to the console.
        Dim clientdata As String = Encoding.ASCII.GetString(bytes)
        Console.WriteLine(("Client Sent: " + clientdata))
        ' Return the data received from the client to the console.
        Dim responseString As String = "Hello"
        'Dim chat_name As String = "Name"
        Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
        networkStream.Write(sendBytes, 0, sendBytes.Length)
        Console.WriteLine(("Response: " + responseString))
        tcpClient.Close() 'Close TcpListener and TcpClient
        tcpListener.Stop()
    End While");

Thats my server ^ everything works fine, but I need the 1st piece of data read to be stored, such as if I get "Name" it should be stored in an array

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱我好吗 2024-07-18 16:57:54

您需要准确定义“第一个数据”的含义 - 该数据是否以某种形式分隔(例如 HTTP 标头 - 键/值对由回车换行符分隔)? 长度前缀(如指定 Content-Length 标头时的 HTTP 主体)? 您几乎肯定不只想要第一个字节。

如果您希望只发送名称,然后发送其他内容,而没有任何迹象表明它们是不同的数据位,那么您将会失望。 流只是字节序列 - 没有任何内容(内置)可以说“读取客户端在第一个 API 调用中发送的内容”。

You'll need to define exactly what you mean by "1st piece of data" - is this data delimited in some form (like HTTP headers - key/value pairs are delimited by carriage-return line-feed)? Length-prefixed (like HTTP bodies when the Content-Length header is specified)? You almost certainly don't just want the first byte.

If you were hoping to just send the name and then send something else, without any indication of the fact that they're different bits of data, you're going to be disappointed. Streams are just sequences of bytes - there's nothing (built-in) to say "read what the client sent in their first API call".

美男兮 2024-07-18 16:57:54

这应该有效:

Dim strFirstByte as string = vbNullString
While 1
   ' ... Your code ...

    Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
    If strFirstByte = vbNullString Then strFirstByte = bytes(0).ToString("X2")

    ' ... The rest of your code ...
End While

This should work:

Dim strFirstByte as string = vbNullString
While 1
   ' ... Your code ...

    Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
    If strFirstByte = vbNullString Then strFirstByte = bytes(0).ToString("X2")

    ' ... The rest of your code ...
End While
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文