SocketPolicy exception int unity和C#服务器之间的TCP连接



我正在开发简单的多人游戏,我正在自己的编码服务器上工作(在 C# 中)。

在服务器上有TCPListener:

public void StartClientHandler()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("ClientHandler started");
Console.ForegroundColor = ConsoleColor.White;
TcpListener server = null;
try
{
Int32 port = 4200;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
server = new TcpListener(localAddr, port);
server.Start();

while (true)
{
TcpClient client = server.AcceptTcpClient();
lobby.addToOnline(new Client(client,lobby));
}
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}

Unity 客户端具有 TCPClient:

static public bool InitializeConnection()
{
try
{
client.Connect(ip, port);
if (client.Connected)
{
stream = client.GetStream();
Thread listener = new Thread(new ThreadStart(Listener));
listener.Start();
}
else Debug.Log("not connected:(");
return client.Connected;
}
catch (Exception e)
{
Debug.Log(e.StackTrace);
return false;
}

}

本地(使用"本地主机"地址)一切都很完美! 但是当我尝试通过互联网或本地 ip 与朋友一起测试它时,它会出现以下异常:

at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy) [0x00000] in :0 at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in :0 at System.Net.Sockets.TcpClient.Connect (System.Net.IPEndPoint remote_end_point) [0x00000] in :0 at System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, Int32 port) [0x00000] in :0 UnityEngine.Debug:Log(Object)

哪里可能有问题?我读了一些关于它的内容,问题应该只出现在 Unity 网络播放器中,对吗?但是我正在为Windows(EXE文件)构建它,但是我也尝试将其构建到WEB GL播放器中(由于线程而出现问题)

附言。我将路由器中的端口 4200 转发到我的服务器(用于互联网连接)

感谢您的任何帮助

好的问题不在政策中。即使我转发了端口,我的服务器也没有侦听打开的端口。

我不确定问题到底是什么,但是从服务器代码中,我将ip形式127.0.0.1更改为0.0.0.0,并将端口形式Int32的类型更改为经典int。

这解决了我的问题。

如果有人能够解释,我会很高兴:)

最新更新