I am creating a TCP server on raspberry pi so that i can control it from my android phone over WIFI. I have connected both the pi and the phone to my WIFI router.
import socket from cookieLED_FINAL import callLED host = '192.168.100.100' port = 5560 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 complete.") return s def setupConnection(): s.listen(1) # Allows one connection at a time. conn, address = s.accept() print("Connected to: " + address + ":" + str(address)) return conn 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) command = dataMessage if command == 'GET': reply = GET() elif command == 'REPEAT': reply = REPEAT(dataMessage) elif command == 'STORE': print("Store command received. Time to save a picture") storeFile(dataMessage) print("FINISHED STORING FILE") break elif command == 'LED_ON': callLED() reply = 'LED was on' elif command == 'EXIT': print("Our client has left us :(") break elif command == 'KILL': print("Our server is shutting down.") s.close() break else: reply = 'Unknown Command' # Send the reply back to the client conn.sendall(str.encode(reply)) print("Data has been sent!") conn.close() s = setupServer() while True: try: conn = setupConnection() dataTransfer(conn) except: break
When using IP:
[Errno 99] Cannot assign requested address
127.162.100.100 or 0.0.0.0: The socket is being created but android client is not getting connected.
On my android phone i am using an app named TCP/UDP Test Tool which I downloaded from play store.
I am new on linux as well as on raspberry pi 3 B+.
PROGRAMMING LANGUAGE: PYTHON 3.5