0 Replies - 252 Views - Last Post: 21 October 2013 - 10:39 AM Rate Topic: -----

#1 Brownbear  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-October 13

vb10 chatserver+client error on client disconnect (advanced)

Posted 21 October 2013 - 10:39 AM

Hello everyone,

i have a problem with some code, it's a chat server/client program using the vb10 build in tcp protocols.
i tried a lot of things but it's a very random error.

error description by the computer: --none-- it just closes the program like you pressed the close button (in windows 7 it triggers the "program doesn't work anymore, searching for a solution" screen)

error description by me:
connecting the client to the server goes perfectly, i can send messages between the server, the client, and even with other clients. the problems start when i try to disconnect (using the client program). sometimes it crashes directly when i press the disconnect button, if it doesn't i try to reconnect to the server, no errors there, and again i can chat. if i then disconnect again (2nd time without closing any of the programs) it sometimes crashes. if it still works (this almost never happens) it will crash after the 3th disconnect.

i have tried searching google and some forums for the life saving tip, but i couldn't find it, i also tried running the client and server in debugging mode, but it seems like the crash is not always at the same exact line, but it varys between about 4 (i have highlighted them).

here are the connection and disconnection codes the server and client use (note: the program is in dutch so some variables are in dutch):

Server code:

form1.vb
Imports System.Net
Imports System.IO
Imports System.Net.Sockets

Public Class form1
    Dim msgstring As String = Nothing
    Dim key As Char = ""
    Dim Client As TcpClient
    Dim poort As Integer
    Dim listener As TcpListener
    Dim newClient As ChatClient
    Dim clientlist As New List(Of ChatClient)
    Dim loaded As Boolean = False

 Public Sub btnStart_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Try
            chkSettingClient.Checked = True
            lblStatus.Text = "Starting server"
            poort = txtPoort.Text
            listener = New TcpListener(IPAddress.Any, poort)
            lblPoort.Text = txtPoort.Text
            listener.Start()
            lblStatus.Text = "Starting client accept"
            listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), listener)
            lblStatus.Text = "Online"
            btnStart.Enabled = False
            btnStop.Enabled = True
            btnChangePoort.Enabled = True
        Catch
            lblStatus.Text = "Offline"
            MsgBox("Er is iets mis gegaan bij het opstarten van de server." + vbNewLine + "Een mogelijke oorzaak is het poortnummer. Controleer of u het juiste poort nummer hebt ingevoerd en probeer het opnieuw." + vbNewLine + vbNewLine + "Foutcode: S-01", MsgBoxStyle.Critical, "Serverside error")
        End Try

    End Sub

    'Deze functie word uitgevoerd zodra er een client wil verbinden
    Sub AcceptClient(ByVal ar As IAsyncResult)
        Try
            newClient = New ChatClient(listener.EndAcceptTcpClient(ar))
            AddHandler (newClient.RecieveMsg), AddressOf RecieveMsg
            AddHandler (newClient.ClientLogout), AddressOf ClientLogout
            clientlist.Add(newClient)
            'Client joined
            listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), listener)
        Catch : End Try

    End Sub

    Sub ClientLogout(ByVal Client As ChatClient)
        clientlist.Remove(Client)
        'Client disconnected
        countclients()
    End Sub

Sub RecieveMsg(ByVal str As String)
        Dim arrayMsg() As String
        arrayMsg = str.Split(key) ' = ctrl + alt + "-"

        If arrayMsg(0) = "Client" Then
            If arrayMsg.Length >= 3 Then
                For j = 3 To arrayMsg.Length - 1
                    msgstring = msgstring + key + arrayMsg(j)
                Next
                Message(arrayMsg(1) + " zegt: " + arrayMsg(2) + msgstring, True)
                msgstring = Nothing
            Else
                Message(arrayMsg(1) + " zegt: " + arrayMsg(2), True)
            End If
        Else
            Message("Waarschuwing, de volgende string is ontvangen:" + vbNewLine + str, False)
        End If
    End Sub
End Class


Chatclient.vb
Imports System.Net.Sockets
Imports System.IO
Public Class ChatClient
    Public Event RecieveMsg(ByVal Str As String)
    Public Event ClientLogout(ByVal Client As ChatClient)
    Private sWriter As StreamWriter
    Private Client As TcpClient
    Sub New(ByVal xclient As TcpClient)
        Client = xclient
        Client.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
    End Sub

    Private Sub Read()
        Try
            RaiseEvent RecieveMsg(New StreamReader(Client.GetStream).ReadLine)
            Client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf Read), Nothing)
        Catch ex As Exception
            RaiseEvent ClientLogout(Me)
        End Try
    End Sub

    Public Sub Send(ByVal Message As String)
        sWriter = New StreamWriter(Client.GetStream)
        sWriter.WriteLine(Message)
        sWriter.Flush()
    End Sub
End Class


The Client:

Form1.vb
Imports System.IO, System.Net, System.Net.Sockets
Public Class frmChatwindow

    Dim key As String = "" 'BELANGRIJK: dit is de sleutel die als seperator word gebruikt tussen de verschillende onderdelen van het bericht, deze moet gelijk zijn met de key van de server
    Dim Client As TcpClient
    Dim sWriter As StreamWriter
    Dim Nickname As String

    Dim muted As Boolean 'Geeft aan of de client op dit moment gemute is

    'het klikken van de verbind knop
    Private Sub btnVerbind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerbind.Click

        If btnVerbind.Text = "Verbind" Then
            Try
                If txtServer.Text = "" Or txtPoort.Text = "" Or txtNickname.Text = "" Then
                    MsgBox("Zorg er voor dat alle benodigde gegevens ingevult zijn.")
                    Exit Sub
                End If
                Nickname = txtNickname.Text

                Client = New TcpClient(txtServer.Text, CInt(txtPoort.Text))
                Client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf read), Nothing)
                btnVerbind.Text = "Log uit"
                Me.Text = "Chat client - Verbonden met " + txtServer.Text + ":" + txtPoort.Text
                lblVerbonden.Text = "Verbonden met " + txtServer.Text + ":" + txtPoort.Text
                lblNickname.Text = Nickname
            Catch ex As Exception
                displaymsg("Kan geen verbinding maken met de server")
            End Try
        Else
            Try
                Client.Client.Close()
            Catch : End Try
            Client = Nothing
            btnVerbind.Text = "Verbind"
            Me.Text = "Chat client - Niet verbonden"
            lblVerbonden.Text = "Niet verbonden"
            lblNickname.Text = "Niet verbonden"
        End If
    End Sub

    Sub read(ByVal ar As IAsyncResult)
        If lblVerbonden.Text = "Niet verbonden" Then
            Exit Sub
        Else

            showmsg(New StreamReader(Client.GetStream).ReadLine)
            Try
                Client.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing)
            Catch
                displaymsg("De verbinding met de server is verbroken.")
                Try
                    Client.Client.Close()
                Catch : End Try
                Client = Nothing
                btnVerbind.Text = "Verbind"
                Me.Text = "Chat client - Niet verbonden"
                lblVerbonden.Text = "Niet verbonden"
                lblNickname.Text = "Niet verbonden"
                Exit Sub
            End Try
        End If
    End Sub
End Class


I would really appreciate your view on the reason of the crash.

greetings from the netherlands, Brownbear

Is This A Good Question/Topic? 0
  • +

Page 1 of 1