C# UDP messages over internet ip

32 Views Asked by At

I have the following code on the server side to listen incoming messages via udp:

class Program
{
    static void Main()
    {
        UdpClient udpServer = new UdpClient(27016);

        while (true)
        {
            IPEndPoint clientEndpoint = new IPEndPoint(IPAddress.Any, 0);
            byte[] data = udpServer.Receive(ref clientEndpoint);

            string clientMessage = Encoding.ASCII.GetString(data);
            Console.WriteLine("Received: " + clientMessage);
        }
    }
}

And the following code for the client:

class Vlient
{
    static void Main()
    {
        UdpClient udpClient = new UdpClient();
        udpClient.Connect("LOCAL IP", 27016);

        while (true)
        {
            Console.Write("Enter a message: ");
            string message = Console.ReadLine();

            byte[] data = Encoding.ASCII.GetBytes(message);
            udpClient.Send(data, data.Length);
        }
    }
}

The code works perfectly with my local ip address but as soon as i change it to external ip with same port, i am not receiving any msgs. I have enabled this udp port in my router and mapped it with my local ip. To test if it works, i created a counter strike sever on this udp port 27016 and tested it with other machine using internet ip as well. It works, i joined the server. What am i missing in my code that its not working with message?

I enabled the port in my router, done settings in my firewall with inbound rules accepting udp and tcp connections on port 27016. Tested with a game and it works. But its not working when i use this port in my code

0

There are 0 best solutions below