简体   繁体   中英

Running Python socket script through ADB

I have a somewhat unusual problem. I want to run some code on my android smartphone via the ADB (which along with SL4A is already set up) and then I want it to act as a server and respond with the program's results while my host machine acts as a client and receives this data. However, I am not an expert in socket operations and I'm getting results I don't understand. My code is as follows:

from device import android
import socket

droid = android.Android()

# Open socket
s = socket.socket()
HOST = socket.gethostname()
PORT = 8000
s.bind((HOST, PORT))
s.listen(5)

print "HOST = ", HOST, ", PORT = ", PORT

# Accept instructions
c, addr = s.accept()
print "Got connection from", addr

data = ""
while True:
    chunk = c.recv(1024)
    if (not chunk):
        break
    else:
        data.extend(chunk)
s.close()

# DO STUFF WITH RECEIVED INPUT

# Send result
c, addr = s.accept()
print "Got connection from", addr
while True:
    if (c.send(str(result)):
        break
s.close()

The code I'm running on the client, my host machine, is as follows:

s = socket.socket()
HOST = "localhost"
PORT = 8000
s.connect((HOST, PORT))
s.send(data)
s.close()

# IN A DIFFERENT FUNCTION

s = socket.socket()
HOST = "localhost"
PORT = 8000
try:
    s.connect((HOST, PORT))
    results = s.recv(1024)
    s.close()
    return results
except Exception:
    return False

The environment has been set up this way:

set AP_PORT=54023
adb forward tcp:%AP_PORT% tcp:%AP_PORT%

So, how this is supposed to work is this: the server is set up to run on my Android smartphone and accept input to run the program on my Android smartphone from another program running on my PC. After the Android program has finished running, it should send the result back to my PC.

However, I'm having several problems. I can't seem to get the first connection open. I either get 10061's or 10049's and I'm not sure why that is. I've looked at the existing documentation available and I'm still not getting results. I'm also not sure if this code is actually running on the Android device and this is very important to my application so I really need some sort of confirmation on this.

Any enlightenment would be greatly appreciated.

The server program never ends until it reaches an exception, because you set up an inf loop: while True .
c.close() close the current connection, then the loop starts again and the program waits for another connection to accept.

The host program instead should end after the message is sent or when it reaches an exception.

What do you mean with: " I'm getting results I don't understand... This code runs without error... The problem I'm having is that I'm not getting any results back" ?

What results do you get? Have you checked the data var?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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