I am trying to create a simple HttpServer in flutter. I am developing on an apple silicone mac (MacOS Monterey).
I am constantly getting the following error message (independently of the port I try to bind - and I know that it is not currently binded):
SocketException (SocketException: Failed to create server socket (OS Error: The shared flag to bind() needs to be
trueif binding multiple times on the same (address, port) combination.), address = 127.0.0.1, port = 18006)
Anyone knows how to fix this? It is clear that the port is not bind (any port will default to the same exception!). Is this one more of the "protective" features of MacOS Monterey?
Thank you!
Code example:
class _HomeState extends State<Home> {
String statusText = "Start Server";
startServer() async {
setState(() {
statusText = "Starting server on Port : 18034";
});
var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 18006);
print("Server running on IP : " +
server.address.toString() +
" On Port : " +
server.port.toString());
await for (var request in server) {
request.response
..headers.contentType =
new ContentType("text", "plain", charset: "utf-8")
..write('Hello, world')
..close();
}
setState(() {
statusText = "Server running on IP : " +
server.address.toString() +
" On Port : " +
server.port.toString();
});
}