UDP to server does not work

170 Views Asked by At

At work, I have to route some data that are coming in using UDP. I've followed some tutorials such as this blog or this tutorial and cooked up two console applications, one to send UDP messages (client) and one to read them (listener). When I enter the IP address 127.0.0.1 (localhost) everything goes well, but no other address works. If I use my real local IP address, I have no luck. Since I locally do not have a static IP, I've also tried installing the listener on a server that does have a static IP. In doing so, I've made sure to add a rule to the Windows Firewall inbound rules set. Still no luck. Any ideas? Here's my code:

/simon/

Client

    class Client
{
    private readonly Socket _sendingSocket;
    private readonly IPEndPoint _receiverEndpoint;
    private static int outboundPortNumber = int.Parse(ConfigurationManager.AppSettings["OutgoingPortNumber"]);

    public Client()
    {
        var receiverAddress = IPAddress.Parse(ConfigurationManager.AppSettings["IpOfReceiver"]);
        _receiverEndpoint = new IPEndPoint(receiverAddress, outboundPortNumber);
        _sendingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        _sendingSocket.EnableBroadcast = true;
    }

    public void Transmit()
    {
        bool done = false;
        while (!done)
        {
            Console.WriteLine("Hit q to quit");
            var keypress = Console.ReadKey();

            if (keypress.KeyChar.ToString().ToLower() == "q")
                done = true;
            else
                SendTextToServer();
        }
    }

    private void SendTextToServer()
    {
        Console.WriteLine("Enter text to send");
        var textToSend = Console.ReadLine();
        byte[] send_buffer = Encoding.ASCII.GetBytes(textToSend);
        try
        {
            _sendingSocket.SendTo(send_buffer, _receiverEndpoint);
        }
        catch (Exception send_exception)
        {
            Console.WriteLine(" Exception {0}", send_exception.Message);
        }
    }
}

Listener

   class Listener
{
    private bool _started;
    private ManualResetEvent _stop;
    private UdpClient _client;
    private IPEndPoint _endPoint;
    private Thread _workingThread;

    private static int inboundPortNumber = int.Parse(ConfigurationManager.AppSettings["IncomingPortNumber"]); 

    public void Start()
    {
        _started = true;
        _stop = new ManualResetEvent(false);
        InitializeUdpClient();
        InitializeWorkingThread();
    }

    private void InitializeUdpClient()
    {
        _endPoint = new IPEndPoint(IPAddress.Any, inboundPortNumber);
        _client = new UdpClient(_endPoint);
    }

    private void InitializeWorkingThread()
    {
        _workingThread = new Thread(WorkerFunction);
        _workingThread.Name = "WorkingThread";
        _workingThread.Start();
    }

    private void WorkerFunction()
    {
        while (_started)
        {
            var res = _client.BeginReceive(iar =>
            {
                if (iar.IsCompleted)
                {
                    byte[] receivedBytes = _client.EndReceive(iar, ref _endPoint);
                    OutputToConsole(receivedBytes);


                }
            }, null);

            if (WaitHandle.WaitAny(new[] { _stop, res.AsyncWaitHandle }) == 0)
            {
                break;
            }
        }
    }

    private void OutputToConsole(byte[] receivedBytes)
    {
        string receivedPacket = Encoding.ASCII.GetString(receivedBytes);
        Console.WriteLine("{0} received at {1}", receivedPacket, DateTime.Now.ToString("hhMMss.ffff"));
    }
}

--Edit --

One of the comments suggested that I should look at some logging. Using Microsoft Network Monitor to capture my data traffic, I've tried the following: (1) On dev machine: When sending to 127.0.0.1 from my dev machine (so from localhost to localhost) no data is logged. I find that to be strange since this is the only scenario in which the messages are received and nicely printed out to the console. (2) When I send from my dev machine to the server, there is some outbound data captured on my dev machine and on the server there is also incoming data. I've run netstat -b on the server, which does not show any UDP protocol at all. Since in method InitializeUdpClient() the UdpClient is with an IPEndpoint so I should be bound to the port. Why then does it not show up when I run netstat? Based on this new info, have a suggestion as to why the listener does not receive any data?

/simon/

1

There are 1 best solutions below

0
On

Sorry guys, the problem was somehow related to the firewall. I had enabled set the nibound rules on the server and outbound rules on my development machine, but a company proxy silently blocked the traffic. When switching over to the wireless network for guests, it my traffic arrived on the server.

Simon