Unable to connect to a flask server running on my pc from my phone when my pc is connected to my phones hotspot

58 Views Asked by At

I'm somewhat new to development and encountered an issue about which I could not find any info. I've got 2 apps, one to run on a mobile device (.NetMAUI), and another to create a server on a different device (python and flask).

To simplify things let's say that's how the non-mobile app looks:

from flask import Flask, Response, request, jsonify
app = Flask(__name__)
@app.route('/scan', methods=['POST'])
def json_scan_task():
    return jsonify({'it ran':'thats good'})

if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(port=8080, host='0.0.0.0')

and the mobile connecting app does this to connect to the server and get the json response

public static async Task<string> MakePost(string Tasktype)
{
//URIString is a static property and a formated string $"http://{server_ip}:{server_port}/scan"
    var WR = (HttpWebRequest)HttpWebRequest.Create(new Uri(URIString));
    WR.ContentType = @"application/json";
    WR.Method = @"POST";
    WR.Accept = @"text/plain";
    WR.Timeout = (90 * 1000);//1.5 minuty w milisekundach
    var streamWriter = new StreamWriter(WR.GetRequestStream());
    streamWriter.Write(Tasktype);
    streamWriter.Close();

    //httpResponse
    var HR = WR.GetResponseAsync();
    HR.Wait();
    WebResponse httpResponse = HR.Result;

    var stream = new StreamReader(httpResponse.GetResponseStream());
    string response = stream.ReadToEnd();
    stream.Close();

    return response;
}

This works fine when both my phone and PC are connected to the same wifi or a hotspot on another device, tested it multiple times and used it often during the development of the mobile app.

But it fails to connect when my PC is connected to my phones hotspot with "This site can't be reached". It's necessary for the data connection to be a hotspot on a mobile device and for the server to run on another, this isn't dictated by me.

I've made sure that the new IP for the server was correctly written in the mobile app, checked on multiple mobile devices and multiple pc's.

I know that above in the code the method type is POST and below are examples with GET, but that should not affect whether a mobile device can or cannot connect to a server, invalid method type should result in a 405 not "This site can't be reached"

server running on my pc while both devices are connected to the same wifi

mobile device connected to the server

same code, on the same pc, but connected to a mobile hotspot

same phone, this time with a hotspot running and my pc connected to it

Method not allowed example, both my phone and pc connected to the same wifi

0

There are 0 best solutions below