Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (14)
Showing
with 154 additions and 28 deletions
......@@ -5,7 +5,8 @@ using System.Text;
namespace ClientUdp
{
public enum Commande {
public enum Commande
{
POST, GET, HELP, QUIT, STOPSERVEUR, SUBSCRIBE, SUBSCRIBEv2, UNSUBSCRIBE
};
......@@ -25,6 +26,7 @@ namespace ClientUdp
this.commande = commande;
this.commandeType = type;
this.dataSize = data.Length;
this.pseudo = pseudo;
this.data = data;
}
......@@ -44,10 +46,16 @@ namespace ClientUdp
Encoding.ASCII.GetBytes(data, 0, data.Length, b, 33);
}
public static byte[] GetBytes(Commande commande, CommandeType type, String pseudo, String data)
{
ChatMessage chatCommande = new ChatMessage(commande, type, pseudo, data);
return chatCommande.GetBytes;
}
public override string ToString()
{
return "[" + commande + "," + commandeType + ",\"" + pseudo + "\"," + dataSize + ",\"" + data + "\"]";
}
}
}
}
\ No newline at end of file
File added
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
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 = 20;
public const int messageSize = bufferSize - (pseudoSize + 2);
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;
if (pseudo.Length > pseudoSize)
{
pseudo = pseudo.Substring(0, pseudoSize);
}
this.pseudo = pseudo;
if (data.Length > messageSize)
{
data = data.Substring(pseudoSize+2, messageSize);
}
this.data = data;
}
public ChatMessage(byte[] buffer)
{
this.commande = (Commande)buffer[0];
this.commandeType = (CommandeType)buffer[1];
// Decodage du buffer de bytes en ASCII vers un string
this.pseudo = System.Text.Encoding.ASCII.GetString(buffer, 2, pseudoSize).TrimEnd('\0');
this.data = System.Text.Encoding.ASCII.GetString(buffer, pseudoSize+2, bufferSize-(pseudoSize+2)).TrimEnd('\0');
this.dataSize = data.Length;
}
public byte[] GetBytes()
{
byte[] b = new byte[bufferSize];
b[0] = (byte)this.commande;
b[1] = (byte)this.commandeType;
Encoding.ASCII.GetBytes(pseudo, 0, pseudo.Length, b, 2);
Encoding.ASCII.GetBytes(data, 0, data.Length, b, 22);
return b;
}
public static byte[] GetBytes(Commande commande, CommandeType type, String pseudo, String data)
{
ChatMessage chatCommande = new ChatMessage(commande, type, pseudo, data);
return chatCommande.GetBytes();
}
public override string ToString()
{
return "[" + commande + "," + commandeType + ",\"" + pseudo + "\"," + dataSize + ",\"" + data + "\"]";
}
}
}
\ No newline at end of file
......@@ -16,11 +16,9 @@ namespace ClientUdp
{
//************************************************************** Initialisation
string serverIP = "130.79.81.145"; // A changer
string serverIP = "130.79.81.203"; // A changer
int serverPort = 11111; // A changer
string[] tabMessage = { "Bonjour", "Maiko", "je", "suis", "le", "client", "tu", "es", "le", "ser", "veur", "caca" };
// Création de la socket d'écoute UDP
Socket clientSocket = new Socket(
AddressFamily.InterNetwork,
......@@ -35,31 +33,57 @@ namespace ClientUdp
// Création du EndPoint serveur
EndPoint serverEP = new IPEndPoint(IPAddress.Parse(serverIP), serverPort);
// Création du nouveau chat message
ChatMessage[] chatMessages = {
new ChatMessage(Commande.SUBSCRIBE, CommandeType.REQUETE, "William", "Je suis subscribe"),
//new ChatMessage(Commande.POST, CommandeType.REQUETE, "Jean-Pierre", "Hello World !"),
//new ChatMessage(Commande.POST, CommandeType.REQUETE, "Jacques", "aurevoir aurevoir"),
//new ChatMessage(Commande.UNSUBSCRIBE, CommandeType.REQUETE, "William", "Je suis unsubscribe"),
new ChatMessage(Commande.POST, CommandeType.REQUETE, "William", "aurevoir aurevoir"),
//new ChatMessage(Commande.GET, CommandeType.REQUETE, "caca", "pipi"),
/*new ChatMessage(Commande.STOPSERVEUR, CommandeType.REQUETE, "", "")*/};
// Lecture message au clavier
int i = 0;
while (i<12)
{
String msg = tabMessage[i];
//************************************************************** Communications
//************************************************************** Communications
foreach (ChatMessage chatMessage in chatMessages)
{
// Encodage du string dans un buffer de bytes en ASCII
byte[] buffer = System.Text.Encoding.ASCII.GetBytes(msg);
byte[] buffer = chatMessage.GetBytes();
Console.WriteLine("Taille buffer : " + buffer.Length);
// Envoie du message au serveur
int nBytes = clientSocket.SendTo(buffer, 0, buffer.Length, SocketFlags.None, serverEP);
Console.WriteLine("Nouveau message envoye vers "
+ serverEP
+ " (" + nBytes + " octets)"
+ ": \"" + msg + "\"");
i++;
int nBytes;
nBytes = clientSocket.SendTo(buffer, 0, buffer.Length, SocketFlags.None, serverEP);
Console.WriteLine("Nouveau message envoye vers: " + chatMessage);
switch (chatMessage.commande)
{
case Commande.POST:
break;
case Commande.GET:
break;
case Commande.STOPSERVEUR:
break;
case Commande.SUBSCRIBE:
while (true)
{
buffer = new Byte[ChatMessage.bufferSize];
nBytes = clientSocket.ReceiveFrom(buffer, buffer.Length, SocketFlags.None, ref serverEP);
ChatMessage cmRecuSub = new ChatMessage(buffer);
Console.WriteLine("Message reçu: " + cmRecuSub);
}
break;
case Commande.UNSUBSCRIBE:
break;
}
buffer = new Byte[ChatMessage.bufferSize];
nBytes = clientSocket.ReceiveFrom(buffer, buffer.Length, SocketFlags.None, ref serverEP);
ChatMessage cmRecu = new ChatMessage(buffer);
Console.WriteLine("Message reçu: " + cmRecu);
}
//************************************************************** Conclusion
// Fermeture socket
......@@ -71,7 +95,7 @@ namespace ClientUdp
Console.WriteLine(E.Message);
Console.ReadKey();
}
Console.ReadKey();
}
}
......
No preview for this file type
No preview for this file type
File added
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
......@@ -58,6 +58,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ChatMessage.cs" />
<Compile Include="ClientUdp.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
......
No preview for this file type
352632859b0aa355128ead04e1bc0c28bebf19d4
347d8d7d626bd3c2f6896d987099347a913054d2
\\vfiler-ad-etu.ad.unistra.fr\wguthmann\Bureau\S32\Chat\clientUdp\clientUdp\bin\Debug\clientUdp.exe
\\vfiler-ad-etu.ad.unistra.fr\wguthmann\Bureau\S32\Chat\clientUdp\clientUdp\bin\Debug\clientUdp.pdb
\\vfiler-ad-etu.ad.unistra.fr\wguthmann\Bureau\S32\Chat\clientUdp\clientUdp\obj\x86\Debug\clientUdp.csprojAssemblyReference.cache
\\vfiler-ad-etu.ad.unistra.fr\wguthmann\Bureau\S32\Chat\clientUdp\clientUdp\obj\x86\Debug\clientUdp.csproj.CoreCompileInputs.cache
\\vfiler-ad-etu.ad.unistra.fr\wguthmann\Bureau\S32\Chat\clientUdp\clientUdp\obj\x86\Debug\clientUdp.exe
\\vfiler-ad-etu.ad.unistra.fr\wguthmann\Bureau\S32\Chat\clientUdp\clientUdp\obj\x86\Debug\clientUdp.pdb
\\vfiler-ad-etu.ad.unistra.fr\wguthmann\Bureau\S32\Chat\clientUdp\clientUdp\obj\x86\Debug\clientUdp.csprojResolveAssemblyReference.cache
U:\Bureau\S32\Chat\clientUdp\clientUdp\bin\Debug\clientUdp.exe
U:\Bureau\S32\Chat\clientUdp\clientUdp\bin\Debug\clientUdp.pdb
U:\Bureau\S32\Chat\clientUdp\clientUdp\obj\x86\Debug\clientUdp.csprojAssemblyReference.cache
U:\Bureau\S32\Chat\clientUdp\clientUdp\obj\x86\Debug\clientUdp.csproj.CoreCompileInputs.cache
U:\Bureau\S32\Chat\clientUdp\clientUdp\obj\x86\Debug\clientUdp.exe
U:\Bureau\S32\Chat\clientUdp\clientUdp\obj\x86\Debug\clientUdp.pdb
No preview for this file type
No preview for this file type
File added