SHARE

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 only handles 1 connection and closes after the client disconnects/stops sending information.

I was cut off at the end so I had to sum up Port Forwarding really quickly so please forgive my rushed-ness. Also I think the quality may be a smidge iffey but it should be good enough for now.

If you have any requests feel free to comment/send me a message!

c#
source

22 COMMENTS

  1. @twister1986 you need ton implements a client too, if you don't receive any connection, you application stop at this line until a client send a connection request on the UDP socket

  2. 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.

  3. 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. 

  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. 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

  6. @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.

  7. 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);
    newSocket.Bind(endpoint);
    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)
    {
    newSocket.Send(data);
    }
    while(true)
    {
    if (!newSocket.Connected)
    {
    Console.WriteLine("Client disconnected");
    break;
    }
    data = new byte[1024];
    recv = newSocket.ReceiveFrom(data, ref tmpRemote);
    if (recv == 0)
    break;
    Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

    }
    }
    }
    }

LEAVE A REPLY

Please enter your comment!
Please enter your name here