Lidgren create server instance for a second time results in System.Net.Sockets.SocketException

Stack Overflow Asked by Danahi on October 22, 2020

I am using Lidgren in Monogame.
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’

Relevant code:

// initializing the server
NetPeerConfiguration _config = new NetPeerConfiguration("some app id");
// code to enable certain messages.
_server = new NetServer(_config);

// cleaning up the server instance
_server.Shutdown("server shutdown");
_server = null;

The code crashes at _server.Start() with binding the socket.(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?

One Answer

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 however it was fixed so be sure to have the latest version of lidgren-network

you can use _server.Socket.Close(); just to be sure the socket closes before starting another server or instance.

Answered by Thompson sparta on October 22, 2020

Add your own answers!

Related Questions

how to count number of file with a matching pattern using python

2  Asked on February 16, 2021 by suryansh-mathur


R First Row By Group When Condition Is Met

4  Asked on February 16, 2021 by bvowe


C++ program won’t print anything with vectors?

2  Asked on February 16, 2021 by luckylone-official


What is damping for?

3  Asked on February 16, 2021 by lix


Spring 5 MVC not finding mapping to controller returning JSON

2  Asked on February 16, 2021 by gary-kephart


Pointer value in c

3  Asked on February 15, 2021 by gsoap


Memory allocation of map[int]interface{} vs map[int]struct{}

2  Asked on February 15, 2021 by gabriel-gonzalez


Does the “leftmost prefix rule” of Index applies to SQL Server?

2  Asked on February 15, 2021 by drillfreak100


How to add ArrayList to JsonObject in Kotlin

2  Asked on February 15, 2021 by rajitha-perera


How to get data from axios request?

1  Asked on February 15, 2021 by theyaxxe


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP