Socket初步實現雙工通訊
阿新 • • 發佈:2021-01-30
同一份程式碼稍微改改就能實現伺服器和客戶端同步執行
單位一:
import socket import threading from queue import Queue # 傳送訊息 def server_send_data(s_new): while True: # 準備傳送資料 send_data = str(input()) s_new.send(send_data.encode('utf-8')) # 接收訊息 def server_recv_data(s_new, addr): while True: recv_data1 = s_new.recv(1024) if recv_data1: print('對方表示:[{}]'.format(recv_data1.decode('utf-8'))) def client_send_data(s): while True: # 傳送資料 send_data = str(input('')) if send_data == 'exit': break s.send(send_data.encode('utf-8')) def client_recv_data(s): while True: # 接收資料 recv_data = s.recv(1024) if recv_data: print('對方表示:[{}]'.format(recv_data.decode('utf-8'))) # 服務端實現-------------- def server_client(): # 建立服務端套接字 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 服務端繫結埠 server.bind(('127.0.0.1', 5054)) # 服務端變為被動監聽模式 server.listen(4) print("等待連線") # 等待客戶端連結 s_new, addr = server.accept() print('已經和【{}】已經成功連線!!'.format(addr[0])) try: # 5.服務端接受訊息 t1 = threading.Thread(target=server_recv_data, args=(s_new, addr)) # 6.服務端傳送訊息 t2 = threading.Thread(target=server_send_data, args=(s_new,)) t1.start() t2.start() except Exception as rest: print(rest) # 客戶端實現-------------- def client_server(): # 建立客戶端套接字 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 客戶端繫結埠 client.connect(('127.0.0.1', 5053)) # 客戶端實現-------------- # 5.關閉套接字 print("連線成功,可以通訊") try: t1 = threading.Thread(target=client_send_data, args=(client,)) t2 = threading.Thread(target=client_recv_data, args=(client,)) t1.start() t2.start() except Exception as rest: print(rest) t3 = threading.Thread(target=client_server) t4 = threading.Thread(target=server_client) t3.start() t4.start()
單位2
import socket import threading from queue import Queue # 傳送訊息 def server_send_data(s_new): while True: # 準備傳送資料 send_data = str(input()) s_new.send(send_data.encode('utf-8')) # 接收訊息 def server_recv_data(s_new, addr): while True: recv_data1 = s_new.recv(1024) if recv_data1: print('對方表示:[{}]'.format(recv_data1.decode('utf-8'))) def client_send_data(s): while True: # 傳送資料 send_data = str(input('')) if send_data == 'exit': break s.send(send_data.encode('utf-8')) def client_recv_data(s): while True: # 接收資料 recv_data = s.recv(1024) if recv_data: print('對方表示:[{}]'.format(recv_data.decode('utf-8'))) # 服務端實現-------------- def server_client(): # 建立服務端套接字 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 服務端繫結埠 server.bind(('127.0.0.1', 5053)) # 服務端變為被動監聽模式 server.listen(4) print("等待連線") # 等待客戶端連結 s_new, addr = server.accept() print('已經和【{}】已經成功連線!!'.format(addr[0])) try: # 5.服務端接受訊息 t1 = threading.Thread(target=server_recv_data, args=(s_new, addr)) # 6.服務端傳送訊息 t2 = threading.Thread(target=server_send_data, args=(s_new,)) t1.start() t2.start() except Exception as rest: print('對方已經退出!') # 客戶端實現-------------- def client_server(): # 建立客戶端套接字 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 客戶端繫結埠 client.connect(('127.0.0.1', 5054)) # 客戶端實現-------------- # 5.關閉套接字 try: print("連線成功,可以通訊") t1 = threading.Thread(target=client_send_data, args=(client,)) t2 = threading.Thread(target=client_recv_data, args=(client,)) t1.start() t2.start() except Exception as rest: print(rest) t3 = threading.Thread(target=client_server) t4 = threading.Thread(target=server_client) t3.start() t4.start()
效果:有小bug,但是正常執行沒問題。
先開的一方:
後開的一方: