简体   繁体   English

监听端口已经在使用中吗? Unity网络错误

[英]Is listen port already in use? Unity networking error

I am trying to Network a small 2 player Unity game. 我正在尝试联网一个2人小游戏。 This is my first go at networking, so please feel free to correct me on anything I am doing wrong. 这是我第一次接触网络,所以请随时纠正我做错的任何事情。 I am attempting to make a simple matchmaking service where, on the main menu of the game, a player can click the Find Match button. 我试图提供一种简单的配对服务,在游戏的主菜单上,玩家可以单击“查找配对”按钮。 This will execute the following function 这将执行以下功能

private HostData[] hostList;
private const string typeName = "UniqueGameName";
private const string gameName = "RoomName";
public void FindMatch() {
    RefreshHostList();
    if (hostList != null) {
        JoinServer(hostList[0]);
    } else {
        StartServer();
    }
}

And here are the corresponding methods called in that function. 这是该函数中调用的相应方法。

private void RefreshHostList()
{
    MasterServer.RequestHostList(typeName);
}

void OnMasterServerEvent(MasterServerEvent msEvent)
{
    if (msEvent == MasterServerEvent.HostListReceived)
        hostList = MasterServer.PollHostList();
}

private void JoinServer(HostData hostData)
{
    Network.Connect(hostData);
}

private void StartServer()
{
    Network.InitializeServer(2, 25000, !Network.HavePublicAddress());
    MasterServer.RegisterHost(typeName, gameName);
}

The idea is, when the player clicks Find Match, it will refresh the host list. 这个想法是,当玩家单击“查找匹配项”时,它将刷新主机列表。 If it finds a host, it will join their server and begin a match. 如果找到主机,它将加入其服务器并开始比赛。 If it doesn't, it will begin its own server and wait for another player. 如果没有,它将启动自己的服务器并等待另一个播放器。 When testing this, the first player that clicks Find Match is able to start a server, but then the second player receives the error message "...Is the listen port already in use?". 测试时,第一个单击“查找匹配项”的播放器可以启动服务器,但是第二个播放器会收到错误消息“ ...监听端口已在使用中?”。 When I debug and step through my program, the second player's hostList is always null, even with the first players successful server creation and registration. 当我调试并逐步执行程序时,即使第一个播放器成功创建和注册服务器,第二个播放器的hostList始终为null。

Are you using both instances of app on same machine? 您是否在同一台计算机上使用两个应用程序实例? If you occupy network port once then you are not able to initialize server on same port on the same machine. 如果占用网络端口一次,则无法在同一台计算机上的同一端口上初始化服务器。

In general I would recommend looking into SingalR technology for your needs, it uses websockets. 通常,我建议您根据需要使用SingalR技术,该技术使用了websocket。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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