[英]Lidgren create server instance for a second time results in System.Net.Sockets.SocketException
I am using Lidgren in Monogame.我在 Monogame 中使用 Lidgren。 There is a possibility that a player would want to host a game, then exits the lobby, and wants to host again.玩家有可能想要主持一场比赛,然后离开大厅,并想再次主持。 However, when I do this in my code, the following exception is raised: System.Net.Sockets.SocketException: 'Only one usage of each socket address (protocol/network address/port) is normally permitted'但是,当我在我的代码中执行此操作时,会引发以下异常: System.Net.Sockets.SocketException: '每个套接字地址(协议/网络地址/端口)通常只允许使用一次'
Relevant code:相关代码:
// initializing the server
NetPeerConfiguration _config = new NetPeerConfiguration("some app id");
// code to enable certain messages.
_server = new NetServer(_config);
_server.Start();
// cleaning up the server instance
_server.Shutdown("server shutdown");
_server = null;
The code crashes at _server.Start()
with binding the socket.( m_socket.Bind()
).代码在_server.Start()
处因绑定套接字而崩溃。( m_socket.Bind()
)。
How can I clean the server/socket up so that I can create a new instance of the server a second time around?如何清理服务器/套接字,以便再次创建服务器的新实例?
The cause of this is an improper shutdown of the previous server's instance meaning the port is still in use by that instance.造成这种情况的原因是前一个服务器实例的不正确关闭,这意味着该端口仍在被该实例使用。 Seems to have been a bug, the internal cleanup didn't work properly https://github.com/lidgren/lidgren-network-gen3/issues/94 https://github.com/lidgren/lidgren-network-gen3/issues/94#issuecomment-369561026 however it was fixed so be sure to have the latest version of lidgren-network https://github.com/RevoluPowered/lidgren-network/tree/feature/socket-overhaul似乎是一个错误,内部清理工作不正常https://github.com/lidgren/lidgren-network-gen3/issues/94 https://github.com/lidgren/lidgren-network-gen3/问题/94#issuecomment-369561026但是它已修复,因此请确保拥有最新版本的lidgren-network https://github.com/RevoluPowered/lidgren-network/tree/feature/socket-overhaul
you can use _server.Socket.Close();
你可以使用_server.Socket.Close();
just to be sure the socket closes before starting another server or instance.只是为了确保套接字在启动另一个服务器或实例之前关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.