博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python socket 多人聊天室
阅读量:6247 次
发布时间:2019-06-22

本文共 3521 字,大约阅读时间需要 11 分钟。

参考来源(其实我从上面复制了一点):
Python 的 Socket 编程教程  http://www.oschina.net/question/12_76126
Python线程指南 http://www.open-open.com/lib/view/open1345476194313.html
Python Socket文档 https://docs.python.org/3/library/socket.html#socket-objects
具体思路:
每个client有两个线程,分别负责接收和发送,当没有发送时,在raw_input()那卡住,当没有接收时,在recv()那卡住
server为每个client开两个线程,分别处理接收和发送。每个发送的线程在con.wait()那阻塞,等待notify。每个接收的线程,在recv()那里等待来自client的输入,接收到输入后,发出一个notify,激活所有输出线程,自身则因为循环在下一个recv()那里等待。
这样做的优势时在等待期间,cpu都处于空闲状态,cpu只在接收和发送的瞬间被使用。
目前没有想到更好方法。。。
注意:
我做得有点粗糙,复制后有些注释和代码对应不起来,也没有添加新的注释
 
标签: <无>
 

代码片段(4)

1. [文件] client.py ~ 976B     (283)     

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 24 17:35:50 2013
 
@author: zbg
"""
 
import
socket
import
threading
 
 
inString
=
''
outString
=
''
nick
=
''
 
def
DealOut(s):
    
global
nick, outString
    
while
True
:
        
outString
=
raw_input
()
        
outString
=
nick
+
': '
+
outString
        
s.send(outString)
 
def
DealIn(s):
    
global
inString
    
while
True
:
        
try
:
            
inString
=
s.recv(
1024
)
            
if
not
inString:
                
break
            
if
outString !
=
inString:
                
print
inString
        
except
:
            
break
         
 
nick
=
raw_input
(
"input your nickname: "
)
ip
=
raw_input
(
"input the server's ip adrress: "
)
sock
=
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip,
8888
))
sock.send(nick)
 
thin
=
threading.Thread(target
=
DealIn, args
=
(sock,))
thin.start()
thout
=
threading.Thread(target
=
DealOut, args
=
(sock,))
thout.start()
 
#sock.close()

2. [文件] server.py ~ 2KB     (285)     

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 25 10:33:44 2013
 
@author: zbg
"""
import
socket
import
sys
import
threading
 
con
=
threading.Condition()
HOST
=
raw_input
(
"input the server's ip adrress: "
)
# Symbolic name meaning all available interfaces
PORT
=
8888
# Arbitrary non-privileged port
data
=
''
 
s
=
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print
'Socket created'
s.bind((HOST, PORT))
s.listen(
10
)
print
'Socket now listening'
 
#Function for handling connections. This will be used to create threads
def
clientThreadIn(conn, nick):
    
global
data
#infinite loop so that function do not terminate and thread do not end.
    
while
True
:
    
#Receiving from client
        
try
:
            
temp
=
conn.recv(
1024
)
            
if
not
temp:
                
conn.close()
                
return
            
NotifyAll(temp)
            
print
data
        
except
:
            
NotifyAll(nick
+
" leaves the room!"
)
            
print
data
            
return
 
    
#came out of loop
 
def
NotifyAll(sss):
    
global
data
    
if
con.acquire():
        
data
=
sss
        
con.notifyAll()
        
con.release()
  
def
ClientThreadOut(conn, nick):
    
global
data
    
while
True
:
        
if
con.acquire():
            
con.wait()
            
if
data:
                
try
:
                    
conn.send(data)
                    
con.release()
                
except
:
                    
con.release()
                    
return
                     
 
while
1
:
    
#wait to accept a connection - blocking call
    
conn, addr
=
s.accept()
    
print
'Connected with '
+
addr[
0
]
+
':'
+
str
(addr[
1
])
    
nick
=
conn.recv(
1024
)
     
#send only takes string
    
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
    
NotifyAll(
'Welcome '
+
nick
+
' to the room!'
)
    
print
data
    
print
str
((threading.activeCount()
+
1
)
/
2
)
+
' person(s)!'
    
conn.send(data)
    
threading.Thread(target
=
clientThreadIn , args
=
(conn, nick)).start()
    
threading.Thread(target
=
ClientThreadOut , args
=
(conn, nick)).start()
 
s.close()

3. [图片] QQ截图20131027115134.png    

4. [图片] QQ截图20131027115232.png    

转载地址:http://siria.baihongyu.com/

你可能感兴趣的文章
【Oracle Database】数据库日志管理
查看>>
在CentOS7上安装MongDB【4.0.0版本】
查看>>
Juniper SRX 240 DY×××用户登录日志
查看>>
入门一班 201801012 管道符
查看>>
Cs6/7笔记01.5、安装Centos6、7
查看>>
开源工具Arena,数据科学家再也不用为Kubernetes犯难啦!
查看>>
JavaScript初学者必看“箭头函数”
查看>>
ffmpeg 使用小记
查看>>
变频电源六大故障及处理方法
查看>>
HBase核心知识点总结
查看>>
朋友圈唯美的心灵鸡汤语录,句句经典入心
查看>>
怎么还原回收站删除的文件?这个操作最实用
查看>>
相机SD卡数据恢复,必知小技巧
查看>>
如何发送和接收 Windows Phone 的磁贴通知
查看>>
配置pxe自动化安装centos系统
查看>>
C语言之为数据类型定义别名
查看>>
HTML DOM 之 DOM对象:Document Object Model (文档对象模型)
查看>>
【网络编程】大端模式和小端模式(大头序和小头序)
查看>>
Angular之Rxjs基础操作
查看>>
U3D笔记(三)
查看>>