-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
37 lines (30 loc) · 997 Bytes
/
Program.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
28
29
30
31
32
33
34
35
36
37
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace UDPServer
{
class Program
{
static void Main()
{
IPEndPoint endPoint = new(IPAddress.Parse("127.0.0.1"), 1115);
using Socket socket = new(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(endPoint);
byte[] buffer = new byte[512];
EndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
try
{
int bytesReceived = socket.ReceiveFrom(buffer, ref clientEndPoint);
string data = Encoding.ASCII.GetString(buffer, 0, bytesReceived);
Console.WriteLine("Received: {0}", data);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: {0}", ex.Message);
}
}
}
}
}