I'm trying to make a simple C# HTTP Listener that listens for external connections. I installed dotnet
on the VM Instance(uses Ubuntu 22.04 by the way), and uploaded my source files for the HTTP Listener. This is the code:
Program.cs:
using System;
...
public class Program
{
public const string ADDRESS = "http://VM_EXTERNAL_IP";
public const int PORT = 80;
public static Server server;
static void Main(string[] args)
{
server = new Server(ADDRESS, PORT);
}
}
Server.cs:
public class Server
{
public string address;
public int port;
public Server(string address, int port)
{
HttpListener httpserver = new HttpListener();
httpserver.Prefixes.Add(address + ":" + port + "/");
httpserver.Start();
Console.WriteLine("Listening on " + address + ":" + port + "/");
while (httpserver.IsListening)
{
HandleHTTPConnections(httpserver);
}
}
}
I don't think the request-response handler code is relevant, because this part: httpserver.Prefixes.Add(address + ":" + port + "/");
, gives me errors:
Unhandled exception. System.Net.HttpListenerException (99): Cannot assign requested address
at System.Net.HttpEndPointManager.GetEPListener(String host, Int32 port, HttpListener listener, Boolean secure)
at System.Net.HttpEndPointManager.AddPrefixInternal(String p, HttpListener listener)
at System.Net.HttpEndPointManager.AddListener(HttpListener listener)
Now, I tried to set the address to a lot of values and here is the outcomes of each try:
So after reading what I tried to explain here, maybe you can tell me what to try next?
Use the address http://*
, and not the external IP address which is not accessible from the VM's network stack.
The address *
means listen on all network adapters for connection requests.
httpserver.Prefixes.Add("http://*:" + port.ToString() + "/");