SocketPolicy 异常 int TCP Unity 与 C# 服务器之间的连接
Posted
技术标签:
【中文标题】SocketPolicy 异常 int TCP Unity 与 C# 服务器之间的连接【英文标题】:SocketPolicy exception int TCP Connection between unity and C# server 【发布时间】:2017-05-13 08:26:31 【问题描述】:我正在开发简单的多人游戏,我正在自己的编码服务器上工作(在 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);
并且统一客户端有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;
本地(使用“localhost”地址)一切正常! 但是当我试图通过互联网或本地 ip 与朋友一起测试它时,它会抛出这个异常:
在 System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy) [0x00000] in :0 在 System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in :0 在 System.Net.Sockets.TcpClient.Connect (System.Net.IPEndPoint remote_end_point) [0x00000] in :0 在 System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, Int32 端口) [0x00000] in :0 UnityEngine.Debug:Log(Object)
哪里有问题?我读了一些关于它的东西,问题应该只出现在统一网络播放器中,对吧?但我正在为 Windows(EXE 文件)构建它,但我也尝试将它构建到 WEB GL 播放器中(由于线程存在问题)
PS。我将路由器中的 4200 端口转发到我的服务器(通过 Internet 连接)
感谢您的帮助
【问题讨论】:
【参考方案1】:好的问题不在政策中。即使我转发了端口,我的服务器也没有在那个打开的端口上监听。
我不确定究竟是什么问题,但表单服务器代码我将 ip 表单 127.0.0.1 更改为 0.0.0.0 并将端口类型 Int32 更改为经典 int。
这解决了我的问题。
如果有人能解释我会很高兴:)
【讨论】:
以上是关于SocketPolicy 异常 int TCP Unity 与 C# 服务器之间的连接的主要内容,如果未能解决你的问题,请参考以下文章