Skip to content
Snippets Groups Projects
Commit 50f70ce5 authored by Cyril HECKMANN's avatar Cyril HECKMANN
Browse files

Ajout Subscribe et unsubscribe: Fonctionnel

parent a6f1eb9e
No related merge requests found
......@@ -29,6 +29,8 @@ namespace ClientUdp
}
static void Main(string[] args)
{
Console.WriteLine("Port(pas 11111)?");
int portClient = Convert.ToInt32(Console.ReadLine());
try
{
......@@ -53,7 +55,7 @@ namespace ClientUdp
// Liaison de la socket au point de communication
clientSocket.Bind(new IPEndPoint(IPAddress.Any, 22222));
clientSocket.Bind(new IPEndPoint(IPAddress.Any, portClient));
// Création du EndPoint serveur
......
......@@ -14,6 +14,7 @@ namespace ServeurUdp
static List<ChatMessage> Allmessages = new List<ChatMessage>();
static bool listen = true;
static List<EndPoint> subscribers = new List<EndPoint>();
static void SendAllMsg(Socket servSocket, EndPoint dest)
{
......@@ -28,6 +29,19 @@ namespace ServeurUdp
Console.WriteLine("All messages send to " + dest);
}
static void SendToSub(Socket servSocket, List<EndPoint> dests, ChatMessage msg)
{
byte[] buffer;
foreach (EndPoint ep in dests)
{
// Encodage du string dans un buffer de bytes en ASCII
buffer = msg.GetBytes();
// Envoie du message
int nBytes = servSocket.SendTo(buffer, 0, buffer.Length, SocketFlags.None, ep);
}
Console.WriteLine("Message send to subscribers");
}
static void Main(string[] args)
{
try
......@@ -72,13 +86,24 @@ namespace ServeurUdp
+ ": \"" + msg + "\"");
// ========= Gestion de commande ===============
if (receive.commande == Commande.POST)
Allmessages.Add(receive);
else if (receive.commande == Commande.GET)
Thread th;
switch(receive.commande)
{
// !!!!!! Passage d'argument multiple à un thread !!!!!
Thread sendAll = new Thread(x => SendAllMsg(serverSocket,lastUser));
sendAll.Start();
case Commande.POST:
Allmessages.Add(receive);
th = new Thread(x => SendToSub(serverSocket, subscribers, receive));
th.Start();
break;
case Commande.GET:
th = new Thread(x => SendAllMsg(serverSocket, lastUser));
th.Start();
break;
case Commande.SUBSCRIBE:
subscribers.Add(lastUser);
break;
case Commande.UNSUBSCRIBE:
subscribers.Remove(lastUser);
break;
}
// ==========================================
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment