Commit 4bcb5857 authored by teyermann's avatar teyermann
Browse files

debut chat client + server

parent 13a390a8
*.vs*
......@@ -5,7 +5,8 @@ using System.Text;
namespace ClientUdp
{
public enum Commande {
public enum Commande
{
POST, GET, HELP, QUIT, STOPSERVEUR, SUBSCRIBE, SUBSCRIBEv2, UNSUBSCRIBE
};
......@@ -14,26 +15,60 @@ namespace ClientUdp
class ChatMessage
{
public const int bufferSize = 1500;
public const int pseudoSize = 30;
public Commande commande; // commande
public CommandeType commandeType; // type (Requête/Réponse)
public int dataSize; // taille de la donnée
public String pseudo; // pseudo
public String data; // données de la commande
public ChatMessage(Commande commande, CommandeType type, String data)
public ChatMessage(Commande commande, CommandeType type, String pseudo, String data)
{
this.commande = commande;
this.commandeType = type;
this.dataSize = data.Length;
this.pseudo = pseudo;
this.data = data;
}
public ChatMessage(byte[] buffer)
{
int i = 2;
if (buffer.Length > bufferSize)
throw new ArgumentException("Buffer trop grand");
this.commande = (Commande)buffer[0];
this.commandeType = (CommandeType)buffer[1];
while (i < pseudoSize && Encoding.ASCII.GetString(buffer, i, 1) != "\0")
{
this.pseudo += Encoding.ASCII.GetString(buffer, i, 1);
}
i = pseudoSize + 2;
while (i < bufferSize && Encoding.ASCII.GetString(buffer, i, 1) != "\0")
{
this.data += Encoding.ASCII.GetString(buffer, i, 1);
}
}
public byte[] GetBytes()
{
byte[] res = new byte[bufferSize];
res[0] = (byte)this.commande;
res[1] = (byte)this.commandeType;
//encodage du pseudo et ajout au tableau de bytes
Encoding.ASCII.GetBytes(this.pseudo, 0, this.pseudo.Length, res, 2);
//encodage du message et ajout au tableau de bytes
Encoding.ASCII.GetBytes(this.data, 0, this.data.Length, res, pseudoSize + 2);
return res;
}
public override string ToString()
......

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual C# Express 2010
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2018
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "clientUdp", "clientUdp\clientUdp.csproj", "{0F2D151A-DDD4-4AAD-8FAA-B58B6C4BE47E}"
EndProject
Global
......@@ -17,4 +19,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CEA829F0-3EBB-4274-B544-54538DA17321}
EndGlobalSection
EndGlobal
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClientUdp
{
public enum Commande
{
POST, GET, HELP, QUIT, STOPSERVEUR, SUBSCRIBE, SUBSCRIBEv2, UNSUBSCRIBE
};
public enum CommandeType { REQUETE, REPONSE };
class ChatMessage
{
public const int bufferSize = 1500;
public const int pseudoSize = 30;
public Commande commande; // commande
public CommandeType commandeType; // type (Requête/Réponse)
public int dataSize; // taille de la donnée
public String pseudo; // pseudo
public String data; // données de la commande
public ChatMessage(Commande commande, CommandeType type, String pseudo, String data)
{
this.commande = commande;
this.commandeType = type;
this.dataSize = data.Length;
this.pseudo = pseudo;
this.data = data;
}
public ChatMessage(byte[] buffer)
{
int i = 2;
if (buffer.Length > bufferSize)
throw new ArgumentException("Buffer trop grand");
this.commande = (Commande)buffer[0];
this.commandeType = (CommandeType)buffer[1];
while (i < pseudoSize + 2 && buffer[i] != 0)
{
this.pseudo += Encoding.ASCII.GetString(buffer, i, 1);
i++;
}
i = pseudoSize + 2;
while (i < bufferSize && buffer[i] != 0)
{
this.data += Encoding.ASCII.GetString(buffer, i, 1);
i++;
}
}
public byte[] GetBytes()
{
byte[] res = new byte[bufferSize];
res[0] = (byte)this.commande;
res[1] = (byte)this.commandeType;
//encodage du pseudo et ajout au tableau de bytes
Encoding.ASCII.GetBytes(this.pseudo, 0, this.pseudo.Length, res, 2);
//encodage du message et ajout au tableau de bytes
Encoding.ASCII.GetBytes(this.data, 0, this.data.Length, res, pseudoSize + 3);
return res;
}
public override string ToString()
{
return "[" + commande + "," + commandeType + ",\"" + pseudo + "\"," + dataSize + ",\"" + data + "\"]";
}
}
}
......@@ -14,10 +14,9 @@ namespace ClientUdp
{
try
{
//************************************************************** Initialisation
string serverIP = "0.0.0.0"; // A changer
int serverPort = 000000 ; // A changer
string serverIP = "127.0.0.1"; // A changer
int serverPort = 11111; // A changer
// Création de la socket d'écoute UDP
......@@ -34,16 +33,12 @@ namespace ClientUdp
// Création du EndPoint serveur
EndPoint serverEP = new IPEndPoint(IPAddress.Parse(serverIP), serverPort);
// Lecture message au clavier
Console.Write("? ");
String msg = Console.ReadLine();
ChatMessage chatM = new ChatMessage(Commande.GET, CommandeType.REQUETE, "Thomas", "Test message");
//************************************************************** Communications
// Encodage du string dans un buffer de bytes en ASCII
byte[] buffer = System.Text.Encoding.ASCII.GetBytes(msg);
byte[] buffer = chatM.GetBytes();
Console.WriteLine("Taille buffer : "+buffer.Length);
// Envoie du message au serveur
......@@ -52,7 +47,7 @@ namespace ClientUdp
Console.WriteLine("Nouveau message envoye vers "
+ serverEP
+ " (" + nBytes + " octets)"
+ ": \"" + msg + "\"");
+ ": \"" + chatM + "\"");
//************************************************************** Conclusion
......@@ -60,7 +55,6 @@ namespace ClientUdp
Console.WriteLine("Fermeture Socket...");
clientSocket.Close();
}
catch (SocketException E)
{
......
File added
File added
......@@ -58,6 +58,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ChatMessage.cs" />
<Compile Include="ClientUdp.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
......
02c6a2f9ad3976ec6b0d6a020599dccd1ab07d5a
D:\Cours IUT INFO\Semestre 3\S32\ProjetS32\S32\Chat\clientUdp\clientUdp\bin\Debug\clientUdp.exe
D:\Cours IUT INFO\Semestre 3\S32\ProjetS32\S32\Chat\clientUdp\clientUdp\bin\Debug\clientUdp.pdb
D:\Cours IUT INFO\Semestre 3\S32\ProjetS32\S32\Chat\clientUdp\clientUdp\obj\x86\Debug\clientUdp.csprojAssemblyReference.cache
D:\Cours IUT INFO\Semestre 3\S32\ProjetS32\S32\Chat\clientUdp\clientUdp\obj\x86\Debug\clientUdp.csproj.CoreCompileInputs.cache
D:\Cours IUT INFO\Semestre 3\S32\ProjetS32\S32\Chat\clientUdp\clientUdp\obj\x86\Debug\clientUdp.exe
D:\Cours IUT INFO\Semestre 3\S32\ProjetS32\S32\Chat\clientUdp\clientUdp\obj\x86\Debug\clientUdp.pdb
File added
File added
File added

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual C# Express 2010
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2018
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "serveurUdp", "serveurUdp\serveurUdp.csproj", "{7FD485EB-1D1E-4208-868C-5994E555E8AE}"
EndProject
Global
......@@ -17,4 +19,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6790DBDC-DA79-4222-A40D-C6C68E77E66A}
EndGlobalSection
EndGlobal
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClientUdp
{
public enum Commande
{
POST, GET, HELP, QUIT, STOPSERVEUR, SUBSCRIBE, SUBSCRIBEv2, UNSUBSCRIBE
};
public enum CommandeType { REQUETE, REPONSE };
class ChatMessage
{
public const int bufferSize = 1500;
public const int pseudoSize = 30;
public Commande commande; // commande
public CommandeType commandeType; // type (Requête/Réponse)
public int dataSize; // taille de la donnée
public String pseudo; // pseudo
public String data; // données de la commande
public ChatMessage(Commande commande, CommandeType type, String pseudo, String data)
{
this.commande = commande;
this.commandeType = type;
this.dataSize = data.Length;
this.pseudo = pseudo;
this.data = data;
}
public ChatMessage(byte[] buffer)
{
int i = 2;
if (buffer.Length > bufferSize)
throw new ArgumentException("Buffer trop grand");
this.commande = (Commande)buffer[0];
this.commandeType = (CommandeType)buffer[1];
while (i < pseudoSize+2 && buffer[i] != 0)
{
this.pseudo += Encoding.ASCII.GetString(buffer, i, 1);
i++;
}
i = pseudoSize + 2;
while (i < bufferSize && buffer[i] != 0)
{
this.data += Encoding.ASCII.GetString(buffer, i, 1);
i++;
}
}
public byte[] GetBytes()
{
byte[] res = new byte[bufferSize];
res[0] = (byte)this.commande;
res[1] = (byte)this.commandeType;
//encodage du pseudo et ajout au tableau de bytes
Encoding.ASCII.GetBytes(this.pseudo, 0, this.pseudo.Length, res, 2);
//encodage du message et ajout au tableau de bytes
Encoding.ASCII.GetBytes(this.data, 0, this.data.Length, res, pseudoSize + 2);
return res;
}
public override string ToString()
{
return "[" + commande + "," + commandeType + ",\"" + pseudo + "\"," + dataSize + ",\"" + data + "\"]";
}
}
}
......@@ -5,6 +5,7 @@ using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClientUdp;
namespace ServeurUdp
{
......@@ -33,16 +34,15 @@ namespace ServeurUdp
// Reception message client
EndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);
byte[] buffer = new byte[80];
byte[] buffer = new byte[1500];
int nBytes = serverSocket.ReceiveFrom(buffer, buffer.Length, SocketFlags.None, ref clientEP);
// Decodage du buffer de bytes en ASCII vers un string
String msg = System.Text.Encoding.ASCII.GetString(buffer, 0, nBytes);
ChatMessage cm = new ChatMessage(buffer);
Console.WriteLine("Nouveau message de "
+ clientEP
+ " (" + nBytes + " octets)"
+ ": \"" + msg + "\"");
+ ": \"" + cm + "\"");
//************************************************************** Conclusion
......
File added
File added
Markdown is supported
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