Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have developed C# application to send and receive UDP broadcast message. Its working perfectly in windows. But in my Raspberry Pi getting error "Network is unreachable" This will work only when I assign my correct gateway in the network settings.

How I can make this work without assigning the gateway.


My Code

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace ConsoleApplication3
{
    public static class Data
    {
        public const int RCV_TIMEOUT = 1000;
        public const int MaxRetry = 2;      
        public const string BroadCastIP = "255.255.255.255";
        public const int UDPPort = 8080;
       
    }

    class Program
    {
        enum Response
        {
            Error, NotFound, Found
        }
     
        static void Main(string[] args)
        {
            while (true)
            {
                
				Thread.Sleep(1000);
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                socket.EnableBroadcast = true;
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, Data.RCV_TIMEOUT);
                IPEndPoint ipendpoint = new IPEndPoint(System.Net.IPAddress.Parse(Data.BroadCastIP), Data.UDPPort);

                byte[] message = Encoding.ASCII.GetBytes("#Where are you");
                SendSearchMessage(socket, message, ipendpoint);
                socket.Close();

          
        	}
        }


        private static void SendSearchMessage(Socket socket, byte[] message, EndPoint rmtdevice)
        {
          
            DateTime dtStart = DateTime.Now;


            try
            {
                socket.EnableBroadcast = true;
                socket.SendTo(message, message.Length, SocketFlags.None, rmtdevice);
                Console.WriteLine("Send.");

               
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); }
            

        }
       
    }
}
Posted
Comments
Jochen Arndt 27-Apr-15 7:36am    
This is a problem of your Pi's network configuration and/or your local network topology. So you should have posted that information instead of the code snippet.

But why you don't want to setup the gateway address?
It is needed to connect to the internet (e.g. for installing updates).

However, the problem may be solved by not broadcasting to the whole world (which does not happen) but using the broadcast address of your local subnet (e.g. 192.168.1.255).
Arun Kumar K S 27-Apr-15 8:33am    
Actually I am using this code for discovering the Raspberry Pi device from an unknown network. We cant set a gateway before findig the Raspberry Pi. After findig the IP we Change the IP of the Raspberry pi to current network then we will communicate to the Raspberry Pi using TCP connection. But the raspbeery pi not sending back my search request.

I can send broadcast message in any network configuration from windows. But my raspberry not working like that.
Jochen Arndt 27-Apr-15 8:59am    
Let me try to understand:
You have a Pi that is configured with a static IP but is now connected to a different network and you want the Pi to change its network configuration to use the new one.

You wrote "But the raspbeery pi not sending back my search request."
How should he do that? You did not show any code to perform that. While the Pi has a different network setup, he can't send packets to other networks without a gateway (the packets are dropped because there is no routing information).

My suggestions (requiring some work on the Pi that is started upon boot):
- Check if network of current config is up
- If so all is OK
- If not, change network to use DHCP and try to get an IP
- If that fails start a broadcast listener as daemon
- Upon receiption of the broadcast message, configure the network according to the provided data

But I'm not sure if the last option can be realised (to check this the Pi must receive the broadcast message).
Arun Kumar K S 28-Apr-15 3:46am    
Thank you very much for your help. I solved my issue by setting a default gateway "0.0.0.0". Your explanations helped me to identify and solve this issue.
Jochen Arndt 28-Apr-15 4:28am    
Fine to hear that you solved your problem and thank you for your feedback.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900