Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I'm using UdpClient which works fine with a single adapter, but on a machine with hyper-v installed (multiple virtual adapters) the code does not get a response.

Has anyone encountered this and a workaround?

What I have tried:

C#
private string DoUDPSearch(int port)
{
    var host = Dns.GetHostEntry(Dns.GetHostName());
    string ipaddr = "";
    foreach (var ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            try
            {
                var Client = new UdpClient();
                Client.Client.SendTimeout = 2000;
                Client.Client.ReceiveTimeout = 2000;
                var RequestData = Encoding.ASCII.GetBytes("SomeRequestData");
                var ServerEp = new IPEndPoint(ip, 0);

                Client.EnableBroadcast = true;
                Client.Send(RequestData, RequestData.Length, new IPEndPoint(IPAddress.Broadcast, port));

                var ServerResponseData = Client.Receive(ref ServerEp);
                var ServerResponse = Encoding.ASCII.GetString(ServerResponseData);
                //Console.WriteLine("Recieved {0} from {1}", ServerResponse, ServerEp.Address.ToString());
                ipaddr = ServerEp.Address.ToString();
                logger("Server IP = " + ServerEp.Address);
                Client.Close();

                return ipaddr;
            }
            catch { Console.WriteLine("unable to connect."); }
        }
    }
    logger("Server IP not found.");
    return ipaddr;
}
Posted
Updated 10-Dec-16 21:43pm
Comments
Afzaal Ahmad Zeeshan 10-Dec-16 13:09pm    
UDP protocol itself doesn't guarantee any response or delivery. That is the job of TCP. Does TCP work fine in this case as I have not worked in virtualized adapter system?

I worked with "TCP or UDP case" 2 years ago, and left using it in favor of TCP. UDP tends to be fast, so it keeps dropping everything.

Anyways, have a look at this thread.
Richard MacCutchan 11-Dec-16 3:22am    
Are you sure that you have network paths between all the nodes? Did the remote devices receive the broadcast messages?
Mehdi Gholam 11-Dec-16 3:44am    
Found the problem with a lot of tinkering... (wee hee!!), see solution 2

Try to increase your hosts OS UDP Buffer Size:

For Win:

C#
[HKEY_LOCAL_MACHINE \SYSTEM \CurrentControlSet\Services\Afd\Parameters]

DefaultReceiveWindow = 10240
DefaultSendWindow = 10240


Please comment if that didn't helped.
 
Share this answer
 
Comments
Mehdi Gholam 11-Dec-16 0:55am    
Didn't help.
Found the problem, the client should send with port = 0 [ServerEp] and it will receive a response on the correct port of the server (if you set the client port send to be = the server's port [ServerEp] it will always work regardless which is not what you want).

The correct code is below:
C#
private string DoUDPSearch(int port)
{
    var host = Dns.GetHostEntry(Dns.GetHostName());
    string ipaddr = "";
    foreach (var ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            try
            {
                var ServerEp = new IPEndPoint(ip, 0);
                var Client = new UdpClient(ServerEp);
                Client.Client.SendTimeout = 2000;
                Client.Client.ReceiveTimeout = 2000;
                var RequestData = Encoding.ASCII.GetBytes("SomeRequestData");

                Client.Send(RequestData, RequestData.Length, new IPEndPoint(IPAddress.Broadcast, port));

                var ServerResponseData = Client.Receive(ref ServerEp);
                var ServerResponse = Encoding.ASCII.GetString(ServerResponseData);
                ipaddr = ServerEp.Address.ToString();
                logger("Server IP = " + ServerEp.Address);
                Client.Close();

                return ipaddr;
            }
            catch { Console.WriteLine("unable to connect."); }
        }
    }
    logger("Server IP not found.");
    return ipaddr;
}
 
Share this answer
 

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