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:
- VM External IP – `ADDRESS = “http://34.118.16.122″`: The error from above
- VM Internal IP – `ADDRESS = “http://10.186.0.2″`: The same error from above
- Generic – `ADDRESS = “http://+”`: Another error that states that ADDRESS is already in use by other process
- `ADDRESS = “http://localhost”`: It doesn’t give me any errors but that doesn’t achieve the goal which is to connect from outside.
Now, I want to mention that when I rand this code on my Windows PC and did the port forwarding in my personal router on 80, I managed to connect from outside my local network by using the global IP of my router, instead of the `192.168.X.X` one. Because of this success on my PC I believed that maybe I need to do the equivalent of port forwarding but on the Google Cloud’s VM Instance, so I asked Google Cloud’s Assistant AI and told me that I need to redirect the port `80` to `22` using `ssh -L 80:localhost:22 user@EXTERNAL_IP_ADDRESS` command. I tried to run this command in the Ubuntu shell however I got “Permission denied” error message even thou I was logged as superuser. However as far as I understand the SSH is a different protocol than HTTP and updating SSH port wouldn’t change anything regarding HTTP.
The next thing I tried is setting a new firewall rule that allows any HTTP connection on port 80 on any internal IP address, however this didn’t change anything.
So after reading what I tried to explain here, maybe you can tell me what to try next?
2
Answers
Use the address
0.0.0.0
, and not the external IP address.Also note that the default TCP port is 8080 and not 80. You can use port 80 but you must specify that when deploying the service.
The address
0.0.0.0
means listen on all IPv4 network adapters for connection requests.The actual address assigned to your instance is not known until the container instance starts. Knowing that address is not important for your use case (starting a process that listens for TCP connections).
I haven’t used google cloud for a little while.
Have you read the Google Cloud documentation for IP addresses, or in general under the section "connect to VMs"
(Image).
here is some Google Cloud docs i found about Locating IP addresses for an instance and Running a basic IIS web server. maybe you can get somthing from those 🙂
I have found Googles documentation to be very good in most cases, You could also try asking ChatGPT, I’ve often used it for google cloud related information