multicast clients connected via hamachi

211 Views Asked by At

I have made a server that uses UDP multicast sockets. I am able to run the server on my network and reach every client. The problem is that I was now trying to reach clients that are connected via hamachi. I've read that it supports multicast, however, the messages do not seem to be received.

This is how i create the udpclient:

        localIPaddress = IPAddress.Any;
        multicastAddress = = IPAddress.Parse("233.0.0.2");
        multicastPort = 7778;

        // Create endpoints
        multicastEndPoint = new IPEndPoint(multicastAddress, multicastPort);
        remoteEndPoint = new IPEndPoint(_localIPaddress, multicastPort);

        // Create and configure UdpClient
        socket = new UdpClient();
        // The following three lines allow multiple clients on the same PC
        socket.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        socket.ExclusiveAddressUse = false;
        // Bind, Join
        socket.Client.Bind(remoteEndPoint);
        socket.JoinMulticastGroup(multicastAddress);

        // Start listening for incoming data
        socket.BeginReceive(new AsyncCallback(ReceiveCallback), null);

This is how I receive information:

    private void ReceiveCallback(IAsyncResult _result)
    {
        try
        {
            IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, multicastPort);
            byte[] data = socket.EndReceive(_result, ref clientEndPoint);
            // Restart listening for udp data packages
            socket.BeginReceive(new AsyncCallback(ReceiveCallback), null);

            if (data.Length < 4)
            {
                Disconnect();
                return;
            }

            // Handle Data
            using (Packet packet = new Packet(data))
            {
                HandleData(packet);
            }
        }
        catch (Exception ex)
        {
            if (instance.isConnected)
                Debug.Log($"Error receiving UDP Multicast data: {ex}");
        }
    }

And this is how I send information:

    public void SendData(Packet _packet)
    {
        try
        {
            socket.Send(_packet.ToArray(), _packet.Length(), multicastEndPoint);
        }
        catch (Exception ex)
        {
            Debug.Log($"Error multicasting UDP data: {ex}");
        }
    }

Any information would be appreciated as there isn't much about how to solve this.

0

There are 0 best solutions below