python socket 多人聊天室
Python 的 Socket 编程教程 http://www.oschina.net/question/12_76126
Python线程指南 http://www.open-open.com/lib/view/open1345476194313.html
具体思路:
每个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

python socket 多人聊天室的更多相关文章
- Python实现网络多人聊天室
网络多人聊天室 文件结构: chatroom ├── client.py # 客户端代码 ├── language.py # 语言文件 ├── server.py # 服务端代码 └── set ...
- Python实现网络图形化界面多人聊天室 - Windows
Python实现网络图形化界面多人聊天室 - Windows 项目名称:网络多人聊天室图形界面版本 项目思路: server.py 服务端文件,主进程中,创建图形化界面,询问地址(主机名,端口),点击 ...
- Python实现网络图形化界面多人聊天室 - Linux
网络图形化界面多人聊天室 - Linux Windows版本:https://www.cnblogs.com/noonjuan/p/12078524.html 在Python实现网络多人聊天室基础上, ...
- 与众不同 windows phone (31) - Communication(通信)之基于 Socket UDP 开发一个多人聊天室
原文:与众不同 windows phone (31) - Communication(通信)之基于 Socket UDP 开发一个多人聊天室 [索引页][源码下载] 与众不同 windows phon ...
- 与众不同 windows phone (30) - Communication(通信)之基于 Socket TCP 开发一个多人聊天室
原文:与众不同 windows phone (30) - Communication(通信)之基于 Socket TCP 开发一个多人聊天室 [索引页][源码下载] 与众不同 windows phon ...
- java socket之多人聊天室Demo
一.功能介绍 该功能实现了一个类似QQ的最简单多人聊天室,如下图所示. 二.目录结构 三.服务端 1)SocketServer类,该类是服务端的主类,主要负责创建聊天窗口,创建监听客户端的线程: pa ...
- Python实现网络多人聊天室 - Windows
项目名称:多人聊天室项目结构: client.py server.py settings.py项目思路:服务端接收客户端连接,客户端发送信息给服务端,服务端将信息发送给所有客户端.项目实现:主进程负责 ...
- Apache MiNa 实现多人聊天室
Apache MiNa 实现多人聊天室 开发环境: System:Windows JavaSDK:1.6 IDE:eclipse.MyEclipse 6.6 开发依赖库: Jdk1.4+.mina-c ...
- 多人聊天室(Java)
第1部分 TCP和UDP TCP:是一种可靠地传输协议,是把消息按一个个小包传递并确认消息接收成功和正确才发送下一个包,速度相对于UDP慢,但是信息准确安全:常用于一般不要求速度和需要准确发送消息的场 ...
随机推荐
- XSD-学习总结
1.代码详细分析 <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/X ...
- Git的状态转换
近期公司用Git来管理代码,用起来是要比svn爽一些.就是刚接触的时候比較痛苦,特别是那些状态(版本号的提交/合并/回退).差点把我搞晕了. 如今回过头来总结一下,就清楚多了. ...
- ubuntu16.04彻底卸载mysql并且重新安装mysql
首先删除mysql: sudo apt-get remove mysql-* dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P 清理完毕: ...
- JOptionPane的经常使用4种对话框
JOptionPane类有4个用于显示对话框的静态方法: 消息.选项.确认,输入对话框 showMessageDialog://显示一条消息并等待用户OK showConfirmDialog://显示 ...
- 更改Mysql数据库存储位置
默认安装位置 C:\Program Files\MySQL\MySQL Server 5.7 一.首先把mysql的服务先停掉. 二.更改MySQL配置文件My.ini中的数据库存储主路径 打开MyS ...
- java实现快速排序算法
1.算法概念. 快速排序(Quicksort)是对冒泡排序的一种改进.由C. A. R. Hoare在1962年提出.2.算法思想. 通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据 ...
- stylus
stylus介绍 是个什么鬼?对于开发来说,CSS的弱点在于静态化.我们需要一个真正能提高开发效率的工具, LESS , SASS 都在这方面做了一些贡献. Stylus 是一个CSS的预处理框架,2 ...
- angular 禁止事件冒泡 和 默认行为
事件冒泡和事件捕捉一直以来都是被讨论的话题,也许大家平时在工作中没有遇到过需要解决事件冒泡的情况举个例子: <body ng-click="fun1()"> <d ...
- windwos at 以及shutdown使用方法
at 12:00 shutdown -r如果今天12点没过去,今天12点重启,如果今天12点已经过去,明天12点重启.每天12点重启的命令是at 12:00 /every:Monday,Tuesday ...
- Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别
utf8_unicode_ci和utf8_general_ci对中.英文来说没有实质的差别.utf8_general_ci 校对速度快,但准确度稍差.utf8_unicode_ci 准确度高,但校对速 ...