views:

271

answers:

4

Am starting with socket programming with a simple UDPClient program to send some data. The large code snippet is below:

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

class ShowIP
{
    public static void Main(string[] args)
    {
        string name = Dns.GetHostName();
        //name = "GSL1460";
        name = "GSL1296";
        try
        {
            IPAddress[] addrs = Dns.GetHostEntry(name).AddressList;
            foreach (IPAddress addr in addrs)
                Console.WriteLine("{0}/{1}", name, addr);

            Console.WriteLine("Started listening");
            Thread listenerThread = new Thread(new ThreadStart(StartListeningUDP));
            listenerThread.Start();

            Console.WriteLine("Started sending");
            for (int counter = 0; counter <= 3; counter++)
            {
                Thread.Sleep(1000);
                Console.WriteLine("Sending {0} time", counter.ToString());
                StartSendingUDP(addrs[0]);
            }
            Console.ReadLine();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    private static void StartListeningUDP()
    {
        UdpClient udpListener = null;
        IPEndPoint nwPoint = new IPEndPoint(IPAddress.Any, 12345);

        while (true)
        {
            try
            {
                udpListener = new UdpClient(12345);
                Console.WriteLine("Waiting to receive");
                Byte[] receivedBytes = udpListener.Receive(ref nwPoint);
                string receivedData = Encoding.ASCII.GetString(receivedBytes);

                Console.WriteLine("Data received : " + receivedData);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                udpListener.Close();
            }
        }
    }

    private static void StartSendingUDP(IPAddress clientAddress)
    {
        UdpClient udpSender = new UdpClient();
        try
        {
            Byte[] sendBytes = Encoding.ASCII.GetBytes("Say HI to Papa...");

            Console.WriteLine("Data Sent : Say HI to Papa...");
            udpSender.Send(sendBytes, sendBytes.Length, new IPEndPoint(clientAddress, 12345));
        }
        finally
        {
            udpSender.Close();
        }

    }
}

The sample works fine on local machine, but am not able to send data to another machine on the intranet.

During testing

  • Am uncommenting the appropriate code to send data to his machine
  • Am running the Receiver bit on his machine
  • Have checked that the required port is open on his machine

Am I missing something? Please suggest.

A: 

I'm not a C# person, so I can't comment too much on your code, but it looks basically okay. Make sure that the IP address you're sending to is being resolved correctly to your receiving machine.

Also, see if Windows has firewalled your internet connection, and try disabling the firewall if so. And, I know that Microsoft has some ideas about "safe" code that have caused us some problems in the past. I don't have any specifics, but there might be settings in the project that keep it from being able to access the network.

mbyrne215
+1  A: 

udpSender.Flush?

rob
A: 

The UDP-Listener might be listening on localhost only. You could try to replace

udpListener = new UdpClient(12345)

in StartListeningUDP() with

udpListener = new UdpClient(new IPEndPoint(IPAddress.Any,12345))
Stephan Keller
Tried that, didnt help! But thanks...
Codex
A: 

Hi you can't really send UDP over the internet without doing few things before. you will get too many udp filters on the way. even if you will disable your firewall, your router/provider modem can be set to block it. else - your provider servers will block it. so in fact you will have to make sure that this port is open for UDP, just as on your localhost it won't work unless you will open this port in the firewall and/or install the loopback adapter

Nir