Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
756 views
in Technique[技术] by (71.8m points)

Send image to android studio client from python server via socket programming

I have made a python program that uses socket communication to send images. i made a python server and client program for this purpose where the server sends an image to the client. Now i want to make an android application (via android studio) that acts as a client and receives the image from the python server. I am a beginner to java and have never used android studio prior to this. I would like to know the functions,classes etc that would help me with this problem and how i should implement them (not the code exactly but just how the java code should receive the data). The code for my python server is :

import socket
from time import sleep
from time import time

host = ''
port = 5560

filePath = "/home/pi/Desktop/img.jpg"

def setupServer():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print("Socket created.")
    try:
        s.bind((host, port))
    except socket.error as msg:
        print(msg)
    print("Socket bind comlete.")
    return s

def setupConnection():
    s.listen(1) # Allows one connection at a time.
    conn, address = s.accept()
    print("Connected to: " + address[0] + ":" + str(address[1]))
    return conn

def sendPic(s, filePath):
    print(filePath)
    pic = open(filePath, 'rb')
    chunk = pic.read(1024)
    s.send(str.encode("STORE " + "C:/Users/usr/Downloads/img.jpg" ))
    t = time()
    while chunk:
        print("Sending Picture")
        s.send(chunk)
        chunk = pic.read(1024)
    pic.close()
    print("Done sending")
    print("Elapsed time = " + str(time() - t) + 's')
    s.close()
    return "Done sending"



def backup(filePath):
    conn = setupConnection()
    response = sendPic(conn, filePath)
    return response


        

s = setupServer()


while True:
    print(filePath)
    backup(filePath)
    print("Operation Complete")
    break

and the python client code is :

import socket
from time import sleep
from time import time

host = '192.168.1.137'
port = 5560


def setupSocket():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    return s


def storeFile(filePath):
    picFile = open(filePath, 'wb')
    print("Opened the file.")
    pic = conn.recv(1024)
    while pic:
        print("Receiving picture still.")
        picFile.write(pic)
        pic = conn.recv(1024)
    picFile.close()

def dataTransfer(conn):
    # A big loop that sends/receives data until told not to.
    while True:
        # Receive the data
        data = conn.recv(1024) # receive the data
        data = data.decode('utf-8')
        # Split the data such that you separate the command
        # from the rest of the data.
        dataMessage = data.split(' ', 1)
        print("Store command received. Time to save a picture")
        storeFile(dataMessage[1])
        reply = "File stored."
        conn.sendall(str.encode(reply))
        print("Data has been sent!")
        break
    conn.close()


while True:
    try:
        conn = setupSocket()
        dataTransfer(conn)
    except:
        break

edit : can someone also let me know if it is better to send an image in chunks like i have or sending an image directly is fine too? or does it make no difference?

question from:https://stackoverflow.com/questions/65832340/send-image-to-android-studio-client-from-python-server-via-socket-programming

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...