我正在使用 UNET 开发 LAN 多人游戏。
我已经使用 UDP 多播来广播服务器 ip,其他设备可以收听它。
我已经为一些需要的 UI 定制扩展了网络管理器。
制作服务器的设备可以广播自己的ip,其他设备自动获取列表中的服务器ip(也可以是游戏名)
在选择服务器时,客户端可以加入游戏。我已经避免了用户输入 ip 的情况。
我很高兴,因为当所有玩家都通过路由器播放时,它可以成功运行。
当我制作任何设备作为 Wifi 热点和其他设备加入它时,最糟糕的部分开始了。
甚至服务器创建失败。
如果有人要帮助我,我也可以分享我的代码。
UNET 有问题吗?
是否需要使用特定的解决方案?
在wifi热点中,我们必须使用与普通路由器不同的端口吗?
如果有人经历过类似的事情,我可以附上我的代码。
如果是这种情况,那么统一提供的默认网络管理器如何工作?如果我们输入 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);
}
}