Bonjour,
j'aimerais transmettre des données de type float, "courant", "tension" et "température" d'un serveur à un client par socket, mais je doute de la taille du tableau et le client reçoit seulement des codes ascii...
J'essaie pour l'instant de taper des valeurs dans les champs et de les recevoir côté client, comme ci-dessous:
Voilà le code du 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 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; namespace TestServeur { public partial class Form1 : Form { Thread Thread1; Socket sock; Socket sockClient; //byte[] tabTension = new byte[50]; //byte[] tabCourant = new byte[50]; //byte[] tabTemperature = new byte[50]; byte[] tabDonnees = new byte[50]; public Form1() { InitializeComponent(); //création de l'objet socle de communication Thread1 = new Thread(new ThreadStart(T1)); sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } public void T1() { string IP; IP = texteBoxIP.Text; string Tension; string Temperature; string Courant; string Donnees; Tension = textBoxTension.Text; Courant = textBoxCourant.Text; Temperature = textBoxTemperature.Text; Donnees = Tension + ';' + Courant + ';' + Temperature ; //tabTension = System.Text.Encoding.ASCII.GetBytes(Tension.ToCharArray()); //tabCourant = System.Text.Encoding.ASCII.GetBytes(Courant.ToCharArray()); //tabTemperature = System.Text.Encoding.ASCII.GetBytes(Temperature.ToCharArray()); tabDonnees = System.Text.Encoding.ASCII.GetBytes(Donnees.ToCharArray()); Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //sock.Bind(new IPEndPoint(IPAddress.Parse("192.168.1.103"), 4000)); sock.Bind(new IPEndPoint(IPAddress.Parse(IP), 4000)); sock.Listen(1); sockClient = sock.Accept(); //sockClient.Send(tabTension); //sockClient.Send(tabCourant); //sockClient.Send(tabTemperature); sockClient.Send(tabDonnees); } private void button2_Click(object sender, EventArgs e) { //fin de l'émission et reception sock.Shutdown(SocketShutdown.Both); //fermeture la liaison par socle sock.Close(); //ferme l'application this.Close(); } private void Form1_Load(object sender, EventArgs e) { } private void timer1_Tick(object sender, EventArgs e) { } private void buttonEcoute_Click_1(object sender, EventArgs e) { Thread1.Start(); } } }
Et celui du client :
Merci d'avance pour votre aide !
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 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; namespace TestClient { public partial class Form1 : Form { Socket sock; Thread Thread1; string Tension; string Courant; string Temperature; string recuDonnees; public Form1() { InitializeComponent(); //création de l'objet socle de communication Thread1 = new Thread(new ThreadStart(T1)); sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } public void T1() { string IP ; IP = texteBoxIP.Text; //demande de connexion au serveur sock.Connect(new IPEndPoint(IPAddress.Parse(IP), 4000)); //sock.Connect(new IPEndPoint(IPAddress.Parse("192.168.1.103"), 4000)); //Lecture de la réponse du serveur, création d'un tableau dynamique. //byte[] bufferTension = new byte[50]; //byte[] bufferCourant = new byte[50]; //byte[] bufferTemperature = new byte[50]; byte[] bufferDonnees = new byte[50]; //receive lit les données dans une mémoire tampon de réception //si le nombre de caractères envoyés par le serveur est > à 50, il faut //appeler de nouveau Receive().Receive()est bloquant (lecture synchrone) while (Thread.CurrentThread.IsAlive) { int i = 0; int n = sock.Receive(bufferDonnees); System.Text.Encoding.ASCII.GetString(bufferDonnees).ToCharArray(); string[] tabDonnees = {ASCIIEncoding.Default.GetString(bufferDonnees)}; while (bufferDonnees[i]!= ';') { Tension += bufferDonnees[i]; i++; }; i++; while (bufferDonnees[i] != ';') { Courant += bufferDonnees[i]; i++; } i++; int TaillebufferDonnees = bufferDonnees.Length; while (bufferDonnees[i] != TaillebufferDonnees) { Temperature += bufferDonnees[i]; i++; }; } } private void Form1_Load(object sender, EventArgs e) { } private void BoutonQuitter_Click_1(object sender, EventArgs e) { //fin de l'émission et reception sock.Shutdown(SocketShutdown.Both); //fermeture la liaison par socle sock.Close(); //ferme l'application this.Close(); } private void BoutonConnexion_Click_1(object sender, EventArgs e) { Thread1.Start(); } private void textBoxReception_TextChanged(object sender, EventArgs e) { } private void timer1_Tick(object sender, EventArgs e) { textBoxTension.Text = Tension; textBoxCourant.Text = Courant; textBoxTemperature.Text = Temperature; //Text = recuDonnees; } } }
Partager