服务器端:

 # -*- coding: utf-8 -*-
import socket, 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() #notifyAll effect there
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()

chatserver.py

客户端:

 # -*- coding: utf-8 -*-
import socket, 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()

chatclient

我把服务器端代码放到我在阿里云买的服务器上运行后在本地运行客户端程序(ip输入服务器ip)即可进行聊天室程序:

服务器端:

两个客户端:

最新文章

  1. KVM安装部署
  2. LDAP抛出Error Code 3 - Timelimit Exceeded 异常,导致CAS连接报错
  3. JS图片懒加载
  4. Android Studio导入项目非常慢的解决办法
  5. Autodesk招聘开发咨询顾问(北京或上海),需要内推的扔简历过来啊
  6. android-eclipse-phonegap 2..9以下(包含2.9)的项目配置
  7. c# 操作excel 替代方案
  8. python实现简易数据库之二——单表查询和top N实现
  9. 构建maven项目3
  10. Day1 summary
  11. android 输出.txt 文本换行问题
  12. URAL1513. Lemon Tale(dp)
  13. (转载)提高mysql插入数据的速度
  14. 教你怎样在ppt2010抠图的小技巧|用ppt2010抠图的方法
  15. [SQL注入3]from_sqli_to_shell_II
  16. Golang网络库中socket阻塞调度源码剖析
  17. Python实例----------每日一贴
  18. 第1章 背景 - Identity Server 4 中文文档(v1.0.0)
  19. Centos 7 telnet 详解
  20. C语言 进制转换

热门文章

  1. 带左右箭头切换的自动滚动图片JS特效
  2. github 使用方法总结 还有一部分不太懂
  3. Java在ACM中的使用
  4. Linux_shell条件判断if中的-a到-z的意思
  5. 转:堆(heap)和栈(stack)有什么区别??
  6. Activity的四种状态
  7. css设置水平垂直居中
  8. Calling a Web API From a .NET Client (C#)
  9. json转换为键值对辅助类
  10. 如何新建XCode项目