Salut tous le monde j'essaye de faire une simple application de chat en utilisant mon adresse IP publique comme adresse de mon serveur (j'utilise le host de no-ip puisque mon adresse publique est dynamique) et je reçois toujours l'erreur suivante du côté du client : Erreur de socket #10061 Connexion refusée. sachant que j'ai établi la règle de NAT (Network Adress Translation) dans mon routeur et biensure j'utilise le même port sur le client et le serveur. Alors comment y remédier à cette erreur. Voila le code source du client et serveur.
Client
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 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, StdCtrls; type TForm1 = class(TForm) Edit1: TEdit; Label1: TLabel; Label2: TLabel; Edit2: TEdit; Button1: TButton; Button2: TButton; IdTCPClient1: TIdTCPClient; Shape1: TShape; Edit3: TEdit; Button3: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Déclarations privées } public { Déclarations publiques } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin IdTCPClient1.Port:= strtoint(edit2.text);(* spécifier le port*) IdTCPClient1.Host:=Edit1.Text; (* Spécifier l'adresse IP du serveur.*) IdTCPClient1.connect; (* demander détablir une connexion*) Shape1.Brush.Color := clblue;(* Brush Spécifie la couleur et le motif utilisés pour remplir le contrôle forme.*) end; procedure TForm1.Button2Click(Sender: TObject); begin IdTCPClient1.Disconnect; (* se déconnecter*) Shape1.Brush.Color := clred; end; procedure TForm1.Button3Click(Sender: TObject); begin IdTCPClient1.WriteLn(edit3.Text); (* envoyer un message de type string à laconnexion courante *) 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 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdBaseComponent, IdComponent, IdTCPServer, ExtCtrls, StdCtrls; type TForm1 = class(TForm) Label1: TLabel; Edit1: TEdit; Label2: TLabel; Edit2: TEdit; Button1: TButton; Button2: TButton; Shape1: TShape; IdTCPServer1: TIdTCPServer; Memo1: TMemo; Button3: TButton; Edit3: TEdit; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure IdTCPServer1Connect(AThread: TIdPeerThread); procedure IdTCPServer1Execute(AThread: TIdPeerThread); procedure Button3Click(Sender: TObject); private { Déclarations privées } public { Déclarations publiques } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin IdTCPServer1.DefaultPort:= strtoint(edit1.Text); IdTCPServer1.Active := true; (*ouvre la une connexion d'écoute qui rend le socketattentif aux demandesdes clients.*) edit2.text:= IdTCPServer1.localname;(*localname Indique le nom du système local.*) Shape1.Brush.Color := clblue; end; procedure TForm1.Button2Click(Sender: TObject); begin IdTCPServer1.Active := false; (* se déconnecter*) Shape1.Brush.Color := clred; end; procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread); begin memo1.Lines.Add ('Connecter de: ' +AThread.Connection.Socket.Binding.PeerIP); end; procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread); var text:string; begin text :=athread.Connection.readln(); memo1.Lines.add( athread.Connection.socket.Binding.peerIp+ text); end; procedure TForm1.Button3Click(Sender: TObject); var Count: Integer; List : TList; begin List := IdTCPServer1.Threads.LockList; try for Count := 0 to List.Count -1 do begin TIdPeerThread(List.Items[Count]).Connection.WriteLn('');//write the command to the client TIdPeerThread(List.Items[Count]).Connection.WriteLn('edit3.text'); end; finally IdTCPServer1.threads.UnlockList; end; end; end.
Partager