Cannot receive data from UDP socket.. WHY??

networking United States
  • 15 years ago
    Dear all,

    I am working on receiving data from a DHCP server (linux machine) from a C# client program on windows. The server is connected to a hardware which sends out data on commands. It seems that I am able to send commands to the server and manage to get the hardware in capture mode but I cant receive any data. The progam just keeps polling.

    This is the code:

    class SimpleUdpClient
    {    
    static byte[] msg = new byte[2000];

       static IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
       static Socket fd = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
       static IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
       static EndPoint Remote = (EndPoint)(sender);

       static void send_msg()                    
       {
           if (fd == null)
           {
               Console.WriteLine("Failure to connect socket!");
           }
           fd.SendTo(msg,len, SocketFlags.None, ipep);
       }

       static void recieve_msg()
       {    fd.Connect(ipep);

           if (!(fd.Poll(1000000, SelectMode.SelectRead)))
           {
               Console.WriteLine("Timeout on answer!\n");
               len = 0;
               return;
           }    
           else{
               msg = new byte[2000];
               len = fd.ReceiveFrom(msg, ref Remote);
               }
       }

       unsafe static void Main()
       {    capture_off(using send function above to send commands for the hardware to send );    

    Is the code correct? Do I need to do any preformatting of the data array before I send out? network byte order? Totally nuts about what is the problem source?  Pls advise. Many many thanks!!

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski