Netstream buffers

bsaxberg

New member
Joined
Nov 19, 2009
Messages
3
Location
North Dakota
Programming Experience
10+
From reading through this thread I was able to get a client server app working successfully. Thank you!

My question is this, if I pass a string of text from the client to the server what is filled in the stream after the text?

Here is the main code in the client. You can see it is getting the byte length to send from the Encoding.ASCII.GetBytes call.
VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If tbSend.Text.Length > 0 Then

            Dim tcp As New TcpClient
            tcp.Connect("127.0.0.1", 8765)
            Dim networkStream As NetworkStream = tcp.GetStream()
            If networkStream.CanWrite And networkStream.CanRead Then
                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(tbSend.Text)
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                Dim bytes(tcp.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0, CInt(tcp.ReceiveBufferSize))
                Dim returndata As String = Encoding.ASCII.GetString(bytes)
                tbReceive.Text = returndata
            Else
                If Not networkStream.CanRead Then
                    tbStatus.Text = "cannot not read data to this stream"
                    tcp.Close()
                Else
                    If Not networkStream.CanWrite Then
                        tbStatus.Text = "cannot write data from this stream"
                        tcp.Close()
                    End If
                End If
            End If

        Else
            MsgBox("Enter Some data to send", MsgBoxStyle.OkOnly)
        End If
    End Sub

On the Server end I am catching it and it is saying that my Buffer Length is 8193 when what was passed was only an 8 character long string. What is the extra data in the stream and how can I trim it off so I only see the string that was sent?

Here is the server code.
VB.NET:
Private Sub accepting(ByVal ar As IAsyncResult)
        'receives filename, filelength, filebytes
        Dim listener As TcpListener = CType(ar.AsyncState, TcpListener)
        Dim clientSocket As TcpClient = listener.EndAcceptTcpClient(ar)
        Dim netstream As NetworkStream = clientSocket.GetStream()
        Dim bytesFrom(clientSocket.ReceiveBufferSize) As Byte
        Dim NewUser As String = ""
        netstream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
        Dim dataFromClient As String = System.Text.Encoding.ASCII.GetString(bytesFrom)
        Dim serverResponse As String = ""
        MsgBox(dataFromClient.Length.ToString)
        If dataFromClient.ToString = "OpenGame" Then
            MsgBox("True")
        End If
        If dataFromClient.ToString.Substring(0, 8) = "OpenGame" Then
            serverResponse = GameInPlay.GameID.ToString
        ElseIf dataFromClient.StartsWith("DisplayName=") Then
            NewUser = dataFromClient.Substring(0, 11)
            For Each con In ConList
                If NewUser = con.DisplayName Then
                    Dim n As Integer = dgvContestants.Rows.Add()
                    dgvConnections.Columns.Item(0).Name = "Contestant"
                    dgvContestants.Rows(n).Cells(0).Value = con.DisplayName
                    serverResponse = "Contestant Added"
                    Exit For
                End If
            Next
        Else
            serverResponse = "Unknown Parameter"
        End If


        Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(serverResponse)
        netstream.Write(sendBytes, 0, sendBytes.Length)
        netstream.Flush()

        clientSocket.Close()
    End Sub

I openly admit I am new to Sockets.

Thank you,
Brian
 

Robert_Zenz

Well-known member
Joined
Jun 3, 2008
Messages
503
Location
Vienna, Austria
Programming Experience
3-5
BufferSize is not equal to the amount of data in the stream.

The Networkstream has the DataAvalaible-property, which tells you that there's data to read, and the Read-Function itself will tell you how many bytes it just fetched into the array.

Bobby
 

bsaxberg

New member
Joined
Nov 19, 2009
Messages
3
Location
North Dakota
Programming Experience
10+
Yes, I understand that but can you tell me what type of data fills the buffer past the 8 characters I'm looking for? As the information in the stream does not equal just the string I'm looking for, I can't seem for the life of me to figure out how to strip off the excess byte information from the string variable after it is set to the stream. It is like I don't know where the stop bit is for the valid information.

The basic idea of using the client/server with sockets would be to send commands back and forth from the server to the clients for a game. This isn't a turn based game, it is one based off of jeopardy and I need to keep track of who "buzzes" in first from the clients to the server. I've got the rest of the game pretty much figured out, it is this "buzzing" mechanism I have to get working next.

I could incorporate some kind of stop bit character but that doesn't seem like the most elegant solution to me. It may work in the time frame needed though.

I thank you for stoking the thought process.

Thank you!
Brian
 

Robert_Zenz

Well-known member
Joined
Jun 3, 2008
Messages
503
Location
Vienna, Austria
Programming Experience
3-5
There is no data behind the available one...

Example:

VB.NET:
Dim buffer() As Byte = {0,1,2,3,4,5,6}
Dim readBytes As Integer = 0

readBytes = netStream.Read(buffer, 0, buffer.Length)

' readBytes will now contain the total amount of read bytes, let's say three...
' our array will now look like this:
'  buffer => 255, 254 , 253, 4 , 5, 6
' Data [b]behind[/b] will not be touched...there is just nothing beyond the avalaible
' data in the NetworkStream.

str = Encoding.ASCII.GetString(buffer, 0, readBytes)

Bobby
 

bsaxberg

New member
Joined
Nov 19, 2009
Messages
3
Location
North Dakota
Programming Experience
10+
Thank you for your replies and information. In the sense of time constraints I will have to worry about sockets and netstreams later. Will have to get this first version done with another method.
 
Top Bottom