new UdpClient(5555) Receive : Udp « Network « C# / CSharp Tutorial






using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

class MainClass {
    private static void Main() {
        IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
        Thread receiveThread = new Thread(ReceiveData);
        receiveThread.IsBackground = true;
        receiveThread.Start();

        UdpClient client = new UdpClient();
        try {
            string text = "message";
            byte[] data = Encoding.UTF8.GetBytes(text);
            client.Send(data, data.Length, remoteEndPoint);
        } catch (Exception err) {
            Console.WriteLine(err.ToString());
        } finally {
            client.Close();
        }
    }

    private static void ReceiveData() {
        UdpClient client = new UdpClient(5555);
        while (true) {
            try {
                IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
                byte[] data = client.Receive(ref anyIP);
                string text = Encoding.UTF8.GetString(data);
                Console.WriteLine(">> " + text);
            } catch (Exception err) {
                Console.WriteLine(err.ToString());
            }
        }
    }
}








33.10.Udp
33.10.1.UDP multi-cast
33.10.2.Udp time out option
33.10.3.new UdpClient(5555) Receive