简体   繁体   中英

send data using udp to multiple clients c#

I know there are already many queries on this forum. I have searched and at the end I am stuck to this issue thats why I am asking it. I have a number of devices which can communicate on UDP. I want to query them for status update. This is my code.

 List<IPAddress> iPAdd = new List<IPAddress>();
        foreach (string s in ipaddress)
        {
            IPAddress ips = IPAddress.Parse(s);
            iPAdd.Add(ips);
        }
        //The main socket on which the server listens to the clients
        byte[] byteData = new byte[1024];
        Byte[] dataToSend = new Byte[] { 0x8B, 0xB9, 0x00, 0x03, 0x05, 0x01, 0x09 };


        try
        {
            serverSocket = new Socket(AddressFamily.InterNetwork,
          SocketType.Dgram, ProtocolType.Udp);
            //Assign the any IP of the machine and listen on port number 1200
            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 0);
            //Bind this address to the server
            serverSocket.Bind(ipEndPoint);
            foreach(IPAddress ip in iPAdd)
            {
                IPEndPoint ipe = new IPEndPoint(ip, 1024);
                EndPoint e = (EndPoint)ipe;
                eps.Add(e);
            }


            //Start sending data

            foreach(EndPoint ep in eps)
            {
                serverSocket.BeginSendTo(dataToSend, 0, dataToSend.Length, SocketFlags.None, ep,
                            new AsyncCallback(OnSend), ep);
                Thread.Sleep(50);
                Receive(ep);

            }

        }
        catch (Exception ex)
        {
             string mess = ex.Message;
        }

receive method -

 private void Receive(EndPoint ep)
    {
        Byte[] receiveBytes = new Byte[1024];
        IPEndPoint localip = new IPEndPoint(IPAddress.Any, 1200);

        UdpClient receivingUdpClient = new UdpClient(localip);



        receivingUdpClient.Client.Receive(receiveBytes);

        receivingUdpClient.Dispose();
    }

Error I am receiving by receivingUdpClient is -

"A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied"

What Am I doing wrong.? If I am completely wrong then also.. Any suggestions and advice are welcome

I got the way to do what I wanted to do. Here is the code and explaination of what I was trying to do. Hope it help others. MY task - Needed to create udp connection between multiple devices of same kind that receive query on 1024 and replied on 1200. At the end I realized its simple as it is for single client. Now I am thinking how silly of me getting confused by these many options available. Here is the code -

 public byte[] serverSocket(String IPadd)
    {
        Socket serverSocket = new Socket(AddressFamily.InterNetwork,
             SocketType.Dgram, ProtocolType.Udp);
        Socket receiveClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        byte[] receivebytes = new byte[100] ;
        try
        {

            byte[] dataToSend = new byte[] { 0x8B, 0xB9, 0x00, 0x03, 0x05, 0x01, 0x09 };
            IPAddress iPAddress = IPAddress.Parse(IPadd);
            int port = 1024;
            IPEndPoint iPEnd = new IPEndPoint(iPAddress, port);

            //Assign the any IP of the machine and listen on port number 1200
            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 1200);
            serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
            serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 2000);
            serverSocket.Bind(ipEndPoint);
            EndPoint ep = (EndPoint)iPEnd;

            serverSocket.SendTo(dataToSend, ep);
            Thread.Sleep(1000);
            serverSocket.Receive(receivebytes);
        }
        catch(Exception ex)
        {

        }
        finally
        {
            serverSocket.Dispose();

        }

        return receivebytes;
    }

String IPadd is the string from the list of ipaddresses that I got from database. So here what I am doing is - I am calling udp Connection and disposing it after receiving data so that I can use the same socket object for other IP address.

See its as simple as that.

And last but not the least, the guys on this forum.. Thank you very much because your suggestions gave me a different look to think of.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM