using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System.IO; using System.Threading; using System.Net.NetworkInformation; namespace go01 { public abstract class socketPlateauBase { #region enum public enum TypeSocket_E { NONE, Serveur, Client, LAST } public enum commande_E { Join, Leave, Place, Win, WhoTurn, LAST } // ConfigGo_S ; null ; Point ; Goban.PlayerTuen(string) ; Goban.Joueur_E(Tour) #endregion #region struct [Serializable] public struct CommandeSocket_S { public commande_E commande; public object data; public CommandeSocket_S(commande_E cmd, object dt) { commande = cmd; data = dt; } } #endregion #region variable protected Socket socServ, socCli; protected Socket SocCli { get { if (socFlag == TypeSocket_E.Serveur && socCli != null || socFlag == TypeSocket_E.Client) { return socCli; } else { return null; } } set { socCli = value; } } protected TypeSocket_E socFlag = 0; public TypeSocket_E SocFlag { get => socFlag; } protected IPAddress adresseIpCourante; protected int port; protected static Boolean connexionEtablie = false; protected Goban plateauDeJeu; protected Goban.ConfigGo_S cfgGo; #endregion public delegate void OnReceived(object myObject, socketPlateauServReception myArgs); public event OnReceived onReceived; public socketPlateauBase(TypeSocket_E socF, Goban.ConfigGo_S cfgGo, Goban g) { plateauDeJeu = g; adresseIpCourante = cfgGo.IpServeur; socFlag = socF; this.port = cfgGo.port; this.cfgGo = cfgGo; } #region commun protected void InitialiserReceptionAsync(Socket sArg) { var netStream = new NetworkStream(sArg, true); var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); object cmd = (object)binaryFormatter.Deserialize(netStream); try { onReceived?.Invoke(this, new socketPlateauServReception(cmd)); } catch (Exception e) { Console.WriteLine(e); } InitialiserReceptionAsync(sArg); } public void senCmd(object cmd) { var netStream = new NetworkStream(socCli, true); var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); binaryFormatter.Serialize(netStream, cmd); } #endregion #region outil public static void net_adapters(ComboBox cb) { List values = new List(); foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { cb.Items.Add(nic.Name); } } public static IPAddress Verifier(string sAdresse) { IPAddress rep = null; if (sAdresse.Trim().Length > 0) { IPAddress[] ipVerifs = Dns.GetHostEntry(sAdresse).AddressList; for (int i = 0; i < ipVerifs.Length; i++) { //rep += ipVerifs[0].ToString(); if (ipVerifs[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { if (ipVerifs[i] != new IPAddress(0X0100007F)) { Ping pVerif = new Ping(); PingReply pRepon = pVerif.Send(ipVerifs[i]); if (pRepon.Status == IPStatus.Success) { rep = ipVerifs[i]; Console.WriteLine(ipVerifs[i] + "Ping réussi"); break; } else { Console.WriteLine(ipVerifs[i] + "Ping KO"); } } } } } else { Console.WriteLine("Renseigner une adresse"); } return rep; } #endregion } }