1from socket import *
2from threading import
thread
3 sockets =
4def
main():5#
建立server_socket套接字物件
6 server_socket =socket(af_inet,sock_stream)7#
繫結埠
8 server_socket.bind(('',8889))9#
監聽10
server_socket.listen()11#
接收客戶端的請求
12while
true:
13 client_socket,client_info =server_socket.accept()
1415
#開啟執行緒處理當前客戶端的請求
16 t = thread(target=readmsg,args=(client_socket,))
17t.start()
18def
readmsg(client_socket):19#
讀取客戶端傳送過來的訊息
20while
true:
21 recv_data = client_socket.recv(1024)22#
23#24for socket in
sockets:
25socket.send(recv_data)
26if
__name__ == '
__main__':
27main()
1from sock import *
2from threading import
thread
3def
readmsg(client_socket):
4while
true:
5 recv_data = client_socket.recv(1024)
6print('
收到:',recv_data.decode('
utf-8'))
7def
writemsg(client_socket):
8while
true:
9 msg = input('
>')
10 client_socket.sent(msg.encode('
utf-8'))
11#建立客戶端套接字物件
12 client_socket =socket(af_inet,sock_stream)13#
呼叫connect()連線伺服器
14 client_socket.connect(('
192.168.0.107
',8888))15#
開啟乙個執行緒處理客戶端的讀取訊息
16 t = thread(target=readmsg,args=(client_socket,))
17t.start()18#
開啟乙個執行緒處理客戶端的傳送訊息
19 t = thread(target=writemsg,args=(client_socket,))
20 t.start()
tcp 多執行緒聊天器
from socket import import threading from time import sleep def main sever 建立套接字 tcp socket socket af inet,sock stream 繫結本地資訊 tcp socket.bind 8081 監聽 t...
Linux下c語言TCP多執行緒聊天室
開發環境 linux ubuntu 12.04 gcc 編譯 gcc server.c lpthread std gnu99 o s gcc client.c lpthread std gnu99 o c 執行服務端 執行2個客戶端 其中乙個客戶端傳送訊息 互相傳送 整體效果圖 客戶端傳送 quit...
Python筆記 多執行緒聊天
import socket import threading import sys s socket.socket socket.af inet,socket.sock stream s.bind 169.254.50.193 8080 file open chat.txt w encoding u...