Bonjour,

J'ai un code tout simple qui me permet d'envoyer un message à un serveur via un port TCP et de récupérer sa réponse.
Le problème, c'est que si ladite réponse contient un accent (comme "Utilisateur inexistant dans la base de données" par exemple), l'affichage à l'écran n'est pas beau du tout (cela donne "Utilisateur inexistant dans la base de donn?es" pour l'exemple précédent).

Régler ce problème doit être tout simple, mais à ma grande honte, la solution m'élude...

Y aurait-il un sauveur dans la salle ?


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
TcpClient client = new TcpClient(server, port);
 
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
 
// Get a client stream for reading and writing.
//  Stream stream = client.GetStream();
 
NetworkStream stream = client.GetStream();
 
// Send the message to the connected TcpServer. 
stream.Write(data, 0, data.Length);
 
// Receive the TcpServer.response.
 
// Buffer to store the response bytes.
data = new Byte[256];
 
// String to store the response ASCII representation.
String responseData = String.Empty;
 
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
if (responseData.Contains("ACQ=OK") != true)
{
	int start = responseData.IndexOf("ERR=");
	LabelMessage.Text = "Erreur !\n" + responseData.Substring(start + 4, responseData.Length - (start + 4) - 5);
}
else
{
	LabelMessage.Text = "OK !";
}
 
// Close everything.
stream.Close();
client.Close();