Citation :
private int tempCpt = 0;
private MySQL maConnexion;
private TcpListener monListener = null;
private TcpClient monClient = null;
private Boitier monBoitier = null;
private int portTCP;
private NetworkStream ns;
private Thread monThread = null;
private byte[] data;
private int recv;
private Principale monForm;
private delegate void affichageLog(string msg);
private DateTime dateTime;
private string longitude;
private string latitude;
private int azimut;
private int speed;
private int idEvenement;
private int oldIDEvenement;
public TCPConnexion(Principale form, MySQL connexion , Boitier boitier)
{
this.maConnexion = connexion;
this.monBoitier = boitier;
this.portTCP = monBoitier.get_port();
this.monForm = form;
this.monListener = new TcpListener(portTCP);
this.monListener.Start();
// Acceptation d'une nouvelle connexion
this.monClient = this.monListener.AcceptTcpClient();
ns = this.monClient.GetStream();
//lancement de l'écoute
this.ecouter();
}
private void ecouter()
{
try
{
if (monClient.Connected)
{
// Création et lancement d'un nouveau Thread
monThread = new Thread(recupData);
monThread.Start();
}
else
{
MessageBox.Show("Erreur de connexion : port : " + portTCP);
}
}
catch (Exception ex)
{
MessageBox.Show("Erreur TCPConnexion Port : " + portTCP + " : " + ex.ToString());
}
}
/* **********************************************************
* Récupération des données de la trame
* ******************************************************** */
private void recupData()
{
while (true)
{
data = new byte[1024];
recv = ns.Read(data, 0, data.Length);
if (recv == 0)
{
break;
}
else
{
string resultat = "";
for (int i = 0; i < recv; i++)
{
if (data[i] == 13)
{
if (resultat.StartsWith("\n" ))
{
resultat = resultat.Substring(1);
}
insertion(resultat);
monForm.lb_log.BeginInvoke(new affichageLog(log), new object[] { resultat });
resultat = "";
}
else
{
resultat = resultat + Encoding.ASCII.GetString(data, i, 1);
}
}
ns.Write(data, 0, recv);
}
}
}
|