Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I've been working on a client and a server application, at the moment it simply sends some text to the specified IP.

It all works fine, except instead of just sending the text it sends a total of 8193 bytes. I've been trying to figure out why, but have not yet found out why.

Any ideas would be greatly appreciated :)
Thanks,
Lloyd

Client:
Imports System.Net.Sockets
Imports System.Text
Module Module1

    Sub Main()
        Dim Data As [Byte]() = Encoding.ASCII.GetBytes(Console.ReadLine)
        Try
            Dim TCP As New System.Net.Sockets.TcpClient
            Console.Write("Enter address: ")
            Dim InputAddress As String
            InputAddress = Console.ReadLine
            TCP.Connect(InputAddress, 1000)
            Dim Stream As NetworkStream = TCP.GetStream
            Stream.Write(Data, 0, Data.Length)
            TCP.Close()
            Console.ReadLine()
        Catch ex As Exception
            Console.WriteLine("Error: " + ex.Message)
            Console.ReadLine()
        End Try
End Sub
End Module


Server:
Imports System.Net.Sockets
Imports System.Text
Module Module1

    Sub Main()
        Dim TCPWait As New TcpListener(1000)
        TCPWait.Start()
        Console.WriteLine("Waiting for connection...")
        Try
            Dim Client As TcpClient = TCPWait.AcceptTcpClient
            Console.WriteLine("Connection established!")
            Dim Stream As NetworkStream = Client.GetStream
            Dim Buffer(Client.ReceiveBufferSize) As Byte
            Stream.Read(Buffer, 0, CInt(Buffer.Length))

            Dim RecievedData As String = Encoding.ASCII.GetChars (Buffer)
            Console.WriteLine(RecievedData.Length)
            Console.WriteLine("Client: " + RecievedData)
            Console.ReadKey()
            Client.Close()
            Console.ReadLine()

        Catch ex As Exception
            Console.WriteLine("Error: " + ex.Message)
        End Try
    End Sub

End Module
Posted
Comments
Henry Minute 29-Nov-10 7:32am    
Is the minimum packet size set to that figure on the network you are using?
LloydA111 29-Nov-10 7:52am    
I think so

1 solution

It's not sending 8000+ characters. You're server is allocating a buffer the size of the ReceiveBufferSize. This is where your 8000+ characters is coming from.

When you call GetChars on your buffer, it's returning all 8000+ characters, not the number of characters that was actually received.
 
Share this answer
 
Comments
LloydA111 29-Nov-10 10:21am    
Ah I see! Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900