Click here to Skip to main content
15,888,052 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all, this is a follow on from a question I posted last week - the following code discovers a media server on my local lan , it works but is very slow and I would like some advice as to how I can either speed it up or do it differently

C#
void ScanForServer()
        {
            IPEndPoint ReceiveIP = null;
            UdpClient listener = null;
            Byte[] RequestData = null;
            byte[] BytesReceived = null;

            int UDPPort = 3483;

            try
            {
                ReceiveIP = new IPEndPoint(IPAddress.Any, UDPPort);
                listener = new UdpClient(this.UDPPort);
         
                // RequestData will contain a server request in the correct format
                // ReceivIP will contain the ip address of a discovered server.
                RequestData = listener.Receive(ref ReceiveIP);
                
                // Which I then send back as a request
                listener.Send(RequestData, RequestData.Length, ReceiveIP);
                // This will return server information such as the tcp port.
                BytesReceived = listener.Receive(ref ReceiveIP);
             }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (listener != null && listener.Client.Connected)
                    listener.Close();
            }
        }


as I say it works perfectly but is VERY SLOW ( can be 30 seconds or so )
Posted
Updated 21-Jan-16 4:14am
v3
Comments
Dave Kreskowiak 21-Jan-16 11:02am    
It appears as though your code is just waiting for a response from the media servers. There is nothing you can do to speed that up.
pkfox 21-Jan-16 12:12pm    
Hi Dave, the strange thing is I have very similar code written in Java which runs on my tablet and the response is almost immediate - I'm beginning to suspect my laptops wi-fi card

1 solution

As Dave says: your code is waiting for responses, and that can't be speed up.
However, it might be worth using the Stopwatch class to work out exactly where it is going slowly:
C#
void ScanForServer()
    {
    IPEndPoint ReceiveIP = null;
    UdpClient listener = null;
    Byte[] RequestData = null;
    byte[] BytesReceived = null;
    int UDPPort = 3483;
    Stopwatch s1 = new Stopwatch();
    Stopwatch s2 = new Stopwatch();
    Stopwatch s3 = new Stopwatch();
    Stopwatch s4 = new Stopwatch();
    Stopwatch s5 = new Stopwatch();
    Stopwatch s6 = new Stopwatch();
    try
        {
        s1.Start();
        ReceiveIP = new IPEndPoint(IPAddress.Any, UDPPort);
        s1.Stop();
        s2.Start();
        listener = new UdpClient(this.UDPPort);
        s2.Stop();
        s3.Start();
        // RequestData will contain a server request in the correct format
        // ReceivIP will contain the ip address of a discovered server.
        RequestData = listener.Receive(ref ReceiveIP);
        s3.Stop();
        s4.Start();
        // Which I then send back as a request
        listener.Send(RequestData, RequestData.Length, ReceiveIP);
        s4.Stop();
        s5.Start();
        // This will return server information such as the tcp port.
        BytesReceived = listener.Receive(ref ReceiveIP);
        s5.Stop();
        }
    catch (Exception ex)
        {
        MessageBox.Show(ex.Message);
        }
    finally
        {
        s6.Start();
        if (listener != null && listener.Client.Connected)
            listener.Close();
        s6.Stop();
        }
    Console.WriteLine("{0}:{1}:{2}:{3}:{4}:{5}",
                        s1.ElapsedMilliseconds,
                        s2.ElapsedMilliseconds,
                        s3.ElapsedMilliseconds,
                        s4.ElapsedMilliseconds,
                        s5.ElapsedMilliseconds,
                        s6.ElapsedMilliseconds);
    }
There probably isn't anything you can do about it in your code, but it may help you to know which part is sluggish.
 
Share this answer
 
v2
Comments
pkfox 21-Jan-16 12:18pm    
I know where it is Griff it's the first call to listener.Receive which is where the grunt work of finding the ip address of a server is done

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