通过wifi热点播放Unity UNET问题

时间:2016-05-25 11:37:59

标签: unity3d udp wifi multiplayer unity3d-unet

我正在使用UNET开发局域网多人游戏。

我使用UDP多播来广播服务器ip,其他设备可以监听它。

我已经扩展了网络管理器,需要进行一些UI自定义。

使服务器可以自动播放其ip和其他设备的设备获取服务器ip(也可以是游戏名称)

在选择服务器时,客户端可以加入游戏。我避免了用户输入ip的情况。

我很高兴,因为当所有玩家通过路由器播放时它能够成功运行。

当我将任何设备设为Wifi Hotspot并且其他设备加入时,最糟糕的部分开始。

甚至服务器创建都失败了。

如果有人帮助我,我也可以分享我的代码。

是UNET的问题吗?

是否需要使用特定的解决方案?

在wifi热点中我们是否必须使用与普通路由器不同的端口?

如果有人遇到类似的话,我可以附上我的代码。

如果是这种情况,那么Unity提供的默认网络管理器如何工作?如果我们输入ip就行了。

我添加了一些额外的脚本sao用户不需要写ip并监听服务器消息。

我认为由于udp多播功能或端口问题导致我的代码崩溃。

我附上了我的代码。

服务器代码:

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;
using System.Text;
using UnityEngine.UI;
using UnityEngine.Networking; // added for test

public class ServerDemoSample : MonoBehaviour 
{
    public static ServerDemoSample instance;

    //public byte[] buffer;
    UdpClient serverOriginator;
    public string serverIP;
    public int broadcastPort = 8080; //THIS IS ORIGINAL
    //int broadcastPort = 7777;
    public IPAddress groupIP = IPAddress.Parse ("239.0.0.222");
    //public IPAddress groupIP = IPAddress.Parse ("224.0.1.0");

    public IPEndPoint remoteEP;

    public Text myIpText;
    public bool isRunning;

    public Button startGameButton;
    public Button creatServerButton;
    public bool iAmServer = false;

    void Start () 
    {
        //DontDestroyOnLoad (gameObject);
        myIpText.text = Network.player.ipAddress;
    }

    void Update()
    {
    }

    public void BroadcastServerIP() 
    {
        Debug.Log(Time.realtimeSinceStartup + ": Broadcasting IP:" + serverIP);
        byte[] buffer = ASCIIEncoding.ASCII.GetBytes(serverIP);
        serverOriginator.Send(buffer, buffer.Length, remoteEP);
        GLOBALS.instance.serverIpAddress = serverIP;
        isRunning = true;
    }

    public void StartServer()
    {
        serverIP = Network.player.ipAddress;
        iAmServer = true;
        GLOBALS.instance.iAmGlobalServer = true;
        //Create UDP Client for broadcasting the server
        serverOriginator = new UdpClient();

        serverOriginator.JoinMulticastGroup(groupIP);

        remoteEP = new IPEndPoint(groupIP, broadcastPort);
        GLOBALS.instance.serverIpAddress = serverIP;
        creatServerButton.transform.FindChild("Text").GetComponent<Text>().text = "Server Created!";
        //Broadcast IP
        InvokeRepeating("BroadcastServerIP", 0, 1f);
        print ("Server IP (SERVER_SCRIPT) :  " + serverIP);
    }
}

客户代码:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
using System;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.Collections;
using System.Collections.Generic;

public class ClientDemoScript : MonoBehaviour 
{
    public static ClientDemoScript instance;
    UdpClient client;
    int receivePort = 8080; //THIS IS ORIGINAL
    public string serverIP_;
    IPAddress groupIP_ = IPAddress.Parse("239.0.0.222");
    public string ipFound;
    byte[] receivedBytes = {0,0,0,0}; // made private for all 

    IPEndPoint remoteEP_;
    public Text serverIp2Text;
    public string results;
    public Text displayServerList;

    public List<string> receivedIpList = new List<string>();

    void Start () 
    {
        instance = this;
        JoinServer();
    }

    void Update()
    {
        serverIp2Text.text = results;
    }

    public void ReceiveServerInfo(IAsyncResult result) 
    {   
        Debug.Log("Received Server Info");

        remoteEP_ = new IPEndPoint(IPAddress.Any, receivePort);

        if (client != null) 
        {
            if (result != null) 
            {
                receivedBytes = client.EndReceive (result, ref remoteEP_);
            } 
            else 
            {
                return;
            }
        }

        ipFound = Encoding.ASCII.GetString (receivedBytes); //made public var

        if (!receivedIpList.Contains (ipFound)) 
        {
            receivedIpList.Add (ipFound);
        }



        Debug.Log("ip: "+ ipFound);

        client.BeginReceive (new AsyncCallback (ReceiveServerInfo), null);
        GLOBALS.instance.serverIpAddress = ipFound;
    }

    public void JoinServer()
    {
        if (client == null) 
        {
            client = new UdpClient(receivePort);
            client.JoinMulticastGroup(groupIP_);
        }

        client.BeginReceive (new AsyncCallback (ReceiveServerInfo), null);
    }

}

0 个答案:

没有答案