Skip to content
Snippets Groups Projects
Commit ab752f6c authored by HOFFMANN MARTIN's avatar HOFFMANN MARTIN
Browse files

Fin de la première version client serveur en boucle

parent 13a390a8
Branches
Tags v1.0
No related merge requests found
Showing
with 470 additions and 0 deletions
File added
File added
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TP1_Chat
{
public enum Commande
{
POST, GET, HELP, QUIT, STOPSERVEUR, SUBSCRIBE, SUBSCRIBEv2, UNSUBSCRIBE
};
public enum CommandeType { REQUETE, REPONSE };
class ChatMessage
{
public const int bufferSize = 1500;
public Commande commande; // commande
public CommandeType commandeType; // type (Requête/Réponse)
public int dataSize; // taille de la donnée
public String data; // données de la commande
public String pseudo;
public ChatMessage(Commande commande, CommandeType type, String data, String pseudo)
{
this.commande = commande;
this.commandeType = type;
this.dataSize = data.Length;
this.data = data;
this.pseudo = pseudo;
}
public ChatMessage(byte[] buffer)
{
this.commande = (Commande)buffer[0];
this.commandeType = (CommandeType)buffer[1];
int taillePseudo = BitConverter.ToInt32(buffer, 2);
this.pseudo = System.Text.Encoding.ASCII.GetString(buffer, 6, taillePseudo);
int tailleData = BitConverter.ToInt32(buffer, 26);
this.data = System.Text.Encoding.ASCII.GetString(buffer, 30, tailleData);
}
public byte[] GetBytes()
{
byte[] buffer = new byte[bufferSize];
buffer[0] = (byte)this.commande;
buffer[1] = (byte)this.commandeType;
// On met dans un tableau la taille de la chaine
byte[] taillePseudo = BitConverter.GetBytes(this.pseudo.Length);
// Ecriture de la taille dans le message
for (int i = 0; i < 4; i++)
{
buffer[i + 2] = taillePseudo[i];
// Fin à 6
}
Encoding.ASCII.GetBytes(this.pseudo, 0, this.pseudo.Length, buffer, 6);
// On laisse 20 cara pour le pseudo
// On récupe la taille
byte[] tailleData = BitConverter.GetBytes(this.data.Length);
for (int i = 0; i < 4; i++)
{
buffer[i + 26] = tailleData[i];
// Fin à 30
}
Encoding.ASCII.GetBytes(this.data, 0, this.data.Length, buffer, 30);
return buffer;
}
public static byte[] GetBytes(Commande commande, CommandeType type, String data, String pseudo)
{
ChatMessage chatCommand = new ChatMessage(commande, type, data, pseudo);
return chatCommand.GetBytes();
}
public override string ToString()
{
return "[" + commande + "," + commandeType + ",\"" + pseudo + "\"," + dataSize + ",\"" + data + "\"]";
}
static void Main(string[] args)
{
ChatMessage m = new ChatMessage(Commande.GET, CommandeType.Requete, "Salut", "Michel");
Console.WriteLine("Hi");
Console.ReadLine();
}
}
}

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2027
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TP1_Chat", "TP1_Chat\TP1_Chat.csproj", "{3FF6E009-506E-4126-BE74-0BE3A0AEF15F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3FF6E009-506E-4126-BE74-0BE3A0AEF15F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3FF6E009-506E-4126-BE74-0BE3A0AEF15F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3FF6E009-506E-4126-BE74-0BE3A0AEF15F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3FF6E009-506E-4126-BE74-0BE3A0AEF15F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9DA979CA-5D80-4A35-92A1-0C7AE458EF23}
EndGlobalSection
EndGlobal
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TP1_Chat
{
public enum Commande
{
POST, GET, HELP, QUIT, STOPSERVEUR, SUBSCRIBE, SUBSCRIBEv2, UNSUBSCRIBE
};
public enum CommandeType { REQUETE, REPONSE };
class ChatMessage
{
public const int bufferSize = 1500;
public Commande commande; // commande
public CommandeType commandeType; // type (Requête/Réponse)
public int dataSize; // taille de la donnée
public String data; // données de la commande
public String pseudo;
public ChatMessage(Commande commande, CommandeType type, String data, String pseudo)
{
this.commande = commande;
this.commandeType = type;
this.dataSize = data.Length;
this.data = data;
this.pseudo = pseudo;
}
public ChatMessage(byte[] buffer)
{
this.commande = (Commande)buffer[0];
this.commandeType = (CommandeType)buffer[1];
int taillePseudo = BitConverter.ToInt32(buffer, 2);
this.pseudo = System.Text.Encoding.ASCII.GetString(buffer, 6, taillePseudo);
this.dataSize = BitConverter.ToInt32(buffer, 26);
this.data = System.Text.Encoding.ASCII.GetString(buffer, 30, dataSize);
}
public byte[] GetBytes()
{
byte[] buffer = new byte[bufferSize];
buffer[0] = (byte)this.commande;
buffer[1] = (byte)this.commandeType;
// On met dans un tableau la taille de la chaine
byte[] taillePseudo = BitConverter.GetBytes(this.pseudo.Length);
// Ecriture de la taille dans le message
for (int i = 0; i < 4; i++)
{
buffer[i + 2] = taillePseudo[i];
// Fin à 6
}
Encoding.ASCII.GetBytes(this.pseudo, 0, this.pseudo.Length, buffer, 6);
// On laisse 20 cara pour le pseudo
// On récupe la taille
byte[] tailleData = BitConverter.GetBytes(this.data.Length);
for (int i = 0; i < 4; i++)
{
buffer[i + 26] = tailleData[i];
// Fin à 30
}
Encoding.ASCII.GetBytes(this.data, 0, this.data.Length, buffer, 30);
return buffer;
}
public override string ToString()
{
return "[" + commande + "," + commandeType + ",\"" + pseudo + "\"," + dataSize + ",\"" + data + "\"]";
}
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TP1_Chat;
namespace ClientUdp
{
class ClientUdp
{
static void Main(string[] args)
{
try
{
//************************************************************** Initialisation
string serverIP = "192.168.43.123"; // A changer
int serverPort = 1000; // A changer
// Création de la socket d'écoute UDP
Socket clientSocket = new Socket(
AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
// Liaison de la socket au point de communication
clientSocket.Bind(new IPEndPoint(IPAddress.Any, 22222));
// Création du EndPoint serveur
EndPoint serverEP = new IPEndPoint(IPAddress.Parse(serverIP), serverPort);
// Lecture message au clavier
Console.Write("? ");
String msg = Console.ReadLine();
//************************************************************** Communications
ChatMessage message;
while (!msg.Equals("exit"))
{
message = new ChatMessage(Commande.POST, CommandeType.REQUETE, msg, "martin");
// Encodage du string dans un buffer de bytes en ASCII
byte[] buffer = message.GetBytes();
Console.WriteLine("Taille buffer : " + buffer.Length);
// Envoie du message au serveur
int nBytes = clientSocket.SendTo(buffer, 0, buffer.Length, SocketFlags.None, serverEP);
//** Communications
Console.WriteLine("Nouveau message envoye vers "
+ serverEP
+ " (" + nBytes + " octets)"
+ ": \"" + msg + "\"");
Console.Write("? ");
msg = Console.ReadLine();
}
//************************************************************** Conclusion
// Fermeture socket
Console.WriteLine("Fermeture Socket...");
clientSocket.Close();
}
catch (SocketException E)
{
Console.WriteLine(E.Message);
Console.ReadKey();
}
Console.ReadKey();
}
}
}
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Les informations générales relatives à un assembly dépendent de
// l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations
// associées à un assembly.
[assembly: AssemblyTitle("TP1_Chat")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TP1_Chat")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly
// aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de
// COM, affectez la valeur true à l'attribut ComVisible sur ce type.
[assembly: ComVisible(false)]
// Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM
[assembly: Guid("3ff6e009-506e-4126-be74-0be3a0aef15f")]
// Les informations de version pour un assembly se composent des quatre valeurs suivantes :
//
// Version principale
// Version secondaire
// Numéro de build
// Révision
//
// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
// en utilisant '*', comme indiqué ci-dessous :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TP1_Chat;
namespace ServeurUdp
{
class ServeurUdp
{
static void Main(string[] args)
{
try
{
// ************************************************************** Initialisation
// Création de la socket d'écoute UDP
Socket serverSocket = new Socket(
AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
// Liaison de la socket au point de communication
serverSocket.Bind(new IPEndPoint(IPAddress.Any, 1000));
//************************************************************** Communications
Console.WriteLine("Attente d'une nouveau message...");
// Reception message client
EndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);
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 message = new ChatMessage(buffer);
while (true)
{
message = new ChatMessage(buffer);
Console.WriteLine("Nouveau message de "
+ clientEP
+ " (" + nBytes + " octets)"
+ ": \"" + message.pseudo + " " + message.data + "\"");
Console.WriteLine("Attente d'une nouveau message...");
// Reception message client
nBytes = serverSocket.ReceiveFrom(buffer, buffer.Length, SocketFlags.None, ref clientEP);
}
//************************************************************** Conclusion
// Fermeture socket
Console.WriteLine("Fermeture Socket...");
serverSocket.Close();
}
catch (SocketException E)
{
Console.WriteLine(E.Message);
Console.ReadKey();
}
Console.ReadKey();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{3FF6E009-506E-4126-BE74-0BE3A0AEF15F}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>TP1_Chat</RootNamespace>
<AssemblyName>TP1_Chat</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ChatMessage.cs" />
<Compile Include="ClientUdp.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ServeurUdp.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
File added
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
\ No newline at end of file
File added
File added
f0a79b95a9395d6bf6491ba22fcecf69a04b60b4
D:\Cours\Année2\S32\TP1_Chat\TP1_Chat\bin\Debug\TP1_Chat.exe.config
D:\Cours\Année2\S32\TP1_Chat\TP1_Chat\bin\Debug\TP1_Chat.exe
D:\Cours\Année2\S32\TP1_Chat\TP1_Chat\bin\Debug\TP1_Chat.pdb
D:\Cours\Année2\S32\TP1_Chat\TP1_Chat\obj\Debug\TP1_Chat.csprojResolveAssemblyReference.cache
D:\Cours\Année2\S32\TP1_Chat\TP1_Chat\obj\Debug\TP1_Chat.csproj.CoreCompileInputs.cache
D:\Cours\Année2\S32\TP1_Chat\TP1_Chat\obj\Debug\TP1_Chat.exe
D:\Cours\Année2\S32\TP1_Chat\TP1_Chat\obj\Debug\TP1_Chat.pdb
D:\Cours\Année2\S32\S32\TP1_Chat\TP1_Chat\bin\Debug\TP1_Chat.exe.config
D:\Cours\Année2\S32\S32\TP1_Chat\TP1_Chat\bin\Debug\TP1_Chat.exe
D:\Cours\Année2\S32\S32\TP1_Chat\TP1_Chat\bin\Debug\TP1_Chat.pdb
D:\Cours\Année2\S32\S32\TP1_Chat\TP1_Chat\obj\Debug\TP1_Chat.csprojResolveAssemblyReference.cache
D:\Cours\Année2\S32\S32\TP1_Chat\TP1_Chat\obj\Debug\TP1_Chat.csproj.CoreCompileInputs.cache
D:\Cours\Année2\S32\S32\TP1_Chat\TP1_Chat\obj\Debug\TP1_Chat.exe
D:\Cours\Année2\S32\S32\TP1_Chat\TP1_Chat\obj\Debug\TP1_Chat.pdb
File added
File added
File added
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