-
Notifications
You must be signed in to change notification settings - Fork 0
/
UdpClient.cs
27 lines (21 loc) · 893 Bytes
/
UdpClient.cs
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
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace UdpFun;
public static class UdpClient
{
public static void SendAndReceive()
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress serverAddr = IPAddress.Parse("127.0.0.1");
IPEndPoint endPoint = new IPEndPoint(serverAddr, 10020);
var receiveBuffer = new byte[1000];
string text = "Hello World";
byte[] send_buffer = Encoding.ASCII.GetBytes(text);
sock.SendTo(send_buffer, endPoint);
var flags = SocketFlags.None;
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
var size = sock.ReceiveMessageFrom(receiveBuffer, ref flags, ref remoteEP, out var packetInformation);
Console.WriteLine($"CLIENT RECEIVED: {Encoding.ASCII.GetString(receiveBuffer[..size])}");
}
}