所以我有一個簡單的套接字服務器在Android模擬器上。當我只向它發送數據時,它工作得很好。但是,如果我想將這些數據回顯給python腳本,它根本不起作用。下面是工作代碼:蟒蛇從套接字接收
安卓
try {
serverSocket = new ServerSocket(port);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (checkingForClients) {
try {
clientSocket = serverSocket.accept();
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
line = null;
while ((line = in.readLine()) != null) {
Log.d("ServerActivity", line);
/* THIS IS THE LINE THAT DOESN'T WORK*/
//out.println(line);
handler.post(new Runnable() {
@Override
public void run() {
if(incomingData == null){
Log.e("Socket Thingey", "Null Error");
}
//out.println(line);
incomingData.setText("Testing");
incomingData.setText(line);
}
});
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
蟒蛇:
import socket
host = 'localhost'
port = 5000
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
s.send('Hello, World!')
# data = s.recv(size) THIS LINE CAUSES PROBLEMS
s.close()
print 'Received:' , data
因此,有2註釋行。沒有這些線,它完美的作品。但是,如果我在Python中添加s.recv(size)
,它就會凍結,我假設等待接收到的數據。但問題是,android代碼永遠不會獲取發送的數據。所以我不知道該怎麼做。
請記住,我是新來的python和套接字。