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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Xml.Serialization;
using System.Collections;
namespace TestService
{
class Server
{
#region declaration
public static bool service_on = false;//l'etat du service
public static ArrayList liste = new ArrayList();//liste des sockets connectées
public static Socket main_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public static Thread[] test = new Thread[50];
public static Thread[] worker_clients = new Thread[50];//les threads d'ecoute a chaque socket connecté
public static Thread listen = new Thread(new ThreadStart(listener));//thread pour ecouter les connexions entrantes
public static int count = 0;
public static int count1 = 0;
public static Socket client;
//nombre de clients connectés
public static bool conect = false;
// public static LS515 scanner = new LS515();
public static String codebarr = "";
#endregion
//sreialisation du message
private static byte[] serialisation(String msg)
{
MemoryStream ms = new MemoryStream();
XmlSerializer ser = new XmlSerializer(typeof(String));
ser.Serialize(ms, msg);
return (ms.ToArray());
}
//serialisation d'une image
private static byte[] serialisationimage(FileStream image)
{
MemoryStream ms = new MemoryStream();
XmlSerializer ser = new XmlSerializer(typeof(FileStream));
ser.Serialize(ms, image);
return (ms.ToArray());
}
//desrialisation du buffer recu
private static String deserialisation(Byte[] buffer)
{
String msg;
MemoryStream ms = new MemoryStream(buffer);
XmlSerializer deser = new XmlSerializer(typeof(String));
msg = (String)deser.Deserialize(ms);
ms.Close();
return (msg);
}
//lancement du serveur
public void Start()
{
if (!service_on)
{
service_on = true;
count = 0;
count1 = 0;
listen.Start();
Console.WriteLine("En écoute sur le port:" + 8001.ToString());
}
else
{
service_on = false;
main_socket.Close();
listen.Abort();
foreach (Thread th in worker_clients)
{
try
{
th.Abort();
}
catch (Exception) { }
}
}
}
private static void listener()
{
main_socket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001));
main_socket.Listen(100);
{
while (service_on)
{
try
{
client = main_socket.Accept();
sayhello(client);
conect = true;
liste.Add(client);
// Thread test1 = new Thread(new ThreadStart(envoie));
// test1.Start();
test[count] = new Thread(new ParameterizedThreadStart(sayhello));
test[count].Start(client);
worker_clients[count] = new Thread(new ParameterizedThreadStart(receiver));
worker_clients[count].Start(client);
count++;
count1++;
}
catch (Exception) { }
}
}
}
//un thread pour recever les infos depuis la socket passée en parametres
//ok
private static void receiver(object socket)
{
Socket sc = socket as Socket;
while (service_on)
{
Byte[] buffer = new Byte[5000];
String msg;
try
{
sc.Receive(buffer);
msg = deserialisation(buffer);
//Console.WriteLine(msg);
Console.WriteLine(msg);
}
catch (Exception e) { };
}
}
//envoie des doonnées
public static void sayhello(object socket)
{
// object socket;
Socket sc = socket as Socket;
Byte[] buffer = new Byte[5000];
foreach (Socket s in liste)
{
if (s == sc)
{
while (sc.Connected)
{
try
{
// Console.ReadLine();
String msg = "hello";
buffer = serialisation(msg);
s.Send(buffer);
}
catch (Exception e) { Console.WriteLine(e.ToString()); }
}
}
}
}
}
} |
Partager