zoukankan      html  css  js  c++  java
  • tcp单线程实现并发

    服务端

    from gevent import monkey;monkey.patch_all()
    import socket
    from gevent import spawn
    
    
    server = socket.socket()
    server.bind(('127.0.0.1',8080))
    server.listen(5)
    
    
    def talk(conn):
        while True:
            try:
                data = conn.recv(1024)
                if len(data) == 0:break
                print(data.decode('utf-8'))
                conn.send(data.upper())
            except ConnectionResetError as e:
                print(e)
                break
        conn.close()
    
    def server1():
        while True:
            conn, addr = server.accept()
            spawn(talk,conn)
    
    if __name__ == '__main__':
        g1 = spawn(server1)
        g1.join()

    客户端

    import socket
    from threading import Thread,current_thread
    
    
    def client():
        client = socket.socket()
        client.connect(('127.0.0.1',8080))
        n = 0
        while True:
    
            data = '%s %s'%(current_thread().name,n)
            client.send(data.encode('utf-8'))
            res = client.recv(1024)
            print(res.decode('utf-8'))
            n += 1
    
    for i in range(400):
        t = Thread(target=client)
        t.start()
    趁自己还没死 多折腾折腾
  • 相关阅读:
    电机调速作业
    迟到的大作业模块分析
    PLC梯形图设计
    电机随笔
    好难
    电机随笔(三)
    电机随笔(二)
    电机随笔 (一)
    机电传动控制直流调速作业
    机电传动控制大作业第一阶段
  • 原文地址:https://www.cnblogs.com/lddragon/p/11359251.html
Copyright © 2011-2022 走看看