Warning: Use of undefined constant REQUEST_URI - assumed 'REQUEST_URI' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\mbc1\wp-content\themes\jannah4\functions.php on line 73
How To Create a UDP Server in C# – jj

How To Create a UDP Server in C#

This has been requested by multiple people so I decided to make a quick UDP Server tutorial. It is not the best server and I may re-do this one in the future – it …



Related Articles


  1. here the code but it is a little bir different

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net.Sockets;
    using System.Net;
    namespace ClientUdpV1
    class Program
    static void Main(string[] args)
    int recv;
    byte[] data = new byte[1024];
    IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 100);

    Socket newSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    Console.WriteLine("Baglanti bekleniyor…");
    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 100);
    EndPoint tmpRemote = (EndPoint)sender;

    recv = newSocket.ReceiveFrom(data, ref tmpRemote);
    Console.WriteLine("{0} adresinden mesaj geldi", tmpRemote.ToString());
    Console.WriteLine(Encoding.ASCII.GetString(data,0, recv));

    string welcome = "Sunucuma hos geldin";
    data = Encoding.ASCII.GetBytes(welcome);

    if (!newSocket.Connected)
    Console.WriteLine("Client disconnected");
    data = new byte[1024];
    recv = newSocket.ReceiveFrom(data, ref tmpRemote);
    if (recv == 0)
    Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));


  2. @MariusKanaporis is right… UDP is connection-less protocol meaning you cannot check a socket to see if it's connected. In order to handle disconnects, the simplest way is to implement a timeout handler. If the server doesn't receive packets from x client within x time, then you can treat that as disconnections.

  3. You should be using a port # that is over 1023, because ports of 0 – 1023 are reserved for well known services if you use a port # less than 1023 you drastically increase the possibility of a collision, think of it this way the higher the port # you use the less likely of a chance you have of that port # already being used by a service or application you don't know about

  4. Some advice, although it doesn't matter lots of people would criticise you're usage of the break statement. Try using a Boolean value that, while true, keeps the loop on and then throw it to false to break the loop. This Makes you're code allot easier to work through for other people.

  5. Hello, I have the same problem like @Amir Mattar , how I can solv it? how can I go out from the loop or what it is. I put this code in my program and if I not send all the time something the program is blocked. 

  6. I've noticed a lot of flaws in this. The IPEndPoint does NOT listen for connections. That's what the socket does. Binding the socket does NOT bind incoming connections, it only binds the socket to the network address so it can start accepting connections. There's also a lot of misleading stuff in your code.

Leave a Reply

Check Also

Back to top button