Question When a TCP byte(string) is received, it writes it, then scrolls down a bunch

Xolitude

New member
Joined
May 26, 2013
Messages
4
Programming Experience
1-3
Ok so subject says it all...


When I write a byte to the tcpserver, the console will print it out, but if there is any text after the returned byte, it scrolls down, also even if there is no string after it.


SERVER:
VB.NET:
Imports System.Net.Sockets
Imports System.Text


Module Module1
    Dim server As TcpListener
    Dim client As TcpClient
    Sub Main()
        server = New TcpListener(8000)
        server.Start()


        Console.WriteLine("Waiting...")
        While True
            client = server.AcceptTcpClient
            Dim networkStream As NetworkStream = client.GetStream()


            Dim bytes(client.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))


            Dim clientdata As String = Encoding.ASCII.GetString(bytes)
            Console.WriteLine(("Client sent: " + clientdata))
        End While
    End Sub


End Module


CLIENT:
VB.NET:
Imports System.Net.Sockets
Imports System.Text


Public Class Form1
    Dim tcpClient As New System.Net.Sockets.TcpClient()
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        tcpClient.Connect("127.0.0.1", 8000)
        Dim networkStream As NetworkStream = tcpClient.GetStream()


        Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
        networkStream.Write(sendBytes, 0, sendBytes.Length)
    End Sub
End Class

I've always used this same code and it's never scrolled all the way down like this before.. any help?
 
Your server code is decoding lots of 0-bytes from your buffer array. NetworkStream.Read is a function and return value is the number of bytes actually read, you have to use this number when you call GetString to only decode the actual data received, excluding all your 'unallocated' 0 value bytes.
 
Your server code is decoding lots of 0-bytes from your buffer array. NetworkStream.Read is a function and return value is the number of bytes actually read, you have to use this number when you call GetString to only decode the actual data received, excluding all your 'unallocated' 0 value bytes.

Uhm... ok? So the help is..
 
What code do you have now in server?
 
Back
Top