欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python基于C/S模式实现聊天室功能

时间:2020-11-08 12:31:50|栏目:Python代码|点击:

最简单的模式,C/S模式实现聊天室

从半双工开始,何谓半双工?半双工即是说双方可以互发消息,但一次只能一个用户发送。

 只要稍微会点socket编程的人都会觉得很简单,所以过过场,直接上代码。

服务器端代码:

from socket import *
from time import ctime
 
HOST = ''
PORT = 4568
BUFSIZE = 1024
ADDR = (HOST,PORT)
 
tcpSerSocket = socket(AF_INET, SOCK_STREAM)
tcpSerSocket.bind(ADDR)
tcpSerSocket.listen(5)
 
while True:
 print('waitint for connection...')
 tcpCliSocket, addr = tcpSerSocket.accept()
 print('connecting from: ', addr)
 
 while True:
 data = tcpCliSocket.recv(BUFSIZE)
 if not data:
  break
 print data
 msg = raw_input('>')
 tcpCliSocket.send('[%s] %s' % (ctime(), msg))
 
tcpCliSocket.close()
tcpSerSocket.close()

客户端代码:

from socket import *
 
HOST = 'localhost'
PORT = 4568
BUFSIZE = 1024
ADDR = (HOST, PORT)
 
tcpCliSocket = socket(AF_INET, SOCK_STREAM)
tcpCliSocket.connect(ADDR)
 
while True:
 data = raw_input('>>')
 if not data:
 break
 tcpCliSocket.send(data)
 data = tcpCliSocket.recv(BUFSIZE)
 if not data:
 break
 print data
 
tcpCliSocket.close()

运行结果我就不截图了,如果还不会的就复制下来运行一遍。

上面只是最简单的入门,一点都不好使,问题多着。

下面看怎么实现全双工。全双工就是双方可任意给对方发送消息。

全双工明显要用到多线程。我们在主线程之外创建两个子线程,一个负责接收消息,另一个负责接受用户输入并发送消息。

服务器端代码:

#coding: utf-8
from socket import *
from time import ctime
import threading
from sys import stdout
 
HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)
 
def Send(sck):
 while True:
 data = raw_input('>')
 sck.send(data)
 
def Deal(sck, addr):
 while True:
 data = sck.recv(BUFSIZE)
 if data == "quit":
  sck.close()
  break
 str = '\nfrom' + addr[0] + ':' + data + '\n>'
 stdout.write(str)
 
chatSerSock = socket(AF_INET, SOCK_STREAM)
chatSerSock.bind(ADDR)
chatSerSock.listen(5)
 
threads = []
 
while True:
 print 'waiting for connection...'
 chatCliSock, addr = chatSerSock.accept()
 print "...connected from: ", addr
 t = threading.Thread(target=Deal, args=(chatCliSock, addr))
 threads.append(t)
 t = threading.Thread(target=Send, args=(chatCliSock,))
 threads.append(t)
 for i in range(len(threads)):
 threads[i].start()
 threads[0].join()
 
chatCliSock.close()
chatSerSock.close()

客户端代码:

#coding: utf8
from socket import *
from time import ctime
import threading
from sys import stdout
 
def Send(sck, test):
 while True:
 data = raw_input('>')
 sck.send(data)
 if data == "quit":
  break
def Recieve(sck, test):
 while True:
 data = sck.recv(BUFSIZ)
 if data == "quit":
  sck.close()
  break
 str = "\nfrom server:" + data + "\n>"
 stdout.write(str)
 
HOST = 'localhost'
PORT= 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
threads = []
 
if __name__ == "__main__":
 chatCliSock = socket(AF_INET, SOCK_STREAM)
 chatCliSock.connect(ADDR)
 t = threading.Thread(target=Send, args = (chatCliSock, None))
 threads.append(t)
 t = threading.Thread(target=Recieve, args = (chatCliSock, None))
 threads.append(t)
 for i in range(len(threads)):
 threads[i].start()
 threads[0].join()
 
chatCliSock.close()

运行结果:

上一篇:python最长回文串算法

栏    目:Python代码

下一篇:Python读取文件内容的三种常用方式及效率比较

本文标题:python基于C/S模式实现聊天室功能

本文地址:http://www.codeinn.net/misctech/20373.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有