0

I have a REST API Web App deployed in Azure as an App Service, which I can successfully make requests to from a console application on my device. My goal is to transition from passing JSON's back and forth via HttpClient on my console side to establishing a connection between the two via web sockets.

Currently, I have the following function that gets called on startup in my App Service.

public static void StartListening()
{
    try
    {
        TcpListener listener = new TcpListener(IPAddress.Any, 80);

        listener.Start();

        using (TcpClient client = listener.AcceptTcpClient())
        {
            using (Stream stream = client.GetStream())
            {
                byte[] requestBuffer = new byte[100];
                int size = stream.Read(requestBuffer, 0, requestBuffer.Length);
                string msg = Encoding.ASCII.GetString(requestBuffer, 0, size);
            }
        }
    } catch (Exception ex) { /* handle exception here */ }
}

And in my console application:

static void StartConnection(string msg)
{
    try
    {
        Console.WriteLine("Accessing tcp");
        byte[] buffer = Encoding.ASCII.GetBytes(msg);

        using (TcpClient client = new TcpClient("hostname here", 80))
        {
            using (Stream stream = client.GetStream())
            {
                stream.Write(buffer, 0, buffer.Length);
            }
        }
    } catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); }
}

For reference, I used the format found here as my guide.

I've made sure that web sockets are set to "on" in my App Service's configuration. Other than that, I'm not aware of any changes I need to make thus far.

My issue comes when actually trying to call the function on the server side. With IpAddress.Any in my listener parameter, I get a 10013 error (Permission denied). Changing the port will sometimes change the error to 10048 (Already in use). When I substitute IpAddress.Any with the Inbound IP, my error changes to 10049 (Cannot assign requested address. The requested address is not valid in its context). Error code references here

If I understand correctly, Azure App Service's can only accept inbound traffic on ports 80 and 443, neither of which work in my case. I don't have much experience working with these technologies, so any insight is welcome.

Also, I have only been able to debug until the listener.Start() line in my code, since my current execution stops there. Because of this, if there's anything obvious that jumps out in the code that follows that, please feel free to point it out.

boulos
  • 3
  • 1
  • Appservices are not designed for long running (websocket) based tasks, they are for 'web sites'. You should be using WebJobs for what you want to do (which can be configured for long running tasks). Actually, it sounds more like you need to be using some of the 'mobile' technologies (in quotes because it's not just for mobile devices, but anything outside the cloud, like your client app). – Neil Aug 19 '20 at 17:19

2 Answers2

0

You'd better use a tool like ngrok which will act like a proxy and send data to your local dev station.

e.g.:

send data to ngrok / it will route to your localhost

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
0

Please use something in the middle, when using web sockets recommended way would be to use SignalR. Azure SignalR is a platform service and using web sockets or REST or other protocols you can do this, it will be scalable and not bring down your App Service as well by not using too many requests.

Hassan Raza
  • 412
  • 2
  • 6