Bonjour,
je n'arrive pas a connecter 2 ordis (clients) a un serveur (3ème ordi).
j'utilise TClientSocket et TClientServer avec D7.
Le but: je récupère un numéro dans une appli tier que je dois envoyer sur deux ordis (clients) et afficher une image en conséquence.
Tout fonctionne bien sauf la gestion des connexions des deux clients avec le serveur.
(Je précise que les ordis sont bien vus sur le réseaux - pas de conflit)
Après un arrêt du serveur, lorsque je relance l'appli serveur :
Des fois aucun des deux clients ne veut se connecter.
Seulement un client se connecte.
Il y a aussi le serveur qui est très occupé ! par moment.
Je pense que je n'est pas bien compris la gestion des connexions.
Pourriez-vous m'aider ?
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
191
192
193
194
195 unit UClientA; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ScktComp, ComCtrls, ExtCtrls, typinfo, jpeg; type Tmoule = record numero : string; cheminA : string; cheminB : string; end; type TForm1 = class(TForm) ClientSocket1: TClientSocket; StatusBar1: TStatusBar; Memo1: TMemo; Panel1: TPanel; Timer1: TTimer; Image1: TImage; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); procedure ClientSocket1Disconnect(Sender: TObject; Socket: TCustomWinSocket); procedure ClientSocket1Connect(Sender: TObject; Socket: TCustomWinSocket); procedure ClientSocket1Connecting(Sender: TObject; Socket: TCustomWinSocket); procedure FormCreate(Sender: TObject); procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket); procedure Timer1Timer(Sender: TObject); private { Déclarations privées} public { Déclarations publiques} end; var Form1: TForm1; listemoule : array of Tmoule; bitON : boolean; //hspviseur : Hwnd; //texte : array[0..255] of char; const basefile = 'C:\bimatiere2\Server\'; implementation {$R *.DFM} //Ouverture de la base des moules pour lecture procedure TForm1.FormCreate(Sender: TObject); var i,j : integer; begin form1.Left := 10; form1.Top := 80; //Recherche fichier base et remplissage liste bitON := false; //form1.WindowState := wsmaximized; memo1.Visible := false; statusbar1.SimpleText := 'SERVER CLOSED'; if fileexists(basefile+'base.txt') then begin memo1.Lines.LoadFromFile(basefile+'base.txt'); if memo1.Lines.Count > 0 then begin i := 0; // index lines memo j := 0; // index de la liste while i < memo1.Lines.Count do begin setlength(listemoule,length(listemoule)+1); listemoule[j].numero := memo1.Lines[i]; listemoule[j].cheminA := memo1.Lines[i+1]; listemoule[j].cheminB := memo1.Lines[i+2]; i := i + 3; j := j + 1; end; form1.Caption := 'Unité A - base chargée'; bitON := true; end else begin Showmessage('il n''y a pas de référence de moule dans la base !!'); end; ClientSocket1.Port:=600; ClientSocket1.Host:='server-rep'; ClientSocket1.Open; end else begin showmessage('Le fichier de base des moules est inexistant !'+#10#13+ 'Le Client Unité A ne peut pas démarrer sans ce fichier.'+#10#13+#10#13+ 'Reprendre l''application de création des moules pour refaire une base !'); end; end; //se produit quand le serveur s'éteint procedure TForm1.ClientSocket1Disconnect(Sender: TObject; Socket: TCustomWinSocket); begin statusbar1.Panels[0].Text := 'Non Reçu'; statusbar1.Panels[1].Text := 'NOT CONNECTED'; clientsocket1.Close; end; //se produit quand le client se connecte procedure TForm1.ClientSocket1Connect(Sender: TObject; Socket: TCustomWinSocket); begin statusbar1.Panels[1].Text := 'CONNECTED TO ' + Socket.RemoteHost; statusbar1.Panels[2].Text := 'No problem'; //clientsocket1.Close; end; //se produit pendant la tentative de connexion avec le serveur procedure TForm1.ClientSocket1Connecting(Sender: TObject; Socket: TCustomWinSocket); begin StatusBar1.Panels[1].Text:= 'Tentative de connection à : '+clientsocket1.Host; end; //fermeture du programme procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin ClientSocket1.Close; end; //gestion ERREUR socket procedure TForm1.ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); begin statusbar1.Panels[2].Text := 'Init Error'; if ErrorEvent=eeconnect then begin statusbar1.Panels[2].Text := 'Err EeConnect'; end; if ErrorEvent=eeDisconnect then begin statusbar1.Panels[2].Text := 'Err EeDisconnect'; end; if ErrorEvent=eeLookUp then begin statusbar1.Panels[2].Text := 'Err EeLookUp'; end; if ErrorEvent=eegeneral then begin statusbar1.Panels[2].Text := 'Err EeGeneral'; end; if ErrorEvent=eeSend then begin statusbar1.Panels[2].Text := 'Err EeSend'; end; if ErrorEvent=eeReceive then begin statusbar1.Panels[2].Text := 'Err EeReceive'; end; // pour ne pas déclencher un autre message d'erreur par Delphi ErrorCode:=0; statusbar1.Panels[1].Text := 'NOT CONNECTED'; end; //lecture du moule reçu procedure TForm1.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket); var lecture : string; i : integer; begin //Reception du moule du type: "Moule: 801" lecture := Socket.ReceiveText; for i := 0 to high(listemoule) do begin //comparaison avec la liste des moules créés if lecture = 'Moule: '+listemoule[i].numero then begin if lecture = 'Moule: 000' then begin statusbar1.Panels[0].Text := 'Moule non trouvé'; end else begin statusbar1.Panels[0].Text := lecture; image1.Picture.LoadFromFile(listemoule[i].cheminA); end; end; end; end; procedure TForm1.Timer1Timer(Sender: TObject); begin if bitON and not clientsocket1.Active then begin //c'est juste pour mon info pendnant la mise au point if clientsocket1.active then form1.Caption := 'Active : Oui' else form1.Caption := 'Active : Non'; ClientSocket1.Host:='server-rep'; ClientSocket1.Open; end; end; end.
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
191
192
193 unit UClientB; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ScktComp, ComCtrls, ExtCtrls, typinfo, jpeg; type Tmoule = record numero : string; cheminA : string; cheminB : string; end; type TForm1 = class(TForm) ClientSocket2: TClientSocket; StatusBar1: TStatusBar; Memo1: TMemo; Panel1: TPanel; Timer1: TTimer; Image1: TImage; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure ClientSocket2Error(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); procedure ClientSocket2Disconnect(Sender: TObject; Socket: TCustomWinSocket); procedure ClientSocket2Connect(Sender: TObject; Socket: TCustomWinSocket); procedure ClientSocket2Connecting(Sender: TObject; Socket: TCustomWinSocket); procedure FormCreate(Sender: TObject); procedure ClientSocket2Read(Sender: TObject; Socket: TCustomWinSocket); procedure Timer1Timer(Sender: TObject); private { Déclarations privées} public { Déclarations publiques} end; var Form1: TForm1; listemoule : array of Tmoule; bitON : boolean; //hspviseur : Hwnd; //texte : array[0..255] of char; const basefile = 'C:\bimatiere2\Server\'; implementation {$R *.DFM} //Ouverture de la base des moules pour lecture procedure TForm1.FormCreate(Sender: TObject); var i,j : integer; begin form1.Left := 10; form1.Top := 80; //Recherche fichier base et remplissage liste bitON := false; //form1.WindowState := wsmaximized; memo1.Visible := false; statusbar1.SimpleText := 'SERVER CLOSED'; if fileexists(basefile+'base.txt') then begin memo1.Lines.LoadFromFile(basefile+'base.txt'); if memo1.Lines.Count > 0 then begin i := 0; // index lines memo j := 0; // index de la liste while i < memo1.Lines.Count do begin setlength(listemoule,length(listemoule)+1); listemoule[j].numero := memo1.Lines[i]; listemoule[j].cheminA := memo1.Lines[i+1]; listemoule[j].cheminB := memo1.Lines[i+2]; i := i + 3; j := j + 1; end; form1.Caption := 'Unité B - base chargée'; bitON := true; end else begin Showmessage('il n''y a pas de référence de moule dans la base !!'); end; ClientSocket2.Port:=600; ClientSocket2.Host:='server-rep'; ClientSocket2.Open; end else begin showmessage('Le fichier de base des moules est inexistant !'+#10#13+ 'Le Client Unité B ne peut pas démarrer sans ce fichier.'+#10#13+#10#13+ 'Reprendre l''application de création des moules pour refaire une base !'); end; end; //se produit quand le serveur s'éteint procedure TForm1.ClientSocket2Disconnect(Sender: TObject; Socket: TCustomWinSocket); begin statusbar1.Panels[0].Text := 'Non Reçu'; statusbar1.Panels[1].Text := 'NOT CONNECTED'; clientsocket2.Close; end; //se produit quand le client se connecte procedure TForm1.ClientSocket2Connect(Sender: TObject; Socket: TCustomWinSocket); begin statusbar1.Panels[1].Text := 'CONNECTED TO ' + Socket.RemoteHost; statusbar1.Panels[2].Text := 'No problem'; end; //se produit pendant la tentative de connexion avec le serveur procedure TForm1.ClientSocket2Connecting(Sender: TObject; Socket: TCustomWinSocket); begin StatusBar1.Panels[1].Text:= 'Tentative de connection à : '+ClientSocket2.Host; end; //fermeture du programme procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin ClientSocket2.Close; end; //gestion ERREUR socket procedure TForm1.ClientSocket2Error(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); begin statusbar1.Panels[2].Text := 'Init Error'; if ErrorEvent=eeconnect then begin statusbar1.Panels[2].Text := 'Err EeConnect'; end; if ErrorEvent=eeDisconnect then begin statusbar1.Panels[2].Text := 'Err EeDisconnect'; end; if ErrorEvent=eeLookUp then begin statusbar1.Panels[2].Text := 'Err EeLookUp'; end; if ErrorEvent=eegeneral then begin statusbar1.Panels[2].Text := 'Err EeGeneral'; end; if ErrorEvent=eeSend then begin statusbar1.Panels[2].Text := 'Err EeSend'; end; if ErrorEvent=eeReceive then begin statusbar1.Panels[2].Text := 'Err EeReceive'; end; // pour ne pas déclencher un autre message d'erreur par Delphi ErrorCode:=0; statusbar1.Panels[1].Text := 'NOT CONNECTED'; end; //lecture du moule reçu procedure TForm1.ClientSocket2Read(Sender: TObject; Socket: TCustomWinSocket); var lecture : string; i : integer; begin //Reception du moule du type: "Moule: 801" lecture := Socket.ReceiveText; for i := 0 to high(listemoule) do begin //comparaison avec la liste des moules créés if lecture = 'Moule: '+listemoule[i].numero then begin if lecture = 'Moule: 000' then begin statusbar1.Panels[0].Text := 'Moule non trouvé'; end else begin statusbar1.Panels[0].Text := lecture; image1.Picture.LoadFromFile(listemoule[i].cheminB); end; end; end; end; procedure TForm1.Timer1Timer(Sender: TObject); begin if bitON and not clientsocket2.Active then begin if clientsocket2.Active then form1.Caption := 'Active : Oui' else form1.Caption := 'Active : Non'; ClientSocket2.Host:='server-rep'; ClientSocket2.Open; end; end; end.
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 unit UServerM; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ScktComp, ComCtrls, ExtCtrls, jpeg; type Tmoule = record numero : string; cheminA : string; cheminB : string; end; type TForm1 = class(TForm) ServerSocket1: TServerSocket; StatusBar1: TStatusBar; Timer1: TTimer; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket); procedure ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket); procedure ServerSocket1ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket); procedure FormShow(Sender: TObject); procedure ServerSocket1Listen(Sender: TObject; Socket: TCustomWinSocket); procedure FormCreate(Sender: TObject); procedure ServerSocket1ClientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); procedure Timer1Timer(Sender: TObject); private { Déclarations privées} public { Déclarations publiques} end; var Form1: TForm1; hspviseur: Hwnd; texte : array[0..255] of char; const basefile = 'C:\bimatiere2\Server\'; implementation {$R *.DFM} // recherche du Static "Moule : ..." de la fenêtre SPVISEUR function enumchildwindowsproc(wnd : hwnd;form : Tform1): bool; export; {$ifdef win32} stdcall; {$endif} var buffer : array[0..99] of char; nomclass : array[0..255] of char; begin result := true; getclassname(wnd,nomclass,256); getwindowtext(wnd,buffer,100); if strpas(nomclass) = 'Static' then if copy(strpas(buffer),0,6) = 'Moule:' then begin hspviseur := wnd; form1.statusbar1.panels[0].Text := buffer; end; end; //Ecoute des clients procedure TForm1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket); begin end; //un client se connecte procedure TForm1.ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket); begin if socket.RemoteHost = 'CLIENTA' then StatusBar1.Panels[2].Text :='Client Unité A Connecté !'; if socket.RemoteHost = 'CLIENTB' then StatusBar1.Panels[3].Text :='Client Unité B Connecté !'; end; //un client de déconnecte procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket); begin if socket.RemoteHost = 'CLIENTA' then StatusBar1.Panels[2].Text :='Client Unité A déconnecté !'; if socket.RemoteHost = 'CLIENTB' then StatusBar1.Panels[3].Text :='Client Unité B déconnecté !'; end; //Server passe à l'écoute procedure TForm1.ServerSocket1Listen(Sender: TObject; Socket: TCustomWinSocket); begin statusbar1.Panels[1].Text := ServerSocket1.Socket.LocalHost + ' OPENED'; end; //fermeture du serveur procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin ServerSocket1.Close; end; procedure TForm1.FormShow(Sender: TObject); begin ServerSocket1.Port:= 600; ServerSocket1.Open; end; //ouverture du serveur procedure TForm1.FormCreate(Sender: TObject); begin form1.Left := 10; form1.Top := 10; hspviseur := findwindow(nil,'Untitled - SpViseur'); if hspviseur <> 0 then begin enumchildwindows(hspviseur,@enumchildwindowsproc,0); end; end; //mise a jour numero du moule et reconnexion si perdue procedure TForm1.Timer1Timer(Sender: TObject); var buffer : array[0..15] of char; begin //tentative de remise en route du serveur si pas demarré if not serversocket1.Active then begin serversocket1.Port:=600; serversocket1.Open; end else begin //envoie du numero moule buffer := ''; if hspviseur<>0 then getwindowtext(hspviseur,buffer,15) else //si le moule n'est pas trouvé on dans la liste envoie 0 comme numéro //le serveur affichera "Moule non trouvé dans l'application REP" buffer := 'Moule: 000'; if serversocket1.Socket.ActiveConnections = 1 then serversocket1.Socket.Connections[0].SendText(strpas(buffer)); if serversocket1.Socket.ActiveConnections = 2 then begin serversocket1.Socket.Connections[0].SendText(strpas(buffer)); serversocket1.Socket.Connections[1].SendText(strpas(buffer)); end; statusbar1.Panels[0].Text := buffer; end; end; //gestion des erreurs procedure TForm1.ServerSocket1ClientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); begin if socket.RemoteHost = 'CLIENTA' then begin if ErrorEvent=eeconnect then begin statusbar1.Panels[2].Text := 'Err EeConnect'; end; if ErrorEvent=eeDisconnect then begin StatusBar1.Panels[2].Text :='Client déconnecté !'; end; if ErrorEvent=eeLookUp then begin statusbar1.Panels[2].Text := 'Err EeLookUp'; end; if ErrorEvent=eegeneral then begin statusbar1.Panels[2].Text := 'Err EeGeneral'; end; if ErrorEvent=eeSend then begin statusbar1.Panels[2].Text := 'Err EeSend'; end; if ErrorEvent=eeReceive then begin statusbar1.Panels[2].Text := 'Err EeReceive'; end; end; if socket.RemoteHost = 'CLIENTB' then begin if ErrorEvent=eeconnect then begin statusbar1.Panels[3].Text := 'Err EeConnect'; end; if ErrorEvent=eeDisconnect then begin StatusBar1.Panels[3].Text :='Client déconnecté !'; end; if ErrorEvent=eeLookUp then begin statusbar1.Panels[3].Text := 'Err EeLookUp'; end; if ErrorEvent=eegeneral then begin statusbar1.Panels[3].Text := 'Err EeGeneral'; end; if ErrorEvent=eeSend then begin statusbar1.Panels[3].Text := 'Err EeSend'; end; if ErrorEvent=eeReceive then begin statusbar1.Panels[3].Text := 'Err EeReceive'; end; end; // pour ne pas déclencher un autre message d'erreur par Delphi ErrorCode:=0; end; end.
Partager