C# network communication IPv6 over UDP socket
Petr Faltus development
NetUDP6receiver.cs
GitHub repository: network-socket-communication-source-codes
git clone https://github.com/petrfaltus/network-socket-communication-source-codes.git
Directories to NetUDP6receiver.cs in the repository: csharp/src
using System; using System.Text; using System.Net; using System.Net.Sockets; namespace NetUDP6receiver { public class Program { private static readonly int RECEIVER_PORT = 10000; private static readonly int BUFFER_SIZE = 4096; public static void Main(string[] args) { Console.WriteLine("UDP IPv6 datagram socket receiver"); try { Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp); Console.WriteLine("- socket created"); IPEndPoint receiver = new IPEndPoint(IPAddress.IPv6Any, RECEIVER_PORT); socket.Bind(receiver); Console.WriteLine("- socket bound on port {0}", RECEIVER_PORT); byte[] buffer = new byte[BUFFER_SIZE]; IPEndPoint peer = new IPEndPoint(IPAddress.IPv6Any, 0); EndPoint peer_ref = (EndPoint)peer; bool stop = false; while (stop == false) { int received_length = socket.ReceiveFrom(buffer, ref peer_ref); string msg = Encoding.ASCII.GetString(buffer, 0, received_length); peer = (IPEndPoint)peer_ref; IPEndPoint local = (IPEndPoint)socket.LocalEndPoint; Console.WriteLine("- message {0}B of {1}B received from [{2}]:{3} on port {4}", msg.Length, received_length, peer.Address, peer.Port, local.Port); Console.WriteLine("|{0}|", msg); if (msg.Equals("stop")) { // received message "stop" to stop the receiver stop = true; } } socket.Close(); Console.WriteLine("- socket closed"); } catch (Exception ex) { Console.WriteLine("- {0}", ex.Message); } } } }
NetUDP6sender.cs
Directories to NetUDP6sender.cs in the repository: csharp/src
using System; using System.Text; using System.Net; using System.Net.Sockets; namespace NetUDP6sender { public class Program { private static readonly string RECEIVER_ADDRESS = "::1"; private static readonly int RECEIVER_PORT = 10000; public static void Main(string[] args) { Console.WriteLine("UDP IPv6 datagram socket sender"); try { Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp); Console.WriteLine("- socket created"); string msg; if (args.Length > 0) msg = args[0]; // message is the first parameter, for example "stop" to stop the receiver else { DateTime now = DateTime.Now; msg = string.Format("This is C# message sent at {0} in {1}", now.ToString("d.M.yyyy"), now.ToString("H:mm:ss")); } Console.WriteLine("|{0}|", msg); byte[] buffer = Encoding.ASCII.GetBytes(msg); IPAddress receiver_address = IPAddress.Parse(RECEIVER_ADDRESS); IPEndPoint receiver = new IPEndPoint(receiver_address, RECEIVER_PORT); int sent_length = socket.SendTo(buffer, receiver); IPEndPoint local = (IPEndPoint)socket.LocalEndPoint; Console.WriteLine("- message {0}B of {1}B sent to [{2}]:{3} on port {4}", sent_length, msg.Length, receiver_address, RECEIVER_PORT, local.Port); socket.Close(); Console.WriteLine("- socket closed"); } catch (Exception ex) { Console.WriteLine("- {0}", ex.Message); } } } }
Development tools
Developer ASCII table
Characters and HTML entities in the UTF-8 table
Predefined web CSS colors
CSS px to rem converter
🤝 Your IP address is 2600:1f28:365:80b0:e655:538a:27ae:d312