2016-05-06 3 views
0

AndroidアプリケーションとPythonでソケット付きのサーバ間の通信を確立する必要があります。アプリケーションクライアント(android java) - サーバ(Python)

クライアントは文字列でサーバーの返信を終了します。ここで

は私のサーバーのコードです:

import socket 
import os 
import subprocess 

s = socket.socket() 
host = "10.255.20.186" # Get local machine name 
port = 9090    # Reserve a port for your service. 
s.bind((host,port)) 
s.listen(4) #number of people than can connect it 

# os.system("python C:\\Users\\Federica\\PycharmProjects\\client-server\\Client2.py") 

sc, address = s.accept() 
print "Connected by: " , address 
sb = 'C:\\Users\\Federica\\PycharmProjects\\client-server\\ricevi' 

#os.chdir(sb) 
fln=sb + os.sep + sc.recv(8)  #read the name of the file 
print fln 
f = open(fln,'wb')     #create the new file 
size = sc.recv(5)     #receive the size of the file 
#size=size[:7] 
print size 
strng = sc.recv(int(size))   #receive the data of the file 
#if strng: 
f.write(strng)      #write the file 
f.close() 

# here I send the photo to Matlab 
cmd = '"matlab" -wait -nodesktop -nosplash -minimize -r "cd C:\\Users\\Federica\\PycharmProjects\\client-server\\; somma(\''+ fln + '\'); exit;"' 

subprocess.call(cmd, shell=True) 

imgPath = "C:\\Users\\Federica\\PycharmProjects\\client-server\\ricevi\\nuova.png" 
print os.path.isfile(imgPath) 
#subprocess.call('start \'' + imgPath + '\'') 

sc.close() 
s.close() 

私はPythonでクライアントと通信するためにそれを行うことができたが、今私は、Androidクライアントをしなければなりません。 どうすればいいですか?

答えて

0

あなたはJavaはそのためのクラスがあり、TCPクライアントを作成する必要があり、そして多分あなたはこれをすることができますTCPクライアントの抜粋であるGUI

を阻止いけないので、別のスレッドに穴通信を行うことを考えます実装:

スニペット:

Socket clientSocket = new Socket("10.255.20.186", 9090); 
DataOutputStream outToPythonServer = new DataOutputStream(clientSocket.getOutputStream()); 
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
sentence = inFromUser.readLine(); 
outToPythonServer.writeBytes(sentence + '\n'); 
modifiedSentence = inFromServer.readLine(); 
System.out.println("FROM SERVER: " + modifiedSentence); 
clientSocket.close(); 
関連する問題