Here's a simple example in C# of how to send and receive data from the same UDP socket:
using System; using System.Net; using System.Net.Sockets; using System.Text; public class UdpExample { public static void Main() { UdpClient client = new UdpClient(12345); IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 0); while (true) { string messageToSend = "Hello, World!"; byte[] bytesToSend = Encoding.ASCII.GetBytes(messageToSend); client.Send(bytesToSend, bytesToSend.Length, "localhost", 12345); byte[] bytesReceived = client.Receive(ref endpoint); string messageReceived = Encoding.ASCII.GetString(bytesReceived); Console.WriteLine($"Received: {messageReceived}"); } } }
In this example, we create a UdpClient
object that listens on port 12345
for incoming messages. We also create an IPEndPoint
object to hold the endpoint of the remote host that we'll be sending messages to.
We then enter a loop where we send a message to localhost
on port 12345
and wait for a response. We construct a message to send by converting a string to an array of bytes using Encoding.ASCII.GetBytes
. We send the message using the Send
method of the UdpClient
object.
We then receive a response using the Receive
method of the UdpClient
object. The ref endpoint
parameter is used to get the endpoint of the remote host that sent the message. We convert the received bytes to a string using Encoding.ASCII.GetString
, and then print the received message to the console.
This loop will continue indefinitely, sending and receiving messages over the same UDP socket. You can modify the message to send and the endpoint to send it to as needed for your own use case.
"C# UDP socket programming example"
using System; using System.Net; using System.Net.Sockets; using System.Text; class UdpExample { static void Main() { UdpClient udpClient = new UdpClient(); // Send data byte[] sendData = Encoding.ASCII.GetBytes("Hello, UDP!"); udpClient.Send(sendData, sendData.Length, "127.0.0.1", 12345); // Receive data IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] receiveData = udpClient.Receive(ref remoteEndPoint); string receivedMessage = Encoding.ASCII.GetString(receiveData); Console.WriteLine("Received: " + receivedMessage); udpClient.Close(); } }
"C# UDP socket bind to specific port"
using System; using System.Net; using System.Net.Sockets; using System.Text; class UdpExample { static void Main() { UdpClient udpClient = new UdpClient(); // Bind to specific port udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, 12345)); // Send and receive data (similar to the previous example) // ... udpClient.Close(); } }
"C# UDP socket asynchronous send/receive example"
using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; class UdpExample { static async Task Main() { UdpClient udpClient = new UdpClient(); // Asynchronous send and receive await SendDataAsync(udpClient); await ReceiveDataAsync(udpClient); udpClient.Close(); } static async Task SendDataAsync(UdpClient udpClient) { byte[] sendData = Encoding.ASCII.GetBytes("Hello, UDP!"); await udpClient.SendAsync(sendData, sendData.Length, "127.0.0.1", 12345); } static async Task ReceiveDataAsync(UdpClient udpClient) { UdpReceiveResult result = await udpClient.ReceiveAsync(); string receivedMessage = Encoding.ASCII.GetString(result.Buffer); Console.WriteLine("Received: " + receivedMessage); } }
"C# UDP socket broadcast example"
using System; using System.Net; using System.Net.Sockets; using System.Text; class UdpExample { static void Main() { UdpClient udpClient = new UdpClient(); // Enable broadcasting udpClient.EnableBroadcast = true; // Broadcast data byte[] sendData = Encoding.ASCII.GetBytes("Broadcast message!"); udpClient.Send(sendData, sendData.Length, new IPEndPoint(IPAddress.Broadcast, 12345)); udpClient.Close(); } }
"C# UDP socket timeout example"
using System; using System.Net; using System.Net.Sockets; using System.Text; class UdpExample { static void Main() { UdpClient udpClient = new UdpClient(); // Set a receive timeout (e.g., 5 seconds) udpClient.Client.ReceiveTimeout = 5000; try { // Attempt to receive data within the timeout byte[] receiveData = udpClient.Receive(ref new IPEndPoint(IPAddress.Any, 0)); string receivedMessage = Encoding.ASCII.GetString(receiveData); Console.WriteLine("Received: " + receivedMessage); } catch (SocketException ex) { Console.WriteLine("Receive timeout: " + ex.Message); } udpClient.Close(); } }
"C# UDP socket exception handling"
using System; using System.Net; using System.Net.Sockets; using System.Text; class UdpExample { static void Main() { UdpClient udpClient = new UdpClient(); try { // Attempt to send data byte[] sendData = Encoding.ASCII.GetBytes("Hello, UDP!"); udpClient.Send(sendData, sendData.Length, "127.0.0.1", 12345); // Attempt to receive data byte[] receiveData = udpClient.Receive(ref new IPEndPoint(IPAddress.Any, 0)); string receivedMessage = Encoding.ASCII.GetString(receiveData); Console.WriteLine("Received: " + receivedMessage); } catch (SocketException ex) { Console.WriteLine("Socket exception: " + ex.Message); } finally { udpClient.Close(); } } }
"C# UDP socket datagram size limit"
using System; using System.Net; using System.Net.Sockets; using System.Text; class UdpExample { static void Main() { UdpClient udpClient = new UdpClient(); // Set a custom buffer size (e.g., 4096 bytes) udpClient.Client.ReceiveBufferSize = 4096; // Send and receive data (similar to previous examples) // ... udpClient.Close(); } }
"C# UDP socket multicast example"
using System; using System.Net; using System.Net.Sockets; using System.Text; class UdpExample { static void Main() { UdpClient udpClient = new UdpClient(); // Join a multicast group IPAddress multicastAddress = IPAddress.Parse("239.0.0.1"); udpClient.JoinMulticastGroup(multicastAddress); // Send and receive multicast data byte[] sendData = Encoding.ASCII.GetBytes("Multicast message!"); udpClient.Send(sendData, sendData.Length, multicastAddress.ToString(), 12345); IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] receiveData = udpClient.Receive(ref remoteEndPoint); string receivedMessage = Encoding.ASCII.GetString(receiveData); Console.WriteLine("Received multicast: " + receivedMessage); udpClient.Close(); } }
"C# UDP socket IPv6 support example"
using System; using System.Net; using System.Net.Sockets; using System.Text; class UdpExample { static void Main() { UdpClient udpClient = new UdpClient(AddressFamily.InterNetworkV6); // Use IPv6 address for sending and receiving data IPAddress ipv6Address = IPAddress.Parse("2001:0db8::1"); udpClient.Connect(ipv6Address, 12345); // Send and receive data (similar to previous examples) // ... udpClient.Close(); } }
pool lifecycleexception onload keyerror letters userid average network-printers .net-standard-2.0 partial