morpionReseau/go01/CreerPartieReseau_serv.cs

78 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace go01
{
public partial class CreerPartieReseau_serv : Form
{
socketPlateau serveur;
public CreerPartieReseau_serv()
{
InitializeComponent();
this.tbUid.Text = Guid.NewGuid().ToString();
this.tbUid.Select(0, 0);
socketPlateau.net_adapters(cbNic);
//serveur = new socketPlateau()
}
private void btnCreatePart_Click(object sender, EventArgs e)
{
btnCreatePart.Enabled = false;
tbPartName.Enabled = false;
tbNomJoeur.Enabled = false;
var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromSeconds(5);
socketPlateau.ConfigGo_S config = new socketPlateau.ConfigGo_S(tbUid.Text, tbPartName.Text, "", tbNomJoeur.Text, int.Parse(tbTaille.Text), int.Parse(tbPionaAligner.Text), IPAddress.Parse(tbIp.Text), int.Parse(tbPort.Text));
serveur = new socketPlateau(socketPlateau.TypeSocket_E.Serveur, config, null);
serveur.AttenteJoueur();
serveur.onJoinServ += new socketPlateau.OnJoinServ(JoueurRejoinServeur);
}
private void JoueurRejoinServeur(object o, socketPlateauServArgs e)
{
Console.WriteLine($"{e.ConfigGo.NomJoeurClient} vs {e.ConfigGo.NomJoeurServeur}");
}
private void broadcast()
{
var Client = new UdpClient();
var RequestData = Encoding.ASCII.GetBytes($"{tbUid.Text};{tbPartName.Text};{tbNomJoeur.Text};{tbTaille.Text};{tbPionaAligner.Text}");
Client.EnableBroadcast = true;
Client.Send(RequestData, RequestData.Length, new IPEndPoint(IPAddress.Broadcast, 8888));
Client.Close();
}
private void cbNic_SelectedIndexChanged(object sender, EventArgs e)
{
var local = NetworkInterface.GetAllNetworkInterfaces().Where(i => i.Name == cbNic.SelectedItem.ToString()).FirstOrDefault();
foreach (UnicastIPAddressInformation ip in local.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
tbIp.Text = ip.Address.ToString();
}
}
}
}
}