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()
    趁自己还没死 多折腾折腾
  • 相关阅读:
    HttpServletResponse对象
    FastJSON 简介及其Map/JSON/String 互转(转载)
    commons-lang3-3.4类库
    HttpClient相关
    JSON.parse()方法 (转载)
    textview用法--推荐
    每日总结
    百度网盘
    每日总结
    每日总结
  • 原文地址:https://www.cnblogs.com/lddragon/p/11359251.html
Copyright © 2011-2022 走看看