时间:2021-05-22
电脑和树莓派在同一局域网内,先在电脑和树莓派创建python运行环境,然后在树莓派中用python运行rpi.py;在电脑上运行computer.py;电脑上输入字符即可在树莓派上即时显示!
rpi.py
#coding:utf-8#import necessary packageimport socketimport timeimport sysHOST_IP = "192.168.31.151" #树莓派的IP地址HOST_PORT = 8888print("Starting socket: TCP...")#1.create socket object:socket=socket.socket(family,type)socket_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print("TCP server listen @ %s:%d!" %(HOST_IP, HOST_PORT) )host_addr = (HOST_IP, HOST_PORT)#2.bind socket to addr:socket.bind(address)socket_tcp.bind(host_addr)#3.listen connection request:socket.listen(backlog)socket_tcp.listen(1)#4.waite for client:connection,address=socket.accept()socket_con, (client_ip, client_port) = socket_tcp.accept()print("Connection accepted from %s." %client_ip)socket_con.send("Welcome to RPi TCP server!")print("Receiving package...")###主循环while True: try: data=socket_con.recv(512) if len(data)>0: print("Received:%s"%data) socket_con.send(data) time.sleep(1) continue except Exception: socket_tcp.close() sys.exit(1)computer.py
import socketimport timeimport sysSERVER_IP = "192.168.31.151" #树莓派的IP地址SERVER_PORT = 8888print("Starting socket: TCP...")server_addr = (SERVER_IP, SERVER_PORT)socket_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)while True: try: print("Connecting to server @ %s:%d..." %(SERVER_IP, SERVER_PORT)) socket_tcp.connect(server_addr) break except Exception: print("Can't connect to server,try it latter!") time.sleep(1) continueprint("Please input:")while True: try: data = socket_tcp.recv(512) if len(data)>0: print("Received: %s" % data) command=raw_input() socket_tcp.send(command) time.sleep(1) continue except Exception: socket_tcp.close() socket_tcp=None sys.exit(1)以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
两个树莓派,在同一个局域网内,传输文件,采用socket方式。client端代码:importsocketimportosimporthashlibclient
飞鸽传书是面向企业、学校、家庭的局域网即时通讯软件,实现局域网内部消息/文件的高速传输、多媒体远程播放和飞鸽网络打印,软件具有即装即用、传输快捷的特点,深受广大
天应即时通讯,相比较QQ、微信等大众通讯工具,这款以企业内部沟通为基础的即时通讯,不少朋友还是比较陌生,更何况是知道天应即时通讯怎么用?小编就简要介绍下,它
即时通讯(InstantMessaging)是目前Internet上最为流行的通讯方式,各种各样的即时通讯软件也层出不穷。服务提供商也提供了越来越丰富的通讯服务
相比较其他即时通讯软件(如QQ、MSN等),网站访客无需安装任何软件,即可通过网页进行对话。网站在线客服是一种网页版即时通讯软件的统称。它实现和网站的无缝结合,