zoukankan      html  css  js  c++  java
  • day29作业

    写一个基于TCP协议套接字,服务端实现接收客户端的连接并发。

    多进程实现

    服务端:

    from multiprocessing import Process
    import socket
    
    
    def task(conn, addr):
        while True:
            try:
                data = conn.recv(1024)
                if not data:
                    break
                print(addr)
                print(data.decode('utf-8'))
                conn.send(data.upper())
    
            except Exception:
                break
    
    
    if __name__ == '__main__':
        server = socket.socket()
        server.bind(('127.0.0.1', 9999))
        server.listen(5)
        while True:
    
            conn, addr = server.accept()
            print(addr)
            p = Process(target=task, args=(conn, addr))
            p.start()
    
    

    客户端

    
    import socket
    
    client = socket.socket()
    client.connect(('127.0.0.1', 9999))
    
    while True:
        send_msg = input('客户端:')
        if send_msg == 'q':
            break
    
        client.send(send_msg.encode('utf-8'))
    
        data = client.recv(1024)
        print(data.decode('utf-8'))
    

    多线程实现

    服务端:

    import socket
    from threading import Thread
    
    server = socket.socket()
    server.bind(('127.0.0.1', 9999))
    server.listen(5)
    
    
    def task(conn):
        while True:
            data = conn.recv(1024)
            if len(data) == 0:
                continue
    
            print(data.decode('utf-8'))
    
            conn.send(data.upper())
    
    
    if __name__ == '__main__':
        while True:
            conn, addr = server.accept()
            print(addr)
    
            t = Thread(target=task, args=(conn,))
            t.start()
    
    

    客户端:

    import socket
    
    client = socket.socket()
    client.connect(('127.0.0.1', 9999))
    
    while True:
        send_msg = input('客户端:')
        if send_msg == 'q':
            break
    
        client.send(send_msg.encode('utf-8'))
    
        data = client.recv(1024)
        print(data.decode('utf-8'))
    
  • 相关阅读:
    Application和Page详解
    Session解析
    CSS设置技巧
    CSS布局模型
    CSS盒模型
    JAVA -Xms -Xmx -XX:PermSize -XX:MaxPermSize 区别
    设计模式——单例模式
    设计模式——工厂模式
    Go语言学习
    每周一个设计模式
  • 原文地址:https://www.cnblogs.com/setcreed/p/11722122.html
Copyright © 2011-2022 走看看