티스토리 뷰
반응형
[c#/wpf/winform] UDPHelper구현(server/client)
#정의부
#싱글톤으로 사용하면된다.
public class UdpHelper
{
public string server_host = "127.0.0.1";
public string server_broadcast = "255.255.255.255";
public int receiver_port = 3333;
public int sender_port = 4333;
private UdpClient sender;
private UdpClient receiver;
private static UdpHelper _instance { get; set; }
public static UdpHelper Instance
{
get
{
return _instance ?? (_instance = new UdpHelper());
}
}
public void Setting(string server_host, int receiver_port, int sender_port)
{
this.server_host = server_host;
this.receiver_port = receiver_port;
this.sender_port = sender_port;
Receive();
}
public void Setting()
{
Receive();
}
public delegate void PacketReceiveEventHandler(string code);
public void Receive(object rECEIVER_PORT)
{
throw new NotImplementedException();
}
public event PacketReceiveEventHandler PacketReceiveEventHandlerEvent;
UdpHelper()
{
}
public void Send(string msg)
{
Send(msg, false);
}
public void Send(string msg, int port)
{
Send(msg, false, port);
}
public void Send(string msg, bool isBroadCast)
{
Send(msg, isBroadCast, sender_port);
}
public async void Send(string msg, bool isBroadCast, int port)
{
try
{
sender = new UdpClient(isBroadCast ? server_broadcast : server_host, port);
byte[] msgToByte = DataConverter.StringToByte(msg);
_ = await sender.SendAsync(msgToByte, msgToByte.Length);
sender.Close();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
public async void SendWithIP(string msg, string ip, int port)
{
try
{
sender = new UdpClient(ip, port);
byte[] msgToByte = DataConverter.StringToByte(msg);
_ = await sender.SendAsync(msgToByte, msgToByte.Length);
sender.Close();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
public void SendByte(byte[] msg)
{
SendByte(msg, sender_port);
}
public void SendObject(object msg)
{
SendObject(msg, sender_port);
}
public void SendObject(object msg, int port)
{
var bytes = ByteConverter.ObjectToByte(msg);
SendByte(bytes, false, port);
}
public void SendByte(byte[] msg, int port)
{
SendByte(msg,false, port);
}
public async void SendByte(byte[] msg, bool isBroadCast, int port)
{
try
{
sender = new UdpClient(isBroadCast ? server_broadcast : server_host, port);
byte[] msgToByte = msg;
_ = await sender.SendAsync(msgToByte, msgToByte.Length);
sender.Close();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
private void Receive()
{
Receive(receiver_port);
}
public async void Receive(int Port)
{
try
{
IPEndPoint epRemote = new IPEndPoint(IPAddress.Any, Port);
receiver = new UdpClient(epRemote);
var receive = await receiver.ReceiveAsync();
//자신이 보낸 패킷은 무시
if (!NetworkHelper.IsLocalHost(receive.RemoteEndPoint))
{
//var msg = ByteConverter.ByteToObject(receive.Buffer);
//Debug.WriteLine(msg);
var msg = ByteConverter.ByteToString(receive.Buffer);
PacketReceiveEventHandlerEvent(msg);
}
receiver.Close();
Receive();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
}
#UdperHelper #Udp #c# #wpf #winform
반응형
'c#' 카테고리의 다른 글
[c#/wpf/unity] 나의 아이피 주소 가져오기 헬퍼구현 (0) | 2022.03.02 |
---|---|
[c#/wpf/winform/unity] Byte to String / String to Byte 로 변환 컨버팅 (0) | 2022.03.02 |
[wpf/c#] mvvm 을 활용한 런처(전원 관리 및 파일 관리) 만들기(1/3) (0) | 2022.03.02 |
[c#/wpf] UpdateSourceTrigger=PropertyChanged (mvvm 사용 시 바인딩 객체값이 변경되면 바로 반영하기) (0) | 2022.02.24 |
[wpf/c#] mvvm light, CommandParameter 사용법 (0) | 2022.02.24 |
댓글
반응형