电脑和树莓派在同一局域网内,先在电脑和树莓派创建python运行环境,然后在树莓派中用python运行rpi.py;在电脑上运行computer.py;电脑上输入字符即可在树莓派上即时显示!

rpi.py

#coding:utf-8
#import necessary package
import socket
import time
import sys


HOST_IP = "192.168.31.151" #树莓派的IP地址
HOST_PORT = 8888
print("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 socket
import time
import sys

SERVER_IP = "192.168.31.151" #树莓派的IP地址
SERVER_PORT = 8888

print("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)
  continue
print("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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?