android serversocket 4g网络不可达
android serversocket 4g network is unreachable
我有一个应用程序要求从 python 网络服务器接收 android 应用程序中的数据。我已经设置了一个本地环境来首先进行测试,并在本地托管了一个 python 服务器。使用套接字创建连接,服务器套接字在 android 应用程序上,客户端是 python 服务器。我基本上需要从服务器接收到应用程序的状态更新,而不需要它(通过 POST 排球,或类似改造)。
对于 android 方面,我基本上遵循了此页面上的 post... https://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/
这是 python 服务器中的客户端套接字...
@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"
这是 android MainActivity,其中线程具有侦听服务器套接字
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();
}
}
}
当 android phone 与 python 服务器位于同一 internal/local 网络时,一切正常。一旦我关闭无线并连接到 4g,python 就会吐出这个错误。我正在使用 mac 如果这有所不同。
~/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 -
我已经在我的路由器中转发了那个端口,并且可以通过它 ping 网站所以我知道流量正在从我的网络传到网络,但我不明白为什么它无法到达 android 设备。我也尝试了很多不同的端口,可能有一个我需要使用的特定端口。这是我第一次玩套接字,我还是一个菜鸟 android 开发者。我敢肯定,当我将其移至托管在 heroku 的 public python 服务器时,我将不得不更多地考虑安全性和 ssl 问题,但是谁能告诉我为什么无法访问android 设备在 public 网络上时?
如果您的 android 设备使用移动连接,则无法通过互联网访问其上的服务器 运行,因为您的移动提供商将阻止传入连接。
我有一个应用程序要求从 python 网络服务器接收 android 应用程序中的数据。我已经设置了一个本地环境来首先进行测试,并在本地托管了一个 python 服务器。使用套接字创建连接,服务器套接字在 android 应用程序上,客户端是 python 服务器。我基本上需要从服务器接收到应用程序的状态更新,而不需要它(通过 POST 排球,或类似改造)。
对于 android 方面,我基本上遵循了此页面上的 post... https://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/
这是 python 服务器中的客户端套接字...
@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"
这是 android MainActivity,其中线程具有侦听服务器套接字
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();
}
}
}
当 android phone 与 python 服务器位于同一 internal/local 网络时,一切正常。一旦我关闭无线并连接到 4g,python 就会吐出这个错误。我正在使用 mac 如果这有所不同。
~/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 -
我已经在我的路由器中转发了那个端口,并且可以通过它 ping 网站所以我知道流量正在从我的网络传到网络,但我不明白为什么它无法到达 android 设备。我也尝试了很多不同的端口,可能有一个我需要使用的特定端口。这是我第一次玩套接字,我还是一个菜鸟 android 开发者。我敢肯定,当我将其移至托管在 heroku 的 public python 服务器时,我将不得不更多地考虑安全性和 ssl 问题,但是谁能告诉我为什么无法访问android 设备在 public 网络上时?
如果您的 android 设备使用移动连接,则无法通过互联网访问其上的服务器 运行,因为您的移动提供商将阻止传入连接。