我有一个应用程序要求从python Web服务器接收Android应用程序中的数据。我已经设置了一个本地环境来首先测试,本地托管了一个python服务器。使用套接字创建连接,服务器套接字在Android应用程序上,客户端是python服务器。我基本上需要从服务器接收状态更新到应用程序,而不需要它(通过带有排球的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手机与python服务器位于同一内部/本地网络时,工作正常。一旦我关闭无线,并连接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上托管的公共python服务器时,我将不得不更多地考虑安全性和ssl,但是任何人都可以告诉我为什么当它在公共网络上时无法到达Android设备?
答案 0 :(得分:1)
如果您的Android设备处于移动连接状态,则无法从互联网上访问其上运行的服务器,因为您的移动服务提供商将阻止传入连接。