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
|
protected override void OnStart(string[] args)
{
Log("Démarrage...");
ipLocal = IPAddress.Parse("127.0.0.1");
numPort = 135;
reception = new System.Net.Sockets.TcpListener(ipLocal,numPort);
numPort = 137;
reception2 = new System.Net.Sockets.TcpListener(ipLocal,numPort);
numPort = 138;
reception3 = new System.Net.Sockets.TcpListener(ipLocal,numPort);
numPort = 139;
reception4 = new System.Net.Sockets.TcpListener(ipLocal,numPort);
Log("TCPListener ok...");
reception.Start();
reception2.Start();
reception3.Start();
reception4.Start();
Log("TCPListener en écoute...");
ThreadStart st = new ThreadStart(ThreadSurveillance);
workerThread = new Thread(st);
ServiceStarted = true;
Log("Thread initialisé...");
workerThread.Start();
}
protected void ThreadSurveillance()
{
Log("Entrée dans thread...");
try
{
while (ServiceStarted)
{
TcpClient client = reception.AcceptTcpClient();
TcpClient client2 = reception2.AcceptTcpClient();
TcpClient client3 = reception3.AcceptTcpClient();
TcpClient client4 = reception4.AcceptTcpClient();
string data = "";
byte[] bytes = new byte[1024];
NetworkStream stream = client.GetStream();
NetworkStream stream2 = client2.GetStream();
NetworkStream stream3 = client3.GetStream();
NetworkStream stream4 = client4.GetStream();
int i;
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
}
while((i = stream2.Read(bytes, 0, bytes.Length))!=0)
{
data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
}
while((i = stream3.Read(bytes, 0, bytes.Length))!=0)
{
data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
}
while((i = stream4.Read(bytes, 0, bytes.Length))!=0)
{
data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
}
data = data.ToUpper();
ShowMsg(data);
client.Close();
}
}
catch (Exception ex)
{
Log(ex.Message +" / "+ ex.StackTrace);
}
Thread.CurrentThread.Abort();
} |
Partager