从前有个聊天室(socket&threading)
服务器端:
# -*- 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)即可进行聊天室程序:
服务器端:
两个客户端:
从前有个聊天室(socket&threading)的更多相关文章
- [Java小程序]聊天室——Socket和ServerSocket的使用
这段小代码是因为担任Java助教给刚学习Java的本科大二的小学弟小学妹们指导,他们的实验作业就是编写一个Java聊天室客户端和服务器,为了避免出纰漏,自己事先写了一下. 客户端Ui代码: packa ...
- AngularJS+Node.js+socket.io 开发在线聊天室
所有文章搬运自我的个人主页:sheilasun.me 不得不说,上手AngularJS比我想象得难多了,把官网提供的PhoneCat例子看完,又跑到慕课网把大漠穷秋的AngularJS实战系列看了一遍 ...
- node+express+socket.io制作一个聊天室功能
首先是下载包: npm install express npm install socket.io 建立文件: 服务器端代码:server.js var http=require("http ...
- 利用socket.io实现多人聊天室(基于Nodejs)
socket.io简单介绍 在Html5中存在着这种一个新特性.引入了websocket,关于websocket的内部实现原理能够看这篇文章.这篇文章讲述了websocket无到有,依据协议,分析数据 ...
- 基于Node的Web聊天室
1 项目名称 Web聊天室(<这是NodeJs实战>第二章的一个案例,把整个开发过程记录下来)
- 【总结】学习Socket编写的聊天室小程序
1.前言 在学习Socket之前,先来学习点网络相关的知识吧,自己学习过程中的一些总结,Socket是一门很高深的学问,本文只是Socket一些最基础的东西,大神请自觉绕路. 传输协议 TCP:Tra ...
- 与众不同 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 ...
- 使用socket搭建一个网络聊天室
#服务器端import socket import threading #创建一个TCP端 sock = socket.socket(socket.AF_INET, socket.SOCK_STREA ...
随机推荐
- EIGRP认证 配置 (仅仅是命令 原理自己去看书) 转自:http://blog.163.com/s_u/blog/static/13308367201111771831631/
EIGRP认证 目的:掌握EIGRP的MD5认证 拓扑:这里IP配置我就不写出来了,应该对大家来说是非常简单的事了,就要细心一点就可以了.首先我们在R1上启用MD5认证R1(config)#key c ...
- Secure CRT 如何连接虚拟机里面的CentOS系统 当主机使用有线网的时候 作者原创 欢迎转载
1.虚拟机的网卡配置如下图所示: 2.在CentOS 5.8的命令行界面:输入如下指令 然后准备修改里面的网关地址和自己的IP地址 3.同时查看自己的IP地址和网关 4.在第二步里面修改,网关地址应该 ...
- PHP扩展开发(5) - PHP常量的定义和读取
1. 定义 //定义PHP常量REGISTER_STRINGL_CONSTANT("SIMPLE_VERSION", PHP_SIMPLE_VERSION, sizeof(PH ...
- phpcms v9 分页
phpcms的分页很简单,只需在需要分页的地方写入如下代码即可: <div id="pages">{$pages}</div> 连样式都有了,如果你是调用的 ...
- sql 当重复的数据有多条时,保留一条,删除其他重复
delete from proj_info where newcode in (select newcode from proj_info group by newcode hav ...
- postgres安装 以及修改postgres 密码
#postgres安装 apt-get install postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3 postgresql-s ...
- logisticregression
from numpy import * import random import time st = time.time() def loaddata(filename): fr = open(''. ...
- ubuntu14.04 reaver不能正常使用
原帖地址: ubuntu14.04 reaver不能正常使用 http://forum.anywlan.com/thread-282404-1-1.html (出处: http://www.anywl ...
- android使用apktool反编译出现Input file (d:\t) was not found or was not readable
Input file (d:\t) was not found or was not readable 出现这个错误是因为apktool压缩包下载错误,我是下成首页的那个压缩包了 正确下载地址:htt ...
- python的工作记录A
马上进入工作自动化: [root@localhost ~]# cat svn_bbs.py import os,sys,commands,subprocess import re,time svnUr ...