-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUdpServer.cs
More file actions
23 lines (18 loc) · 739 Bytes
/
Copy pathUdpServer.cs
File metadata and controls
23 lines (18 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace UdpFun;
public static class UdpServer
{
public static void ListenForIncoming()
{
using Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(IPEndPoint.Parse("127.0.0.1:10020"));
var bytes = new byte[1000];
var flags = SocketFlags.None;
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
var size = socket.ReceiveMessageFrom(bytes, ref flags, ref remoteEP, out var packetInformation);
Console.WriteLine($"SERVER RECEIVED: {Encoding.ASCII.GetString(bytes[..size])}");
socket.SendTo(bytes, 0, size, SocketFlags.None, remoteEP);
}
}