Udp组播解决多网段的问题
public class UdpControl : MonoBehaviour
{
    // public LeftVideoControl leftControl;
    // public RightVideoControl rightControl;
    public PageControl pageControl;

    private UdpClient udpClient;
    private Queue<string> data;
    private string ip;
    private CancellationTokenSource _cancellationTokenSource;

    private void Awake()
    {
        data = new Queue<string>();
        ip = File.ReadAllText(Application.streamingAssetsPath + "/config/ip.txt").Trim();
        _cancellationTokenSource = new CancellationTokenSource();
    }

    void Start()
    {
        udpClient = new UdpClient(50001);

        // try
        // {
        //     if (ip != "0")
        //     {
        //         udpClient.Client.SetSocketOption(
        //             SocketOptionLevel.IP,
        //             SocketOptionName.MulticastInterface,
        //             IPAddress.Parse(ip).GetAddressBytes()
        //         );
        //         udpClient.JoinMulticastGroup(IPAddress.Parse("239.0.0.1"), IPAddress.Parse(ip));
        //     }
        //     else
        //     {
        //         udpClient.JoinMulticastGroup(IPAddress.Parse("239.0.0.1"));
        //     }
        // }
        // catch (Exception e)
        // {
        //     print(e.Message);
        // }

        ReceiveMsg();
    }
}
上一篇