salminnella
salminnella

Reputation: 754

android serversocket 4g network is unreachable

I have an app requirement to receive data in an android app from a python web server. I've setup a local environment to test first, with a python server hosted locally. Using sockets to create the connection, the server socket is on the android app and the client is the python server. I basically need to receive a status update from the server to the app, without asking for it (via POST with volley, or something like retrofit).

For the android side, I have essentially followed the post at this page... https://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/

here's the client socket in the python server...

@app.route('/clientMessage', methods=['GET', 'POST'])
def clientMessage():
    address = request.values.get('address')
    host = address
    port = 6667

    #create an INET, STREAMing socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))

    #send message using socket
    s.sendall("please work\n")

    return "msg sent"

Here's the android MainActivity where the thread that has the listening server socket

public class ServerThread implements Runnable {

    public void run() {
        try {
            if (SERVERIP != null) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        serverStatus.setText("Listening on IP: " + SERVERIP);

                    }
                });
                serverSocket = new ServerSocket(SERVERPORT);
                while (true) {
                    // LISTEN FOR INCOMING CLIENTS
                    Socket client = serverSocket.accept();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            serverStatus.setText("Connected.");
                        }
                    });

                    try {
                        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                        String line = null;
                        while ((line = in.readLine()) != null) {
                            Log.d("ServerActivity", line);
                            final String finalLine = line;
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    mainTextView.setText(finalLine);
                                }
                            });
                        }
                        break;
                    } catch (Exception e) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                serverStatus.setText("Oops. Connection interrupted.");
                            }
                        });
                        e.printStackTrace();
                    }
                }
            } else {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        serverStatus.setText("Couldn't detect internet connection.");
                    }
                });
            }
        } catch (Exception e) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    serverStatus.setText("Error");
                }
            });
            e.printStackTrace();
        }
    }
}

Things work fine when the android phone is in the same internal/local network as the python server. As soon as i turn off the wireless, and connect on the 4g, python spits out this error. I am using a mac if that makes a difference.

~/Documents/androidProjects/pythonExamples$ python serverSockets.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2016-11-04 09:45:05,876] ERROR in app: Exception on /clientMessage [GET]
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
  File "serverSockets.py", line 66, in clientMessage
s.connect((host, port))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
error: [Errno 51] Network is unreachable
127.0.0.1 - - [04/Nov/2016 09:45:05] "GET /clientMessage?address=100.74.228.97 HTTP/1.1" 500 -

I have forwarded that port in my router, and can ping sites through it so i know traffic is getting out of my network to the net, but i can't figure out why it can't reach the android device. I've tried lots of different ports as well, there may be a specific one i need to use. This is my first time playing with sockets, and i'm still a rookie android developer. I'm sure i'll have to think more about security and ssl when i move this to the public python server hosted at heroku, but can anyone give me some insight why this can't reach the android device when its on a public network?

Upvotes: 0

Views: 737

Answers (1)

greenapps
greenapps

Reputation: 11224

If your android device is on mobile connection then a server running on it is not reachable from the internet as your mobile provider will block incoming connections.

Upvotes: 1

Related Questions