lang:csharp:net:client_serveur
Différences
Ci-dessous, les différences entre deux révisions de la page.
| lang:csharp:net:client_serveur [2019/01/17 13:54] – Création avec Client Tcp root | lang:csharp:net:client_serveur [2019/01/23 18:02] (Version actuelle) – Ajout de "TCP" root | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| =====TCP===== | =====TCP===== | ||
| + | ====TcpClient==== | ||
| + | ===Les interfaces=== | ||
| + | Les fonctionnalités du serveur sont dans une '' | ||
| + | <file csharp IServeurFonctions.cs> | ||
| + | namespace Common | ||
| + | { | ||
| + | public interface IServeurFonctions | ||
| + | { | ||
| + | bool ArretPoste(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| - | ====Client==== | + | Coté serveur : |
| + | <file csharp IServeur.cs> | ||
| + | namespace Serveur | ||
| + | { | ||
| + | interface IServeur | ||
| + | { | ||
| + | bool Connect(ushort port); | ||
| + | void Join(); | ||
| + | void StopListening(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <file csharp IServeurImpl.cs> | ||
| + | namespace Serveur | ||
| + | { | ||
| + | interface IServeurImpl : IServeur, IServeurFonctions | ||
| + | { | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Coté client : | ||
| + | <file csharp IClient.cs> | ||
| + | namespace Client | ||
| + | { | ||
| + | interface IClient | ||
| + | { | ||
| + | bool Connect(string adresse, ushort port); | ||
| + | void Disconnect(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <file csharp IClientImpl.cs> | ||
| + | namespace Client | ||
| + | { | ||
| + | interface IClientImpl : IClient, IServeurFonctions | ||
| + | { | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===Client=== | ||
| + | <file csharp ClientTcp.cs> | ||
| + | namespace | ||
| + | { | ||
| + | class ClientTcp : IClient, IDisposable | ||
| + | { | ||
| + | private TcpClient _tcpClient = new TcpClient(); | ||
| + | protected Stream StreamClient { get; private set; } | ||
| + | |||
| + | public bool Connect(string adresse, ushort port) | ||
| + | { | ||
| + | if (StreamClient != null) | ||
| + | return false; | ||
| + | try | ||
| + | { | ||
| + | _tcpClient.Connect(adresse, | ||
| + | } | ||
| + | catch (SocketException e) | ||
| + | { | ||
| + | if (e.SocketErrorCode == SocketError.ConnectionRefused) | ||
| + | return false; | ||
| + | throw; | ||
| + | } | ||
| + | StreamClient = _tcpClient.GetStream(); | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | public void Disconnect() | ||
| + | { | ||
| + | _tcpClient.Close(); | ||
| + | StreamClient? | ||
| + | StreamClient = null; | ||
| + | } | ||
| + | |||
| + | #region IDisposable Support | ||
| + | private bool disposedValue = false; // To detect redundant calls | ||
| + | |||
| + | protected virtual void Dispose(bool disposing) | ||
| + | { | ||
| + | if (!disposedValue) | ||
| + | { | ||
| + | if (disposing) | ||
| + | { | ||
| + | _tcpClient.Close(); | ||
| + | StreamClient? | ||
| + | } | ||
| + | |||
| + | // Free unmanaged resources (unmanaged objects) and override a finalizer below. | ||
| + | // Set large fields to null. | ||
| + | |||
| + | disposedValue = true; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources. | ||
| + | // ~AClient() { | ||
| + | // // Do not change this code. Put cleanup code in Dispose(bool disposing) above. | ||
| + | // | ||
| + | // } | ||
| + | |||
| + | // This code added to correctly implement the disposable pattern. | ||
| + | public void Dispose() | ||
| + | { | ||
| + | // Do not change this code. Put cleanup code in Dispose(bool disposing) above. | ||
| + | Dispose(true); | ||
| + | // Uncomment the following line if the finalizer is overridden above. | ||
| + | GC.SuppressFinalize(this); | ||
| + | } | ||
| + | # | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <file csharp ClientArretPosteTcp.cs> | ||
| + | namespace Client | ||
| + | { | ||
| + | class ClientArretPosteTcp : ClientTcp, IClientImpl | ||
| + | { | ||
| + | public bool ArretPoste() | ||
| + | { | ||
| + | byte[] ArretPoste = Encoding.ASCII.GetBytes(" | ||
| + | StreamClient.Write(ArretPoste, | ||
| + | byte[] bytes = new byte[1500]; | ||
| + | int nbOctets = StreamClient.Read(bytes, | ||
| + | string recu = Encoding.ASCII.GetString(bytes, | ||
| + | if (recu == " | ||
| + | return true; | ||
| + | return false; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===Serveur=== | ||
| + | <file csharp ServeurTcp.cs> | ||
| + | namespace Serveur | ||
| + | { | ||
| + | abstract class ServeurTcp : IServeur | ||
| + | { | ||
| + | private TcpListener _serveur; | ||
| + | |||
| + | public bool Connect(ushort port) | ||
| + | { | ||
| + | try | ||
| + | { | ||
| + | _serveur = new TcpListener(IPAddress.Any, | ||
| + | _serveur.Start(); | ||
| + | } | ||
| + | catch (SocketException e) | ||
| + | { | ||
| + | if (e.SocketErrorCode == SocketError.AddressAlreadyInUse) | ||
| + | return false; | ||
| + | throw; | ||
| + | } | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | protected abstract Action< | ||
| + | |||
| + | public void Join() | ||
| + | { | ||
| + | try | ||
| + | { | ||
| + | while (true) | ||
| + | { | ||
| + | TcpClient client = _serveur.AcceptTcpClient(); | ||
| + | ThreadPool.QueueUserWorkItem((objet) => Pooling(objet), | ||
| + | } | ||
| + | } | ||
| + | catch (SocketException e) | ||
| + | { | ||
| + | // On ignore les erreurs d' | ||
| + | if (e.SocketErrorCode != SocketError.Interrupted) | ||
| + | { | ||
| + | throw; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public void StopListening() | ||
| + | { | ||
| + | _serveur.Stop(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <file csharp ServeurStopPCTcp.cs> | ||
| + | namespace Serveur | ||
| + | { | ||
| + | class ServeurStopPCTcp : ServeurTcp, IServeurImpl | ||
| + | { | ||
| + | private const int WM_USER = 0x0400; | ||
| + | |||
| + | public bool ArretPoste() | ||
| + | { | ||
| + | Process.Start(" | ||
| + | |||
| + | return true; | ||
| + | } | ||
| + | |||
| + | protected override Action< | ||
| + | |||
| + | private static class AsyncClient | ||
| + | { | ||
| + | public static void Connection(object obj) | ||
| + | { | ||
| + | Tuple< | ||
| + | TcpClient client = data.Item1; | ||
| + | IServeurImpl serveur = data.Item2; | ||
| + | |||
| + | NetworkStream stream = client.GetStream(); | ||
| + | AsyncCallback callback = new AsyncCallback(DonneesRecues); | ||
| + | byte[] bytes = new byte[1500]; | ||
| + | stream.BeginRead(bytes, | ||
| + | } | ||
| + | |||
| + | private static void DonneesRecues(IAsyncResult ar) | ||
| + | { | ||
| + | Tuple< | ||
| + | NetworkStream stream = data.Item1; | ||
| + | int nbOctets = stream.EndRead(ar); | ||
| + | byte[] bytes = data.Item2; | ||
| + | string commande = Encoding.ASCII.GetString(bytes, | ||
| + | |||
| + | IServeurImpl serveur = data.Item3; | ||
| + | if (commande == " | ||
| + | { | ||
| + | if (serveur.ArretPoste()) | ||
| + | bytes = Encoding.ASCII.GetBytes(" | ||
| + | else | ||
| + | bytes = Encoding.ASCII.GetBytes(" | ||
| + | stream.Write(bytes, | ||
| + | stream.Close(); | ||
| + | serveur.StopListening(); | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | // On lance l' | ||
| + | stream.BeginRead(bytes, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ====HTTP / HTTPS==== | ||
| <code csharp> | <code csharp> | ||
| using System; | using System; | ||
lang/csharp/net/client_serveur.1547729684.txt.gz · Dernière modification : de root
