客户端单向控制服务器

服务器

# -*- coding:utf-8 -*-
import socket
import os
import time

host = '192.168.1.105'
port = 12345

def main():
while True:
ret = str(conn.recv(1024), encoding="utf-8")
if ('End' in ret) or (ret==""):
print('客户端断开链接')
conn.sendall(bytes("Finish!", encoding="utf-8"))
conn.close()
os.system('netstat -ano | findstr %s' % port)
break
else:
print('服务器: runing...')
if ret == "1":
print ("执行指令:up\n")
cmd = "start t.txt"
os.system(cmd)
elif ret == "2":
print ("执行指令:down\n")
elif ret == "3":
print ("执行指令:left\n")
elif ret == "4":
print ("执行指令:right\n")
elif ret == "5":
print ("执行指令:front\n")
elif ret == "6":
print ("执行指令:behind\n")
elif ret == "7":
print ("执行指令:start\n")
elif ret == "8":
print ("执行指令:stop\n")
else:
print ("未知指令,跳过\n")
break

if __name__=='__main__':
sk = socket.socket()
sk.bind((host, port))
sk.listen(5)
print('The port is listening...')
os.system('netstat -ano | findstr %s' % port)
print('Wait for the client.')
conn, address = sk.accept()
print('Connecting...')
print('Connect from: ', address)
os.system('netstat -ano | findstr %s' % port)
main()

tcp/ip 实现局域网内设备通信_双向通信

客户端

# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Rectangle, Color
from kivy.config import Config
Config.set('graphics', 'width', '360')
Config.set('graphics', 'height', '540')
import socket
import os
import subprocess

host='192.168.1.105'
port=12345

class FloatLayoutApp(App):
def build(self):
def update_rect(layout, *args):
layout.rect.pos = layout.pos
layout.rect.size = layout.size

float_layout = FloatLayout()
button1 = Button(text='1.up', size_hint=(.2, .1), pos=(70,400))
button2 = Button(text='2.down', size_hint=(.2, .1), pos=(215, 400))
button3 = Button(text='3.left', size_hint=(.2, .1), pos=(70, 320))
button4 = Button(text='4.right', size_hint=(.2, .1), pos=(215, 320))
button5 = Button(text='5.front', size_hint=(.2, .1), pos=(70,240))
button6 = Button(text='6.behind', size_hint=(.2, .1), pos=(215, 240))
button7 = Button(text='7.start', size_hint=(.2, .1), pos=(70, 160))
button8 = Button(text='8.stop', size_hint=(.2, .1), pos=(215, 160))
button9 = Button(text='9.con kill', size_hint=(.2, .1), pos=(70,80))
self.button10 = Button(text='10.exit', size_hint=(.2, .1), pos=(215, 80))
float_layout.add_widget(button1)
float_layout.add_widget(button2)
float_layout.add_widget(button3)
float_layout.add_widget(button4)
float_layout.add_widget(button5)
float_layout.add_widget(button6)
float_layout.add_widget(button7)
float_layout.add_widget(button8)
float_layout.add_widget(button9)
float_layout.add_widget(self.button10)

button1.bind(on_press=self.button1_func)
button2.bind(on_press=self.button2_func)
button3.bind(on_press=self.button3_func)
button4.bind(on_press=self.button4_func)
button5.bind(on_press=self.button5_func)
button6.bind(on_press=self.button6_func)
button7.bind(on_press=self.button7_func)
button8.bind(on_press=self.button8_func)
button9.bind(on_press=self.button9_func)
self.button10.bind(on_press=self.button10_func)
self.set_connect()
return float_layout
def set_connect(self):
try:
self.obj=socket.socket()
self.obj.connect((host,port))
cmd=('netstat -ano | findstr %s'%port)
result_out=subprocess.getoutput(cmd)
print(result_out)
except Exception as e:
print ("please open servse\n")
def button1_func(self, event):
print("1.up")
print("客户端runing...")
self.obj.send(bytes("1",encoding="utf-8"))
def button2_func(self, event):
print("2.down")
print("客户端runing...")
self.obj.send(bytes("2",encoding="utf-8"))
def button3_func(self, event):
print("3.left")
print("客户端runing...")
self.obj.send(bytes("3",encoding="utf-8"))
def button4_func(self, event):
print("4.right")
print("客户端runing...")
self.obj.send(bytes("4",encoding="utf-8"))
def button5_func(self, event):
print("5.front")
print("客户端runing...")
self.obj.send(bytes("5",encoding="utf-8"))
def button6_func(self, event):
print("6.behind")
print("客户端runing...")
self.obj.send(bytes("6",encoding="utf-8"))
def button7_func(self, event):
print("7.start")
print("客户端runing...")
self.obj.send(bytes("7",encoding="utf-8"))
def button8_func(self, event):
print("8.stop")
print("客户端runing...")
self.obj.send(bytes("8",encoding="utf-8"))
def button9_func(self, event):
print("9.con kill")
print("客户端runing...")
self.obj.send(bytes("9",encoding="utf-8"))
def button10_func(self, event):
print("10.exit")
self.stop()

if __name__ == "__main__":
FloatLayoutApp().run()

tcp/ip 实现局域网内设备通信_双向通信_02


说明:1-10,能够控制服务器完成指令,可应用于机器人。

客户端服务器双向通信

服务器

# -*- coding:utf-8 -*-
import socket
import os
import time

host = '192.168.1.105'
port = 12345

def main():
while True:
ret = str(conn.recv(1024), encoding="utf-8")
if 'End'==ret:
print('End')
conn.sendall(bytes("Finish!", encoding="utf-8"))
conn.close()
os.system('netstat -ano | findstr %s' % port)
break
else:
print("收到客户端消息:",ret)
meg = input("请输入发送的消息:\n")
conn.sendall(bytes(meg, encoding="utf-8"))
continue

if __name__=='__main__':
while True:
sk = socket.socket()
sk.bind((host, port))
sk.listen(5)
#sk.settimeout(10)
print('The port is listening...')
os.system('netstat -ano | findstr %s' % port)
print('Wait for the client.')
conn, address = sk.accept()
print('Connecting...')
print('Connect from: ', address)
os.system('netstat -ano | findstr %s' % port)
main()

tcp/ip 实现局域网内设备通信_双向通信_03

客户端

# -*- coding:utf-8 -*-
import socket
import os
import subprocess

host = '192.168.1.105'
port = 12345

def set_connect():
obj=socket.socket()
obj.connect((host,port))
cmd='netstat -ano | findstr %s'%port
result_out = subprocess.getoutput(cmd)
print(result_out)
while True:
meg = str(input("请输入发送的消息:\n"))
obj.send(bytes(meg,encoding="utf-8"))

ret=str(obj.recv(1024),encoding="utf-8")
if ret=='Finish!':
obj.close()
else:
print("收到服务器消息:",ret)

result=subprocess.getoutput(cmd)
print(result)

def main():
set_connect()

if __name__=='__main__':
main()

tcp/ip 实现局域网内设备通信_客户端_04