Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Dim serverSocket As New TcpListener(1200)
        Dim clientSocket As TcpClient
        Dim counter As Integer

        serverSocket.Start()
        msg("Chat Server Started.")
        counter = 0

        While (True)
            counter += 1
            clientSocket = serverSocket.AcceptTcpClient()

            Dim bytesFrom(10024) As Byte
            Dim dataFromClient As String
            Dim networkStream As NetworkStream = _
            clientSocket.GetStream()
            networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
            dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
            dataFromClient = dataFromClient.IndexOf("$")

            clientsList(dataFromClient) = clientSocket

            broadcast(dataFromClient + " Joined ", dataFromClient, False)

            msg(dataFromClient + " Joined chat room ")
            Dim client As New handleClinet
            client.startClient(clientSocket, dataFromClient, clientsList)
        End While


Says:
Length cannot be less than zero.
Parameter name: length
Please Help!
Posted
Updated 24-Aug-13 5:22am
v2
Comments
Richard MacCutchan 24-Aug-13 11:15am    
Add some proper error checking in your code. Do not assume that every method call will return a good value.

It seems like that dataFromClient does not contains "$" and that's why
dataFromClient.IndexOf("$")
is returning -1 which causing
dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
to throw "Length cannot be less than zero" exception.

Please debug and check it.
 
Share this answer
 
I made it a little better for you to understand.
 
Share this answer
 

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