1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| public Socket ServerSocket;
public IPAddress ipAddress;
public MainForm()
{
InitializeComponent();
}
public void Start()
{
// on cherche notre adresse ip
ipAddress = AdresseIpLocale(Dns.GetHostName());
// création de la socket
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
//On lie la socket au point de communication
ServerSocket.Bind(new IPEndPoint(ipAddress, 8000));
//On la positionne en mode "écoute"
ServerSocket.Listen(10);
}
catch (SocketException E)
{
Console.WriteLine(E.Message);
}
}
/// <summary>
/// obtenir l'adresse ip à partir d'un DNS
/// </summary>
/// <param name="DNS"></param>
/// <returns></returns>
public IPAddress AdresseIpLocale(string DNS)
{
IPHostEntry host = Dns.GetHostEntry(DNS);
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
{
return ip;
}
}
return null;
} |
Partager