Click here to Skip to main content
15,888,202 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want status update from the devices which communicate on udp, every minute. what method i should use. Any kind of suggestions will be appreciated. Thanks

What I have tried:

I have tried this

foreach (IPAddress ips in iPAddresses)
        {
            Byte[] receiveBytes = { };
            Byte[] dataToSend = new Byte[] { 0x8B, 0xB9, 0x00, 0x03, 0x05, 0x01, 0x09 };
            IPEndPoint endPoint = new IPEndPoint(ips, 1024);
            EndPoint ep = (EndPoint)endPoint;
            Socket udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            try
            {
                udpClient.Connect(endPoint);
                udpClient.SendTo(dataToSend, ep);
               // Thread.Sleep(50);
                IPEndPoint localip = new IPEndPoint(IPAddress.Any, 1200);

                UdpClient receivingUdpClient = new UdpClient(localip);


                receiveBytes = receivingUdpClient.Receive(ref endPoint);
               // Thread.Sleep(50);

                udpClient.Close();


                receivingUdpClient.Close();
                receivingUdpClient.Dispose();
                updateData(receiveBytes, ips);
            }
            finally
            {

                udpClient.Dispose();
            }

        }



The problem I can find is through this line-

receiveBytes = receivingUdpClient.Receive(ref endPoint);



ref endpoint in receive() is having 1st ip address only. its not changing. Send method is able to change ip address. but not receive method.

I have tried some other methods too before this. but this is what I am doing now
Posted
Updated 8-Aug-18 17:27pm

1 solution

You're not checking the "connect" (return) status.

You're probably failing to connect; or not "waiting" long enough.

But since you're effectively ignoring any errors ("empty" ex handler), you're left to wonder what's going on.
 
Share this answer
 
Comments
Member 13769611 9-Aug-18 0:22am    
ok... so you mean this code above I have written will work for multiple clients... its just that I am unable to connect thats why the data is received by 1st IP address.
I will try adding wait and check connection too... and will let you know what works and how...
Thank you so much for looking in to it...
Member 13769611 9-Aug-18 2:36am    
Hi Gerry, Its not working that way. Its always getting true when check Connected status. And another main issue which I am failing to understand is that even after I dispose udp socket then also it stays connected to that IP.
Means if I start a new debug then also the remote endpoint is the same that was in last debug. how is that possible
Member 13769611 9-Aug-18 3:14am    
so, I read and concluded actually udp never connects like tcp. so no need of using connect method. without that udp transfers data.. so connection is not a problem here.. I think the issue is receivingudpclient.
Member 10765128 22-Jun-19 23:04pm    
take the UdpClient out of the foreach loop create the socket somewhere else and make it a private variable from here use that socket to send and receive.

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