Chat avec salons

Description

Voici mon code, cela fait quelques semaines que je le modifie et le remodifie...Au niveau des conditions je pense que je suis bien et que j'ai fais au plus simple...mais ce que je voudrais, c'est la solution à pourquoi mon chat ne fonctionne plus au bout d'un moment.
Voici ses syndrômes :
-Je lance une discussion avec une personne1
-je quitte la discussion avec personne1 pour parler à personne2
-Personne1 m'envoi un message ; j'ai une petite alerte de message qui s'affiche pr me dire que j'ai un nouveau message.
-Je répond à personne1, mais celle-ci ne peut plus m'envoyer de message...il doit y avoir un winsock qui merde ou je ne sais quoi...
MERCI DE VOTRE AIDE

Source / Exemple :


'Ceci est une partie de mon code...la suite est à télécharger...mais mon erreur se trouve dans cette partie de code

Imports MySql.Data.MySqlClient
Imports System.IO
Public Class Form2
    Private Sub Form2_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Dim conn As MySqlConnection = Nothing
        If Form1.SQLOpen(conn) = True Then
            Form1.SQLCommand("UPDATE utilisateur SET connecte=0,salon="""" where id='" & Form1.tb_nom.Text & "'")
            Form1.SQLCommand("UPDATE discussion SET ouvert=0 WHERE moi='" & My.Settings.id & "'")
        End If
        Form1.SQLClose(conn)

        Me.Winsock_client.Close()
        Me.Winsock_serveur.Close()
        Form1.Close()
    End Sub

    Private Sub Form2_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        contacts()
    End Sub
    Private Sub contacts()
        Me.lv_contacts.Clear()
        If Me.cb_salons.Text = "Tous les utilisateurs" Then
            Dim x2 As Integer
            Dim conn2 As MySqlConnection = Nothing
            If Form1.SQLOpen(conn2) = True Then
                Dim reader2 As MySqlDataReader
                reader2 = Nothing
                Dim cmd2 As New MySqlCommand("select id from utilisateur where connecte = 1 and id<>'" & My.Settings.id & "'", conn2)
                reader2 = cmd2.ExecuteReader()
                While reader2.Read()
                    Me.lv_contacts.Items.Add(reader2.GetString(0))
                    Me.lv_contacts.Items(x2).ImageIndex = 0
                    x2 += 1
                End While
            End If
            Form1.SQLClose(conn2)
        Else
            Try
                Dim x As Integer
                Dim conn As MySqlConnection = Nothing
                If Form1.SQLOpen(conn) = True Then
                    Dim reader As MySqlDataReader
                    reader = Nothing
                    Dim cmd As New MySqlCommand("select id from utilisateur where connecte = 1 and id<>'" & My.Settings.id & "' and salon='" & Me.cb_salons.SelectedItem.ToString & "'", conn)
                    reader = cmd.ExecuteReader()
                    While reader.Read()
                        Me.lv_contacts.Items.Add(reader.GetString(0))
                        Me.lv_contacts.Items(x).ImageIndex = 0
                        x += 1
                    End While
                End If
                Form1.SQLClose(conn)
            Catch ex As Exception
                Dim x3 As Integer
                Dim conn3 As MySqlConnection = Nothing
                If Form1.SQLOpen(conn3) = True Then
                    Dim reader3 As MySqlDataReader
                    reader3 = Nothing
                    Dim cmd3 As New MySqlCommand("select id from utilisateur where connecte = 1 and id<>'" & My.Settings.id & "'", conn3)
                    reader3 = cmd3.ExecuteReader()
                    While reader3.Read()
                        Me.lv_contacts.Items.Add(reader3.GetString(0))
                        Me.lv_contacts.Items(x3).ImageIndex = 0
                        x3 += 1
                    End While
                End If
                Form1.SQLClose(conn3)
            End Try
        End If
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Form1.Hide()

        Dim conn As MySqlConnection = Nothing
        If Form1.SQLOpen(conn) = True Then
            Form1.SQLCommand("UPDATE utilisateur SET port='" & My.Settings.port & "' where id='" & My.Settings.id & "'")
        End If
        Form1.SQLClose(conn)

        Me.Winsock_serveur.Close()
        Me.Winsock_serveur.LocalPort = My.Settings.port 'rq : la propriété aurait pu être défini directement
        Me.Winsock_serveur.Listen()
    End Sub

    Private Sub Winsock_serveur_ConnectionRequest(ByVal sender As Winsock_Control.Winsock, ByVal requestID As System.Net.Sockets.Socket) Handles Winsock_serveur.ConnectionRequest
        Me.Winsock_serveur.Accept(requestID)
    End Sub

    Private Sub Winsock_serveur_DataArrival(ByVal sender As Winsock_Control.Winsock, ByVal BytesTotal As System.Int32) Handles Winsock_serveur.DataArrival
        'Recuperation du nom du contact qui m'ecrit
        Dim ip As String = Me.Winsock_serveur.RemoteHostIP
        Dim port As Integer
        Dim contact As String = Nothing
        Dim conn As MySqlConnection = Nothing
        If Form1.SQLOpen(conn) = True Then
            Dim reader As MySqlDataReader
            reader = Nothing
            Dim cmd As New MySqlCommand("select id,port from utilisateur where connecte = 1 and ip='" & ip & "'", conn)
            reader = cmd.ExecuteReader()
            reader.Read()
            contact = reader.GetString(0)
            port = reader.GetInt32(1)
        End If
        Form1.SQLClose(conn)

        'Si je suis en dialogue avec le contact ou en dialogue avec personne
        If Me.lb_copain.Text = contact Or Me.lb_copain.Text = "copain" Then
            Me.lb_copain.Text = contact

            Dim data As String = Nothing
            Me.Winsock_serveur.GetData(data)
            Me.rtb_chat.AppendText(data)
            File.WriteAllText("C:\Temp\" & contact & ".rtf", data)

            'On verifie de statut de la discussion entre les 2 contacts
            Dim ouvert As String = Nothing
            If Form1.SQLOpen(conn) = True Then
                Dim reader As MySqlDataReader
                reader = Nothing
                Dim cmd As New MySqlCommand("select ouvert from discussion where moi='" & My.Settings.id & "' and copain = '" & contact & "'", conn)
                reader = cmd.ExecuteReader()
                reader.Read()
                ouvert = reader.GetString(0)
            End If
            Form1.SQLClose(conn)
            If ouvert = 0 Then
                'Je me connecte au contact
                Me.Winsock_client.Close()
                Me.Winsock_client.Connect(ip, port)

                'Mise à jour de la table discussion :
                If Form1.SQLOpen(conn) = True Then
                    Form1.SQLCommand("UPDATE discussion SET ouvert=1 where moi='" & My.Settings.id & "' and copain='" & Me.lb_copain.Text & "'")
                    Form1.SQLCommand("UPDATE discussion SET ouvert=0 where moi='" & My.Settings.id & "' and copain<>'" & Me.lb_copain.Text & "'")
                End If
                Form1.SQLClose(conn)
            End If

            'Sinon si je suis en dialogue avec un autre contact
        Else
            Dim data2 As String = Nothing
            Me.Winsock_serveur.GetData(data2)
            File.WriteAllText("C:\Temp\" & contact & ".rtf", data2)

            Dim index As Integer
            Dim trouve As Boolean = False
            While index < Me.lv_en_cours.Items.Count
                If Me.lv_en_cours.Items(index).Text = contact Then
                    trouve = True
                    Exit While
                End If
                index += 1
            End While
            Dim cont As Integer
            If trouve = True Then
                Me.lv_en_cours.Items(cont).BackColor = Color.Red
                Me.lv_en_cours.Items(cont).ImageIndex = 0
            Else
                Me.lv_en_cours.Items.Add(contact)
                Me.lv_en_cours.Items(cont).BackColor = Color.Red
                Me.lv_en_cours.Items(cont).ImageIndex = 0
            End If
        End If
    End Sub

    Private Sub cb_salons_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb_salons.Enter
        Me.cb_salons.Items.Clear()
        Me.cb_salons.Items.Add("Tous les utilisateurs")
        Dim conn As MySqlConnection = Nothing
        If Form1.SQLOpen(conn) = True Then
            Dim reader As MySqlDataReader
            reader = Nothing
            Dim cmd As New MySqlCommand("select nom_salon from salon", conn)
            reader = cmd.ExecuteReader()
            While reader.Read()
                Me.cb_salons.Items.Add(reader.GetString(0))
            End While
        End If
        Form1.SQLClose(conn)
    End Sub

    Private Sub cb_salons_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb_salons.SelectedIndexChanged
        Me.lv_contacts.Clear()
        Dim x2 As Integer
        If Me.cb_salons.SelectedItem.ToString = "Tous les utilisateurs" Then
            Dim conn3 As MySqlConnection = Nothing
            If Form1.SQLOpen(conn3) = True Then
                Dim reader3 As MySqlDataReader
                reader3 = Nothing
                Dim cmd3 As New MySqlCommand("select id from utilisateur where connecte=1 and id <> '" & My.Settings.id & "'", conn3)
                reader3 = cmd3.ExecuteReader()
                While reader3.Read()
                    Me.lv_contacts.Items.Add(reader3.GetString(0))
                    Me.lv_contacts.Items(x2).ImageIndex = 0
                End While
            End If
            Form1.SQLClose(conn3)
        Else
            Dim x As Integer
            Dim conn As MySqlConnection = Nothing
            If Form1.SQLOpen(conn) = True Then
                Dim reader As MySqlDataReader
                reader = Nothing
                Dim cmd As New MySqlCommand("select id from utilisateur where salon = '" & Me.cb_salons.SelectedItem.ToString & "' and id<>'" & My.Settings.id & "' and connecte=1", conn)
                reader = cmd.ExecuteReader()
                While reader.Read()
                    Me.lv_contacts.Items.Add(reader.GetString(0))
                    Me.lv_contacts.Items(x).ImageIndex = 0
                End While
            End If
            Form1.SQLClose(conn)
        End If

        If Me.cb_salons.SelectedItem.ToString <> "Tous les utilisateurs" Then
            Dim conn2 As MySqlConnection = Nothing
            If Form1.SQLOpen(conn2) = True Then
                Form1.SQLCommand("UPDATE utilisateur SET salon='" & Me.cb_salons.SelectedItem.ToString & "' where id='" & My.Settings.id & "'")
            End If
            Form1.SQLClose(conn2)
        Else
            Dim conn2 As MySqlConnection = Nothing
            If Form1.SQLOpen(conn2) = True Then
                Form1.SQLCommand("UPDATE utilisateur SET salon="""" where id='" & My.Settings.id & "'")
            End If
            Form1.SQLClose(conn2)
        End If
    End Sub

    Private Sub bt_chat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_chat.Click
        Dim data As String = Me.tb_dialogue.Text
        Me.Winsock_client.Send(My.Settings.id & " dit : " & data & vbCrLf)
        Me.rtb_chat.AppendText(My.Settings.id & " dit : " & data & vbCrLf)
        Me.tb_dialogue.Text = ""
    End Sub

    Private Sub lv_en_cours_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lv_en_cours.Click
        Dim index As Integer
        If Me.lv_en_cours.SelectedItems(index).Text <> Me.lb_copain.Text Then
            'J'indique le nom de mon contact dans la fenêtre
            Me.lb_copain.Text = Me.lv_en_cours.SelectedItems(index).Text

            Dim conn As MySqlConnection = Nothing

            'Recuperation de l'ip et du port du contact selectionné
            If Form1.SQLOpen(conn) = True Then
                Dim reader As MySqlDataReader
                reader = Nothing
                Dim cmd As New MySqlCommand("select ip,port from utilisateur where id = '" & Me.lb_copain.Text & "' and connecte=1", conn)
                reader = cmd.ExecuteReader()
                reader.Read()
                Me.Winsock_client.Close() 'Et connexion sur son pc
                MsgBox(Me.Winsock_client.GetState)
                Me.Winsock_client.Connect(reader.GetString(0), reader.GetString(1))
            End If
            Form1.SQLClose(conn)

            'Mise à jour de la table discussion :
            If Form1.SQLOpen(conn) = True Then
                Form1.SQLCommand("UPDATE discussion SET ouvert=1 where moi='" & My.Settings.id & "' and copain='" & Me.lb_copain.Text & "'")
                Form1.SQLCommand("UPDATE discussion SET ouvert=0 where moi='" & My.Settings.id & "' and copain<>'" & Me.lb_copain.Text & "'")
            End If
            Form1.SQLClose(conn)

            Try
                Dim x As Integer
                'lecture du fichier texte correspondant au contact qui m'a envoyé un message
                Dim stream As New StreamReader("C:\Temp\" & Me.lv_en_cours.SelectedItems(x).Text & ".rtf")
                Me.rtb_chat.Text = stream.ReadToEnd()
                stream.Close()
            Catch ex As Exception
            End Try
        End If
    End Sub

    Private Sub lv_contacts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lv_contacts.Click
        Dim index As Integer
        If Me.lv_contacts.SelectedItems(index).Text <> Me.lb_copain.Text Then
            'J'indique le nom de mon contact dans la fenêtre
            Me.lb_copain.Text = Me.lv_contacts.SelectedItems(index).Text
            'Je nettoie la zone de chat

            Dim conn As MySqlConnection = Nothing
            'Recuperation de l'ip et du port du contact selectionné
            If Form1.SQLOpen(conn) = True Then
                Dim reader As MySqlDataReader
                reader = Nothing
                Dim cmd As New MySqlCommand("select ip,port from utilisateur where id = '" & Me.lb_copain.Text & "' and connecte=1", conn)
                reader = cmd.ExecuteReader()
                reader.Read()
                Me.Winsock_client.Close() 'Et connexion sur son pc
                MsgBox(Me.Winsock_client.GetState)
                System.Threading.Thread.Sleep(3000)
                MsgBox(Me.Winsock_client.GetState)
                Me.Winsock_client.Connect(reader.GetString(0), reader.GetString(1))
            End If
            Form1.SQLClose(conn)

            'Mise à jour de la table discussion :
            If Form1.SQLOpen(conn) = True Then
                Form1.SQLCommand("UPDATE discussion SET ouvert=1 where moi='" & My.Settings.id & "' and copain='" & Me.lb_copain.Text & "'")
                Form1.SQLCommand("UPDATE discussion SET ouvert=0 where moi='" & My.Settings.id & "' and copain<>'" & Me.lb_copain.Text & "'")
            End If
            Form1.SQLClose(conn)

            Try
                Dim x As Integer
                'lecture du fichier texte correspondant au contact qui m'a envoyé un message
                Dim stream As New StreamReader("C:\Temp\" & Me.lv_contacts.SelectedItems(x).Text & ".rtf")
                Me.rtb_chat.Text = stream.ReadToEnd()
                stream.Close()
            Catch ex As Exception
            End Try
        End If
        MsgBox(Me.Winsock_client.GetState)
    End Sub
End Class

Conclusion :


Le dump de ma base de données MySql est dans le zip (chat.sql)

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.