简体   繁体   English

应用程序客户端(android java)-服务器(python)

[英]app client (android java) - server (python)

I need to establish a communication between an android application and a server in Python with sockets. 我需要在带有套接字的Python中的android应用程序和服务器之间建立通信。

The client send a photo end the server reply with a string. 客户端向服务器端发送带照片的照片。

Here is my Server code: 这是我的服务器代码:

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()

I was able to do it to communicate with a client in python but now I have to make the client in Android. 我能够做到这一点,以便与python中的客户端通信,但是现在我必须在Android中创建客户端。 How can I do? 我能怎么做?

You will need to create a TCP client, java has classes for that, and maybe think about doing the hole communication on another Thread so you dont block the GUI 您将需要创建一个TCP客户端,java为此提供了类,并且可能考虑在另一个线程上进行漏洞通信,因此您不会阻塞GUI

This is an Snippet of a TCP client you can implement: 这是您可以实现的TCP客户端的代码段:

Snippet: 片段:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM