Erreur à la fermeture d'un serveur
Bonjour tout le monde,
comme l'indique le titre, je rencontre un problème avec la fermeture de mon formulaire serveur socket, il m'indique un message d'erreur que je n'ai pas compris :? (Impossible d'appeler la valeur Dispose() pendant un CreateHandle().) je ne vois pas de quoi pourrait bien provenir cette erreur, voici le code coté serveur :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CheckForIllegalCrossThreadCalls = False
Serveur = New TcpListener(IPAddress.Any, 80)
Serveur.Start()
ThreadServeur = New Thread(AddressOf Listening)
ThreadServeur.Start()
ListBox1.Items.Add("Serveur lancé et en attente de connexion...")
End Sub
Public Sub Listening()
Dim Client As New NClient
While True
Try
Client.SocketClient = Serveur.AcceptSocket
ClientIP = Client.SocketClient.RemoteEndPoint
Client.ThreadClient = New Thread(AddressOf Lecture)
Clients.Add(ClientIP, Client)
NConnexion(ClientIP)
Client.ThreadClient.Start()
Catch ex As Exception
End Try
End While
End Sub
Public Sub Lecture()
Dim Client As New NClient
Dim DATAS() As Byte
Dim IP As IPEndPoint = ClientIP
Client = Clients(IP)
While True
If Client.SocketClient.Connected Then
DATAS = New Byte(100) {}
Try
If Client.SocketClient.Receive(DATAS, DATAS.Length, 0) > 0 Then
Client.Message = Encoding.UTF7.GetString(DATAS)
Clients(IP) = Client
Data_Recue(IP)
Else
ConnexionFinie(IP)
Exit While
End If
Catch ex As Exception
ConnexionFinie(IP)
Exit While
End Try
End If
End While
Call Fermer_Le_Thread(IP)
End Sub
Public Sub Fermer_Le_Thread(ByVal IP As IPEndPoint)
Dim Client As NClient = Clients(IP)
Try
Client.ThreadClient.Abort()
Catch ex As Exception
Clients.Remove(IP)
End Try
End Sub
Private Sub NConnexion(ByVal IDTerminal As IPEndPoint)
ListBox1.Items.Add(vbCrLf & "Connexion de " & "Client1...")
RichTextBox1.ScrollToCaret()
ListBox1.Items.Add("Client1 en ligne !")
SendAll("Connecté !")
End Sub
Private Sub ConnexionFinie(ByVal IDTerminal As IPEndPoint)
Try
ListBox1.Items.Add(vbCrLf & "Déconnexion de " & "Client1...")
RichTextBox1.ScrollToCaret()
ListBox1.Items.Remove("Client1 en ligne !")
SendAll("Déconnecté !")
Catch ex As Exception
End Try
End Sub
Private Sub Data_Recue(ByVal IDTerminal As IPEndPoint)
RichTextBox1.AppendText(vbCrLf & "Client1" & " : ")
RichTextBox1.AppendText(Data_Obtenue(IDTerminal))
RichTextBox1.ScrollToCaret()
SendAll("Client1" & " : " & Data_Obtenue(IDTerminal))
End Sub
Public Function Data_Obtenue(ByVal IDClient As IPEndPoint) As String
Dim Client As NClient
Client = Clients(IDClient)
Return Client.Message
End Function
Public Sub CreateOne(ByVal IDClient As IPEndPoint)
Dim Client As NClient
Client = Clients(IDClient)
Client.SocketClient.Close()
Client.ThreadClient.Abort()
End Sub
Public Sub CreateAll()
Dim Client As NClient
For Each Client In Clients.Values
Client.SocketClient.Close()
Client.ThreadClient.Abort()
Next
End Sub
Public Sub SendOne(ByVal IDClient As IPEndPoint, ByVal DATAS As String)
Dim Client As NClient
Client = Clients(IDClient)
Client.SocketClient.Send(Encoding.UTF7.GetBytes(DATAS))
End Sub
Public Sub SendAll(ByVal DATAS As String)
Dim Client As NClient
For Each Client In Clients.Values
Client.SocketClient.Send(Encoding.UTF7.GetBytes(DATAS))
Next
End Sub
Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Enter Then
SendAll("Serveur : " & TextBox2.Text)
RichTextBox1.AppendText(vbCrLf & "Serveur : " & TextBox2.Text)
RichTextBox1.ScrollToCaret()
TextBox2.Clear()
End If
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
SendAll("Serveur déconnecté !")
CreateAll()
Serveur.Stop()
ThreadServeur.Abort()
End Sub
End Class |
Merci pour votre aide ! :P