C# network communication over UDP socket

Petr Faltus development

Example source code in Microsoft .NET csharp how to communicate in IPv4 over network UDP socket. The sender and the receiver.

NetUDPreceiver.cs

GitHub repository: network-socket-communication-source-codes

git clone https://github.com/petrfaltus/network-socket-communication-source-codes.git

Directories to NetUDPreceiver.cs in the repository: csharp/src

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace NetUDPreceiver
{
    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 IPv4 datagram socket receiver");

            try
            {
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                Console.WriteLine("- socket created");

                IPEndPoint receiver = new IPEndPoint(IPAddress.Any, 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.Any, 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);
            }
        }
    }
}

NetUDPsender.cs

Directories to NetUDPsender.cs in the repository: csharp/src

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace NetUDPsender
{
    public class Program
    {
        private static readonly string RECEIVER_ADDRESS = "127.0.0.1";
        private static readonly int RECEIVER_PORT = 10000;

        public static void Main(string[] args)
        {
            Console.WriteLine("UDP IPv4 datagram socket sender");

            try
            {
                Socket socket = new Socket(AddressFamily.InterNetwork, 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 3.149.23.112 (ec2-3-149-23-112.us-east-2.compute.amazonaws.com)