2 teleger teleger 于 2017.09.11 20:36 提问

我想通过 'change' 按键 来改变不同显示 (多个客户端的发来的数据,至少两个客户端以上) 5C

class MyServer(SocketServer.BaseRequestHandler):
def setup(self):
ip = self.client_address[0].strip()
port = self.client_address[1]
#print(ctime()+', '+ip+":"+str(port)+" is connect!")#打印连接的客户端ip  以及端口

def handle(self):
connect = self.request

    max_row    = Queue(256); 
    min_row    = Queue(256);
    count      = 0

    self.f     = Figure()   
    self.canvas = FigureCanvasTkAgg(self.f,master=root)
    self.canvas.get_tk_widget().grid(row=1,column=0,columnspan=4)
    self.adf        = self.f.add_subplot(111)
    while connect:
        try:
            data  = connect.recv(512) #data type is str
            bdata = [ord(x) for x in data] #bdata is list  
            pdata = np.array(bdata)
            odata = pdata[::2]*256 + pdata[1::2]     
            if bdata == None:
                break        

            max_row.enqueue(max(odata))
            min_row.enqueue(min(odata))
            count = count + 1
            if count == 4:
                self.adf.cla()
                self.adf.plot(max_row.queue,"g-")
                self.adf.plot(min_row.queue,"r-")
                self.adf.set_xlim(0,260)
                self.adf.set_ylim(0,4095)
                self.adf.set_title(self.client_address[0].strip()) 
                self.canvas.show()
                count = 0
            bdata = None
        except socket.error,arg:
            (errno,err_msg)=arg
            print "server failed:%s,errno=%d"%(err_msg,errno) 
    if connect:
         connect.close()

class ThreadedTcpServer(SocketServer.ThreadingMixIn,SocketServer.TCPServer):
 pass

class Watcher():
def init(self):
self.child = os.fork()
if self.child == 0:
return
else:
self.watch()
def watch(self):
try:
os.wait()
except KeyboardInterrupt:
self.kill()
sys.exit()
def kill(self):
try:
os.kill(self.child,signal.SIGKILL)
except OSError:
pass
#新开一个进程监控输入,如果有ctrl+c的信号,则退出子线程
def main():
serverIP = MyServer.inputEntry.get()
if serverIP == '':
return
server = ThreadedTcpServer((serverIP,port),MyServer)
server_thread = threading.Thread(target=server.serve_forever)
server_thread.deamon = True
server_thread.start()

def changeFun():
pass

if name == '__main__':

matplotlib.use('TkAgg')
Watcher()
root = Tk()

Label(root,text='server ip:').grid(row=0,column=0)#标签控件
MyServer.inputEntry = Entry(root)
MyServer.inputEntry.insert(0,'192.168.1.99')#默认服务器ip
MyServer.inputEntry.grid(row=0,column=1)#文本框控件

Button(root,text='start server',command= main).grid(row=0,column=2)
Button(root,text='change',command=changeFun).grid(row=0,column=3)


root.mainloop()#启动事件循环

#问题的问题是,多个客户端连接时,最后连接的 把之前的给 覆盖掉了,只显示最后的那个客户端发来的数据,
#数据成波形动态显示了,子图的标题为 该客户端的IP

Csdn user default icon
上传中...
上传图片
插入图片