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
|
private void Attente_De_Donnees()
{
Thread_Recp_Donnée = new Thread(Reception_Donnees);
Thread_Recp_Donnée.Start();
}
private void Reception_Donnees()
{
TcpListener server = null;
String Donnee_Recu = null;
int Taille = 0;
LbErreur.Text = "";
Date = null;
Heure = null;
Temperature = null;
Unite = null;
try
{
Int32 port = 13000; // selection du port
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
server = new TcpListener(localAddr, port);
server.Start(); // debut de l'ecoute du port
Byte[] bytes = new Byte[256];//Declaration du buffer
TcpClient client = server.AcceptTcpClient();
// LbEtatConnexion.Text = "Connecté";
Donnee_Recu = null;
NetworkStream stream = client.GetStream();
Taille = stream.Read(bytes, 0, bytes.Length);
Donnee_Recu = System.Text.Encoding.ASCII.GetString(bytes, 0, Taille); //convertie les byte en ascii
Donnee_Recu = Donnee_Recu.ToUpper(); // Process the data sent by the client.
byte[] msg = System.Text.Encoding.ASCII.GetBytes(Donnee_Recu);
client.Close(); // ferme la connexion
}
catch(SocketException ex)
{
LbEtatConnexion.Text = "Erreur de connection:" + ex;
LbErreur.Text = "Erreur : ";
LbEtatConnexion2.Text = ex.Message;
LbTest.Text = ex.Message;
}
finally
{
server.Stop();// Stop listening for new clients.
} |
Partager