Bonjour à tous,
Donc voilà, j'ai réussi à faire fonctionner le contrôle winsock, envoyer des informations localement et à distance sur plusieurs pc (clients) connectés en même temps.
Mais j'ai un soucis.
Si je prend un exemple tout basique :
j'ai le serveur sur un pc, et le clien sur un autre.
Je fais une connexion à distance.
Tout va bien.
Je déconnecte le client, et me reconnecte avec.
Si il y a envoi d'information du Serveur au client, j'aurai une erreur du style :
"Erreur d'execution '40006'
Etat de connexion ou de protocole erroné pour la transaction ou la requête requise", une erreur concernant le Serveur.
Evidemment cela me le fait dès la reconnexion du client car le nombre de client total augmente de 1, et le serveur envoi à tous les clients le message "/i\Nouveau client".
Donc je suppose que la connexion se ferme mal au niveau du serveur pour le client donné, mais j'ai beau toucher à tout, ça ne change rien...=/
Donc je me tourne vers vous, en vous déposant le code du serveur et du client, afin de voir si vous trouvez la petite erreur.
Question de patience, alors je vous remercie d'avance du temps que vous consacrerez à mon problème.
Code Serveur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 Dim NbClient As Byte Dim strData As String Dim i As Byte Dim nbr As Long Private Sub Command1_Click() nbr = NbrEnr.Caption For i = 1 To NbClient tcpServer(i).SendData Me.txtSendData.Text Next i txt.Text = txt.Text & vbCrLf & txtSendData.Text NbrEnr.Caption = nbr + 1 End Sub Private Sub Command2_Click() nbr = NbrEnr.Caption Dim X As String Dim Y As String X = C1.Text Y = C2.Text For i = 1 To NbClient tcpServer(i).SendData "C²|" & X & "|" & Y Next i txt.Text = txt.Text & vbCrLf & "C²|" & X & "|" & Y NbrEnr.Caption = nbr + 1 End Sub Private Sub Command3_Click() Unload Me End Sub Private Sub Command4_Click() tcpServer(0).Listen End Sub Private Sub envoi2_Click() End Sub Private Sub Form_Load() intMax = 0 tcpServer(0).LocalPort = 1001 tcpServer(0).Listen ' Donne à la propriété LocalPort une valeur ' entière. Puis invoque la méthode Listen. End Sub Private Sub tcpServer_DataArrival _ (Index As Integer, ByVal bytesTotal As Long) nbr = NbrEnr.Caption ' Déclare une variable pour les données reçues. ' Invoque la méthode GetData et paramètre pour ' ces données la propriété Text d'un contrôle ' TextBox nommé txtOutput. 'tcpServer(intMax).GetData strData tcpServer(Index).GetData strData txtOutput(Index).Caption = strData 'txt.Text = txt.Text & vbCrLf & strData NbrEnr.Caption = nbr + 1 Me.txtSendData.Text = strData Command1_Click Dim Ch As String Dim CutCh Ch = txtOutput(Index).Caption & vbCrLf CutCh = Split(Ch, "|") Select Case CutCh(0) Case "Text" txt.Text = txt.Text & vbCrLf & CutCh(1) 'On renvoit vers les autres clients For i = 1 To NbClient tcpServer(i).SendData strData Next i Case "Deco" dcxID.Text = dcxID.Text & vbCrLf & "Déconnexion de " & Str(requestID) Me.tcpServer(0).Close 'NbClient = NbClient - 1 Case Else 'MsgBox "Données non identifiables" End Select End Sub Private Sub tcpServer_ConnectionRequest _ (Index As Integer, ByVal requestID As Long) If Index = 0 Then NbClient = NbClient + 1 Load tcpServer(NbClient) tcpServer(NbClient).LocalPort = 0 tcpServer(0).Close tcpServer(NbClient).Accept requestID txtSendData.Text = "/i\Nouveau connecté" Command1_Click Load txtOutput(NbClient) End If ' Vérifie que le contrôle est fermé. Sinon, ferme ' la connexion en cours avant d'accepter la ' nouvelle. 'If tcpServer.State <> sckClosed Then _ tcpServer.Close ' Accepte la demande avec le paramètre requestID. 'tcpServer.Accept requestID End Sub Private Sub Timer2_Timer() stat.Caption = tcpServer(0).State If stat.Caption = "0" Then tcpServer(0).Listen End If End Sub Private Sub Timer3_Timer() nb.Caption = NbClient str2.Caption = i End Sub Private Sub txtSendData_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyRight Then Me.Image1.Left = Me.Image1.Left + 100 C1.Text = Image1.Left C2.Text = Image1.Top Command2_Click End If If KeyCode = vbKeyLeft Then Me.Image1.Left = Me.Image1.Left - 100 C1.Text = Image1.Left C2.Text = Image1.Top Command2_Click End If If KeyCode = vbKeyDown Then Me.Image1.Top = Me.Image1.Top + 100 C1.Text = Image1.Left C2.Text = Image1.Top Command2_Click End If If KeyCode = vbKeyUp Then Me.Image1.Top = Me.Image1.Top - 100 C1.Text = Image1.Left C2.Text = Image1.Top Command2_Click End If End Sub Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyRight Then Me.Image1.Left = Me.Image1.Left + 100 C1.Text = Image1.Left C2.Text = Image1.Top Command2_Click End If If KeyCode = vbKeyLeft Then Me.Image1.Left = Me.Image1.Left - 100 C1.Text = Image1.Left C2.Text = Image1.Top Command2_Click End If If KeyCode = vbKeyDown Then Me.Image1.Top = Me.Image1.Top + 100 C1.Text = Image1.Left C2.Text = Image1.Top Command2_Click End If If KeyCode = vbKeyUp Then Me.Image1.Top = Me.Image1.Top - 100 C1.Text = Image1.Left C2.Text = Image1.Top Command2_Click End If End Sub
Code Client :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Private Sub Command1_Click() MsgBox tcpClient.State End Sub Private Sub Command2_Click() Me.txtSend.Refresh Me.txtOutput.Refresh End Sub Private Sub Command3_Click() tcpClient.SendData "Client : " & txtSend.Text 'txt.Text = txt.Text & vbCrLf & "Client : " & txtSend.Text End Sub Private Sub Command4_Click() Unload Me End Sub Private Sub Command5_Click() End Sub Private Sub Form_Load() ' Le nom du contrôle Winsock est tcpClient. ' Note : pour spécifier un hôte distant, vous pouvez utiliser soit ' l'adresse IP (ex: "121.111.1.1"), soit le nom complet de ' l'ordinateur, comme ci-dessous. 'tcpClient.RemoteHost = "192.168.1.86" 'tcpClient.RemotePort = 1001 stat.Caption = "Déconnecté" End Sub Private Sub cmdConnect_Click() ' Invoque la méthode Connect pour établir une ' connexion. If Option1 = True Then 'Label1.Caption = "connexion en cours..." tcpClient.Connect "192.168.1.86", 1001 Timer1.Enabled = True ElseIf Option2 = True Then 'Label1.Caption = "connexion en cours..." tcpClient.Connect "xxx.xxx.xxx.xxx", 1001 'Ip "externe" masquée ici par sécurité Timer1.Enabled = True End If End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) tcpClient.SendData "Deco|" Cancel = 1 Timer2.Enabled = True End Sub Private Sub tcpClient_DataArrival _ (ByVal bytesTotal As Long) Dim strData As String tcpClient.GetData strData txtOutput.Caption = strData End Sub Private Sub Timer1_Timer() If serverstat.Caption = "7" Then Label1.Caption = "connexion réussie" stat.Caption = "Connecté" Me.Timer3.Enabled = True ElseIf serverstat.Caption = "6" Then Label1.Caption = "connexion échouée" 'Me.tcpClient.Close End If End Sub Private Sub Timer2_Timer() End End Sub Private Sub Timer3_Timer() serverstat.Caption = Me.tcpClient.State If serverstat.Caption = "8" Then Me.tcpClient.Close stat.Caption = "Déconnecté" Label1.Caption = "Perte de connexion" End If If Label1.Caption <> "connexion réussie" Then led.BackColor = &HC0& Else led.BackColor = &HC000& End If End Sub Private Sub txtOutput_Change() Dim Ch As String Dim CutCh Dim X As Single Dim Y As Single Ch = txtOutput.Caption & vbCrLf CutCh = Split(Ch, "|") Select Case CutCh(0) Case "C²" C1 = CutCh(1) C2 = CutCh(2) X = C1.Text Y = C2.Text Debug.Print X, Y Me.Image1.Left = X Me.Image1.Top = Y Case "Text" txt.Text = txt.Text & vbCrLf & CutCh(1) Case "Deco" txt.Text = txt.Text & vbCrLf & CutCh(1) Case Else 'MsgBox "Données non identifiables" End Select txt.Text = txt.Text & vbCrLf & txtOutput.Caption End Sub
Merci
Partager