我正在尝试将网络摄像头捕获的图像从pi发送到pc,但是在使用套接字编程的同时,我得到了大量的延迟(每个图像大约5-6秒)。我在这里做错什么了吗?有没有办法减少这种延迟,或者有其他更好的技术以最小的延迟将数据发送到pc?
服务器端:
import socket
port = 60000
s = socket.socket()
host = ""
s.bind((host, port))
s.listen(5)
print('Server listening....')
while True:
conn, addr = s.accept()
print 'Got connection from', addr
data = conn.recv(1024)
print('Server received', repr(data))
filename='mytext.txt'
f = open(filename,'rb')
l = f.read(1024)
while (l):
conn.send(l)
print('Sent ',repr(l))
l = f.read(1024)
f.close()
print('Done sending')
conn.send('Thank you for connecting')
conn.close()
客户:
import socket # Import socket module
import time
s = socket.socket() # Create a socket object
host = "192.1.1.1" #Ip address that the TCPServer is there
port = 60000 # Reserve a port for your service.
s.connect((host, port))
s.send("Hello server!")
with open('received_file.png', 'wb') as f:
print 'file opened'
start = time.time()
while True:
# print('receiving data...')
data = s.recv(1024)
# print('data=%s', (data))
if not data:
break
# write data to a file
f.write(data)
print(time.time() - start)
f.close()
print('Successfully get the file')
s.close()
print('connection closed')