Temps de création d'un TcpClient
Bonjour
Je me lance dans le TCP IP (windows form) et je suis confronté à un temps de latence gênant
Sur un clic bouton j'ouvre un socket (enfin c'est ce que je pense faire) avec
Code:
TcpClient client = new TcpClient("nom machine", port);
et ça prend 1 seconde environ.
Est ce un temps incompressible ? d'où vient-il ?
Sinon sur quels leviers puis je travailler pour réduire ce temps ? Puis-je par exemple créer le TcpClient à l'ouverture de l'appli et ne le fermer qu'à la fermeture de l'appli ?
merci
Le code complet de mon envoi (puis attente de l'écho en réponse), le tout largement pompé dans les exemples du MSDN
Code:
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
| private void btEnvoi_Click(object sender, EventArgs e)
{
try {
//TcpClient client = new TcpClient(Dns.GetHostName(), port);
TcpClient client = new TcpClient("nom machine", port);
String message = "Coucou TCP !!!" + "<EOF>";
// 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);
TxtServeur.AppendText("envoi de : " + message + "\n");
// 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);
TxtServeur.AppendText("reception de : " + responseData +"\n\n");
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException ex)
{
TxtServeur.AppendText("erreur : " + ex + "\n");
}
catch (SocketException ex2)
{
TxtServeur.AppendText("Socket erreur : " + ex2 + "\n");
} |