블로그 이미지
따시쿵

calendar

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

Notice

2015. 2. 2. 09:12 C# with UDP/IP

프로그램 설명


udp 클라이언트/서버 예제입니다.


UdpClient 클래스는 전송 및 동기 블록 모드 비 연결 UDP 데이터 그램 을 수신하기위한 간단한 방법을 제공합니다. UDP 는 비 연결 전송 프로토콜 이기 때문에, 전송 및 데이터를 수신 하기 전에 원격 호스트 연결을 설정할 필요가 없습니다. 


다음의 두 가지 방법 중 하나를기본 원격 호스트를 확립 하는 옵션 을 가질 것 :


1. 원격 호스트 이름 및 매개 변수로 포트 번호를 사용하여 UdpClient 클래스의 인스턴스를 만듭니다.

2. UdpClient 클래스의 인스턴스를 생성 한 다음 연결 메소드를 호출합니다.


Receive method 의 IPEndPoint 을 이용해서 전송하는 호스트의 정보를 알 수 있습니다.


실행 후


메시지 전송 후



프로그램 작성 순서


1. 소켓과 관련한 네임스페이스를 서버/클라이언트 모두에 포함 시킵니다.


using System.Net;
using System.Net.Sockets;


2. 서버 프로그램


            byte[] data = new byte[1024];
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
            UdpClient server = new UdpClient(ipep);

            Console.WriteLine("Waiting for a client....");

            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);

            data = server.Receive(ref sender);

            Console.WriteLine("Message received from {0}", sender.ToString());
            Console.WriteLine("received data : {0}", Encoding.UTF8.GetString(data, 0, data.Length));

            string welcome = "Welcome to my udp server";
            data = Encoding.UTF8.GetBytes(welcome);
            server.Send(data, data.Length, sender);

            while(true)
            {
                data = server.Receive(ref sender);
                Console.WriteLine("received data : {0}", Encoding.UTF8.GetString(data, 0, data.Length));

                server.Send(data, data.Length, sender);
                Console.WriteLine("send data : {0}", Encoding.UTF8.GetString(data, 0, data.Length));
            }

            server.Close();


3. 클라이언트 프로그램


            byte[] data = new byte[1024];
            string input, stringData;
            UdpClient client = new UdpClient("192.168.0.12", 9050);

            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);

            string welcome = "hello, udp server?";
            data = Encoding.UTF8.GetBytes(welcome);
            client.Send(data, data.Length);

            data = client.Receive(ref sender);

            Console.WriteLine("Message received from {0}", sender.ToString());
            stringData = Encoding.UTF8.GetString(data, 0, data.Length);
            Console.WriteLine(stringData);

            while(true)
            {
                Console.Write("send data : ");
                input = Console.ReadLine();
                if (input == "exit")
                    break;

                data = Encoding.UTF8.GetBytes(input);
                client.Send(data, data.Length);
                data = client.Receive(ref sender);
                stringData = Encoding.UTF8.GetString(data);
                Console.WriteLine("received data : {0}", stringData);
            }

            client.Close();
            Console.WriteLine("Stopping clinet");
            


'C# with UDP/IP' 카테고리의 다른 글

[Program C#]Broadcast  (0) 2015.02.25
[Program C#]UDP Multicast  (0) 2015.02.12
[Program C#]UDP 통신 - 기본(Socket) - 콘솔 버전  (1) 2015.02.02
TCP & UDP 비교  (0) 2015.02.02
posted by 따시쿵