본문 바로가기
컴퓨터/C#

C# UDP 프로그램

by Begi 2019. 7. 16.
반응형

UDP 프로그래밍은 TCP에 비해 매우 간단하다. UDP는 서버 클래스가 없고 UdpClient 클래스를 서버에도 사용한다.

 

사용되는 using 문

using System.Net;

 

using System.Net.Sockets;

 

UDP 수신

UDP를 수신하는 방법은 다음과 같다.

 UdpClient listener = new UdpClient(80);
 IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 80);
 byte[] receive_byte_array;


 if (listener.Available > 0)
 {
    receive_byte_array = listener.Receive(ref groupEP);
 }

listener.Receive 함수는 데이터를 수신할 때까지 Blocking 되는 함수이다. listener.Available를 사용하지 않으면 listener.Receive에서 Blocking 될 수 있다.

 

UDP 송신

UDP를 송신하는 방법은 다음과 같다.

 Byte[] sendBytes // 전송할 데이터


 UdpClient udpClientB = new UdpClient();
 udpClientB.Send(sendBytes, sendBytes.Length, "192.168.0.1", 80);

 

반응형

'컴퓨터 > C#' 카테고리의 다른 글

C# CPU 사용량, 메모리 용량  (0) 2019.10.29
C# Mutex 사용법  (0) 2019.08.05
[C#] 자원 관리 - Managed와 Unmanaged 차이  (0) 2019.07.13
C# 다른 프로그램 실행하기  (0) 2019.07.04
C# using 사용하는 이유  (0) 2019.07.04

댓글