코드를 사용하여 다음을 달성하십시오.
# Server from socket import * from threading import Thread # 클라이언트 정보 및 연결 상태 수신 def recv_data(client_socket,client_info): while True: recv_data = client_socket.recv(1024) recv_content = recv_data.decode('gbk') print(f ' The client said: {recv_content}, from: {client_info}') if recv_content == 'end': print('End message') break # 서버가 클라이언트에게 정보를 보냅니다 def send_data(): while True: msg = input ('>') client_socket.send(msg.encode('gbk')) if msg == 'end': print('메시지 끝') break if __name__ == '__main__': # 소켓 만들기 server_socket = socket(AF_INET,SOCK_STREAM) # 머신의 IP로 변경, 8888은 사용된 인터페이스 입니다 . connect server_socket.listen(5) print('연결 대기 중!') while True: client_socket, client_info = server_socket.accept() print("클라이언트가 성공적으로 연결을 설정했습니다!") t = Thread(target=recv_data, args= (client_socket, client_info)) # 클라이언트로부터 받은 정보를 처리할 스레드 생성 t1 = Thread(target=send_data) # 서버와 정보를 주고받을 스레드 생성 t.start() # 스레드 시작 t1.start ()
# Client from socket import * from threading import Thread def recv_data(): while True: # 서버측 데이터 수신 recv_data = client_socket.recv(1024) recv_content = recv_data.decode('gbk') print(f'The server said: {recv_content}') if recv_content == 'end': print('End message') break # 서버에 정보를 보낼 수 있습니다 def send_data(): while True: msg =input('>') client_socket.send(msg .encode ('gbk')) if msg == 'end': print('message end') break if __name__ == '__main__': # 소켓 생성 client_socket = socket(AF_INET,SOCK_STREAM) # 머신의 IP로 변경, 8888은 사용된 인터페이스 client_socket.connect(('192.168.10.124',8888)) t1 = Thread(target=recv_data) t2 = Thread(target=send_data) t1.start() t2 .start() t1.join() t2.join() client_socket.close()