从客户端(Android-Mobile)向服务器(Python)发送消息

时间:2019-05-20 21:07:06

标签: java android python-3.x sockets socket.io

我正在开发一个具有服务器(Python 3.x)和客户端(Android Studio)的项目。客户端(手机)获取图像(位图)并将其发送到服务器(Python)。在服务器部分,它做了一些深度学习的工作并生成了预测。因此,我需要将该预测(字符串)发送回客户端。但是,我正在使用套接字编程,它不起作用。我已经为此工作了很长时间,并且截止日期已经到来。所以,有人可以帮我吗?

该错误可能是clientSocket.getInputStream()的原因(但我不确定)

HEREİSHOW SERVER(PYTHON)看起来很像


import socket
import base64

HOST = 'localhost'  
PORT = 5433        # 1023 ten büyük bir sayı olsun.

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen(5)
    i = 1 #Dosya adıyla ilgili.

    while True:
        conn, addr = s.accept()
        with conn:
            print(' > connected by ', addr)

            #Take image that was sent from Client
            data = base64.b64encode(b'')
            while True:     
                yenidata = conn.recv(1024)            
                if not yenidata:
                    break            
                data = data + yenidata 

            #Save the image to do DL part.
            fileName = 'yeniResim'+str(i)
            with open("C:/Users/MUSTAFAAKTAS/Desktop/Indirilenler/"+fileName+".png", "wb") as fh:
                fh.write((base64.decodebytes(data))) 
                i = i + 1 
                print(" >",fileName," saved.")

            #In here, I randomly wrote "Message", but in real, I do some DL stuff and try to send the prediction to the Client
            conn.send(bytes("Message",'UTF-8'))
            print(" > Message sent")

此处立即提供(ANDROİD)部分


#I create a connection like this,
Socket client = new Socket(IP, PORT);

#I need 'cevap' which is the prediction that was sent from the server to client
String cevap = ServerRespond();


public String ServerRespond(){

        if(client.isClosed()) {
            return "SERVER Closed";
        }
        else {
                try {
                    ServerSocket ss = new ServerSocket(5433);
                    Socket s = ss.accept();
                    InputStream sin = s.getInputStream();
                    DataInputStream dis = new DataInputStream(sin);
                    return dis.readUTF().toString();
                } catch (SocketTimeoutException s) {
                    return "Socket timed out!";
                } catch (Exception ioe) {
                    return "IO HATASI " + ioe.getLocalizedMessage();
                }

        }
    }

非常感谢您。

0 个答案:

没有答案