0

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.

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, that's the problem. The data var is empty after I receive so I'm not sure what's going on
Ok, and c.send() what value returns? Are you using Python 3?
As far as I've been able to tell, no value has returned. I'm using Python 2.7.6 on my host machine and 2.6.2 on android (that corresponds to Python 3).
OK, thanks I really appreciate your answer. I attempted it but I'm still encountering problems.
Well, I don't think send() method can return nothing. From the Official Documentation: "Send data to the socket. The socket must be connected to a remote socket. The optional flags argument has the same meaning as for recv() above. Returns the number of bytes sent. Applications are responsible for checking that all data has been sent; if only some of the data was transmitted, the application needs to attempt delivery of the remaining data.". So, send() doesn't return the number of bytes sent and it doesn't raise exceptions, like socket.timeout either?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.