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
|
int choix;
TcpListener listenner;
IPHostEntry ipEntry = Dns.GetHostEntry("localhost");
TcpClient client;
Console.Write("Port = ");
try
{
int port = int.Parse(Console.ReadLine());
do
{
Console.WriteLine("Choose a IP address");
foreach (IPAddress ip in ipEntry.AddressList)
{
Console.WriteLine(ip);
}
choix = int.Parse(Console.ReadLine());
}
while ( choix < 0 || choix > ipEntry.AddressList.Length);
Console.WriteLine(choix.ToString());
listenner = new TcpListener(ipEntry.AddressList[choix-1],port);
listenner.Start();
Console.WriteLine("Listening Established....");
client = listenner.AcceptTcpClient();
NetworkStream ns = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize];
int numbyte = ns.Read(buffer, 0, Convert.ToInt32(client.ReceiveBufferSize));
Console.WriteLine("Received " + Encoding.ASCII.GetString(buffer, 0, numbyte));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("Exit");
}
finally
{
Console.ReadKey();
} |
Partager