Je ne parviens pas à, ne serait-ce que commencer un début de dialogue client / serveur
Je me suis inspiré de plusieurs sources un peu différentes mais qui au final reviennent au même.
Le serveur attend, et ne fait rien, jusqu'à être contacté par un client
Mais mon client se connecte (du moins en apparence) sans que le serveur le voit et ce à condition que j'utilise le port 143
si je désactive le serveur, le client se connecte quand même (je ne sais où!)
Je suis un tout petit peu perdu
Serveur:****************************************
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 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net.Sockets; using System.Net; namespace dserver { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public Socket sock=null, actif=null; string iplocale; private void Form1_Load(object sender, EventArgs e) { sock = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.0.106"), 17000);//143 IPEndPoint iep = new IPEndPoint(IPAddress.Any, 143);//143 iplocale = iep.Address.ToString(); sock.Bind(iep); sock.Listen(1); // Socket actif = sock.Accept(); this.Text = "ok"; timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { //ne sert qu'à différer sock.Accept timer1.Stop(); this.Text =iplocale + " en atente de connection.."; actif = sock.Accept(); this.Text = "Connecté!"; } } }
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 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System.Net.Sockets; namespace dClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public Socket sock; public IPEndPoint iep; int i = 0; private void Form1_Load(object sender, EventArgs e) { sock = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string ip = "179.218.193.77"; //port redirigés dans routeur //ip="192.168.0.106";//pour tester en local iep = new IPEndPoint(IPAddress.Parse(ip),143);//143 } private void con_Click(object sender, EventArgs e) { //pour maitriser sock.connect sock.Connect(iep); this.Text = "connecté " ; } } }
Partager