raspberry pi 上的 TCP 服务器未连接到 android raspberry pi 上的 tcp 客户端
TCP server on raspberry pi not connecting to android tcp client on raspberry pi
我正在 raspberry pi 上创建一个 TCP 服务器,以便我可以通过 WIFI 从我的 android phone 控制它。我已将 pi 和 phone 连接到我的 WIFI 路由器。
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[0] + ":" + str(address[1]))
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[0]
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[1])
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
使用IP时:
192.168.100.100 :
[Errno 99] Cannot assign requested address
127.162.100.100 或 0.0.0.0:正在创建套接字,但 android 客户端未连接。
在我的 android phone 上,我正在使用一个名为 TCP/UDP 测试工具的应用程序,它是我从 Play 商店下载的。
我是 linux 和 raspberry pi 3 B+ 的新手。
OS: RASPBIAN
编程语言:PYTHON3.5
下载名为 Advanced IP Scanner 的软件。
将所有设备连接到同一个网络,即接入点。
在 raspberry pi 打开终端并输入 ifconfig 获取 ipv4 而不是 0.0.0.0 或 127.0.0.1 另一个。
例如,如果 ip 显示为 192.168.100.144,则在 windows 的高级 IP 扫描器中输入 192.168.100.1-255 开始扫描并找到具有您的 raspberry pi 名称的 ip写在里面。现在在tcp客户端输入这些ips并连接。
我正在 raspberry pi 上创建一个 TCP 服务器,以便我可以通过 WIFI 从我的 android phone 控制它。我已将 pi 和 phone 连接到我的 WIFI 路由器。
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[0] + ":" + str(address[1]))
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[0]
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[1])
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
使用IP时:
192.168.100.100 :
[Errno 99] Cannot assign requested address
127.162.100.100 或 0.0.0.0:正在创建套接字,但 android 客户端未连接。
在我的 android phone 上,我正在使用一个名为 TCP/UDP 测试工具的应用程序,它是我从 Play 商店下载的。
我是 linux 和 raspberry pi 3 B+ 的新手。
OS: RASPBIAN
编程语言:PYTHON3.5
下载名为 Advanced IP Scanner 的软件。
将所有设备连接到同一个网络,即接入点。
在 raspberry pi 打开终端并输入 ifconfig 获取 ipv4 而不是 0.0.0.0 或 127.0.0.1 另一个。
例如,如果 ip 显示为 192.168.100.144,则在 windows 的高级 IP 扫描器中输入 192.168.100.1-255 开始扫描并找到具有您的 raspberry pi 名称的 ip写在里面。现在在tcp客户端输入这些ips并连接。