what's wrong with this code?

korae

Member
Joined
Jan 24, 2010
Messages
22
Programming Experience
Beginner
Hello everyone! I'm having problems with these codes I modify, it doesn't seem stable. I mean sometimes the program just exits while running, I get errors about threading and I.O.Exceptions. I'm new to visual basic and having difficulty debugging, I'm using vb 2008 express edition. Anyone who can tell me what I did wrong? Or is there something missing? My project is something like a server/client application where the client sends information to the server and the server displays that information.
 

Attachments

  • code.txt
    8.9 KB · Views: 21
Last edited:
I get errors about threading and I.O.Exceptions.
One of the most important things to learn when you start with VB:
The exact error message (and if known the line which throws the error) helps much in finding the error. Both yourself and others ;)
 
One of the most important things to learn when you start with VB:
The exact error message (and if known the line which throws the error) helps much in finding the error. Both yourself and others ;)

I get a lot of these:

A first chance exception of type 'System.IO.IOException' occurred in System.dll

and they seem to add up every time I run the project

In this code whenever the client disconnects, client still remains in the listbox, it will not remove the client in the listbox. the clients username is its IP

VB.NET:
Public Sub removeClient(ByVal client As ConnectedClient)
        If clients.Contains(client) Then
            clients.Remove(client)
            Dim x As Single
            For x = 0 To lbClients.Items.Count - 1
                If lbClients.Items.Item(x) = client.Username Then RemoveClientFromListBox(x) : Exit For
            Next
        End If
    End Sub
    Private Sub RemoveClientFromListBox(ByVal IndexOfClient As String)
        If lbClients.InvokeRequired Then
            Me.Invoke(New StringDelegate(AddressOf RemoveClientFromListBox), IndexOfClient)
        Else
            lbClients.Items.Remove(lbClients.Items.Item(Val(IndexOfClient)))
        End If
    End Sub

    Private Sub MessageReceived(ByVal sender As ConnectedClient, ByVal Message As String)

        Dim data() As String = Message.Split("|"c)
        Select Case data(0)
            Case "/CONNECT"
                If GetClientByName(data(1)) Is Nothing Then
                    sender.Username = data(1)
                    AddClientToListBox(data(1))
                End If
                sender.SendMessage("/Connected")
            Case "/DISCONNECT"
                removeClient(sender)
            Case Else
                MeterData(Message & vbCrLf)
        End Select
    End Sub
 
First chance exceptions in the debugger: In short words: Don't care! In long words:
First and second chance exception handling

On not removing the client from the list:
/DISCONNECT message DOES occur? Put a debug.print in to see if.
"listbox.contains" returns true for a client that is in the list?
Dont cycle through a list with For and then use RemoveAt! IF you do it, loop through the list from back to front and not vice versa. Remember that RemoveAt CHANGES the listbox.items collection!
 
Back
Top