tags:

views:

307

answers:

3

Hi, 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

+1  A: 

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".

Jon Skeet
A: 

I don't know how to tell it what the 1st piece of data is....such as the client has to enter there name first, thats what I want to store.

Please respond with comments, not extra answers. You'll need to design the protocol so that you *do* know how to tell what the first piece of data is. Make the client send some sort of delimiter, or the length of the field before the content.
Jon Skeet
A: 

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
magnifico