Programmering IV December

From Teknologisk videncenter
Revision as of 10:23, 6 December 2016 by Makn (talk | contribs)
Jump to: navigation, search

Server:


       //Listens for data.
       static void Main(string[] args)
       {
           UdpClient client = new UdpClient(11000);
           IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 11000);
           byte[] recivedBytes = client.Receive(ref endpoint);
           string result = Encoding.ASCII.GetString(recivedBytes);
           Console.WriteLine(result);


           Console.ReadLine();
       }




Client:

       //Sends 100 packages
       static void Main(string[] args)
       {
           UdpClient client = new UdpClient();
           IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
               byte[] messageBytes = Encoding.ASCII.GetBytes("Hello!");
               client.Send(messageBytes, messageBytes.Length, endpoint);
       }


Server/Client program

       static void Main(string[] args)
       {
           Thread thread = new Thread(Listener);
           thread.Start();
           IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11005);
           UdpClient client = new UdpClient();
           while (true)
           {
               Console.WriteLine("Write something:");
               string input = Console.ReadLine();
               byte[] messageBytes = Encoding.ASCII.GetBytes(input);
               client.Send(messageBytes, messageBytes.Length, endPoint);
           }
           
       }
       static void Listener()
       {
           IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 11010);
           UdpClient client = new UdpClient(endpoint);
           while (true)
           {
               byte[] recivedBytes = client.Receive(ref endpoint);
               string result = Encoding.ASCII.GetString(recivedBytes);
               Console.WriteLine(result);
           }
       }







       static void Main(string[] args)
       {
           IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11010);
           UdpClient client = new UdpClient();
           Thread thread = new Thread(Listener);
           thread.Start();
           while (true)
           {
               string input = Console.ReadLine();
               byte[] messageBytes = Encoding.ASCII.GetBytes(input);
               client.Send(messageBytes, messageBytes.Length, endpoint);
           }
       }
       static void Listener()
       {
           IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 11005);
           UdpClient client = new UdpClient(endpoint);
           
           while (true)
           {
               byte[] recivedBytes = client.Receive(ref endpoint);
               string result = Encoding.ASCII.GetString(recivedBytes);
               Console.WriteLine(result);
           }
       }