from vcScript import *
import socket
comp = getComponent()
IP = '127.0.0.1'
PROT = 6002
SIZE = 1024
def OnRun():
listensocekt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listensocekt.settimeout(300)
listensocekt.setblocking(False)
listensocekt.bind((IP,PROT))
listensocekt.listen(1)
# 开始循环,等待客户端连接,接受消息和反馈
buffer = bytearray()
bufferTemp = bytearray()
while True:
while True:
try:
datasocket, addr = listensocekt.accept()
except:
delay(0.1)
continue
else:
print("连接的客户端: %s" % str(addr))
datasocket.setblocking(False)
while True:
try:
bufferTemp = datasocket.recv(SIZE)
except:
delay(0.1)
continue
else:
buffer += bufferTemp
if len(bufferTemp) < SIZE:
feedBack = Handler(buffer.decode('utf-8'))
try:
datasocket.send(feedBack.encode('utf-8'))
except:
print buffer
if not bufferTemp:
break
else:
continue
def Handler(cmd):
print(cmd)
if cmd == "":
return None
datasocket.close()
listensocekt.close()