Python socket实现简单聊天室

服务端使用了select模块,实现了对多个socket的监控。客户端由于select在Windows下只能对socket使用,所以使用了多线程来实现对客户端输入和socket连接的同时监控。注意这里的socket设置为了非阻塞。这样就实现了在一个线程中同时进行socket的接收和发送。

服务器代码

# -*- coding: utf-8 -*-
import socket,select

connection_list = []
host = ''
port = 10001

def board_cast(sock,message):
    for socket in connection_list:
        if socket != server_sock and socket != sock:
            try:
                socket.send(message)
            except:
                socket.close()
                print str(socket.getpeername())+' is offline'
                connection_list.remove(socket)

server_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
server_sock.setblocking(0)
server_sock.bind((host,port))
server_sock.listen(10)
connection_list.append(server_sock)

while 1:
    readable,writable,error = select.select(connection_list,[],[])
    for sock in readable:
        if sock == server_sock:
            connection,connection_add = sock.accept()
            message = str(connection_add)+'enter room'
            board_cast(connection,message)
            print connection_add,'%s connect'
            connection_list.append(connection)
        else:
            try:
                date = sock.recv(1024)
                print date
                board_cast(sock,'('+str(sock.getpeername())+') :'+date)
            except:
                message2 = str(sock.getpeername())+ 'is offline'
                board_cast(sock,message2)
                print str(sock.getpeername())+ ' is offline'
                sock.close()
                connection_list.remove(sock)
                continue

客户端代码

# -*- coding: utf-8 -*-
import socket,threading,time
flag = 0
date = ''
lock = threading.Lock()

host = 'localhost'
port = 10001
client_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client_sock.setblocking(0)

class Mythread1(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        global flag, date
        while 1:
            date = raw_input()
            if len(date):
                lock.acquire()
                flag = 1
                lock.release()

class Mythread2(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        global flag
        global date
        while 1:
            try:
                buf = client_sock.recv(1024)
                if len(buf):
                    print buf
            except:
                pass
            if flag:
                try:
                    client_sock.send(date)
                except socket.error, e:
                    print e
                lock.acquire()
                flag = 0
                lock.release()



try:
    client_sock.connect((host,port))
    print"连接成功"
except socket.error,e:
    print e

t1 = Mythread1()
t2 = Mythread2()
t1.start()
t2.start()
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值