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

作者:lijiao 时间:2021-05-01 04:17:53 

最简单的模式,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基于C/S模式实现聊天室功能

来源:https://blog.csdn.net/whoami021/article/details/21285421

标签:python,聊天室
0
投稿

猜你喜欢

  • python正则实现计算器功能

    2022-06-13 06:14:00
  • asp fso操作类

    2011-03-07 10:57:00
  • Python selenium 三种等待方式详解(必会)

    2021-03-13 16:03:33
  • 跟我学习javascript的基本类型和引用类型

    2024-04-28 09:46:42
  • Python 不同对象比较大小示例探讨

    2023-06-11 01:13:32
  • Python常用内置函数的使用教程详解

    2021-11-11 21:23:50
  • 认识Javascript数组

    2009-08-27 15:26:00
  • python列表操作之extend和append的区别实例分析

    2023-08-02 15:14:30
  • pytorch 优化器(optim)不同参数组,不同学习率设置的操作

    2021-09-14 05:05:44
  • Java 如何使用JDBC连接数据库

    2024-01-23 04:30:12
  • python函数默认参数使用避坑指南

    2023-06-19 13:37:50
  • Python3.5实现的三级菜单功能示例

    2023-08-01 13:37:26
  • PyTorch基本数据类型(一)

    2023-06-15 20:56:39
  • python多线程抓取天涯帖子内容示例

    2021-03-18 11:56:42
  • JS表格排序新法

    2010-07-09 06:40:13
  • JavaScript下利用fso判断文件是否存在的代码

    2024-05-02 17:25:30
  • Python 如何对文件目录操作

    2022-10-08 05:40:04
  • sqlserver中的自定义函数的方法小结

    2024-01-24 22:14:11
  • 用 Schema 约束 XML 数据

    2010-08-24 18:21:00
  • Django框架ORM数据库操作实例详解

    2024-01-24 00:23:33
  • asp之家 网络编程 m.aspxhome.com