somepeople

python网络编程简单cs架构代码

server端:

import socket
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
server.bind(('127.0.0.1',8080))
server.listen(5)
while 1:
    conn,client_addr = server.accept()
    print(client_addr)
    while 1:
        try:
            data = conn.recv(1024)
            if not data:
                continue
            print(data)
            conn.send(data.upper())
        except ConnectionResetError:
            break
    conn.close()
server.close()

client:

import socket
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(('127.0.0.1',8080))
while 1:
    msg = input('content:').strip()
    if not msg:
        continue
    client.send(msg.encode('utf-8'))
    data = client.recv(1024)
    print(data.decode('utf-8'))
client.close()

 

posted on 2020-10-07 17:16  Hello_Thanos  阅读(690)  评论(0编辑  收藏  举报

导航