欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python基于C/S模式實(shí)現(xiàn)聊天室功能

 更新時(shí)間:2019年01月09日 15:13:54   投稿:lijiao  
這篇文章主要為大家詳細(xì)介紹了python基于C/S模式實(shí)現(xiàn)聊天室功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最簡(jiǎn)單的模式,C/S模式實(shí)現(xiàn)聊天室

從半雙工開(kāi)始,何謂半雙工?半雙工即是說(shuō)雙方可以互發(fā)消息,但一次只能一個(gè)用戶發(fā)送。

 只要稍微會(huì)點(diǎn)socket編程的人都會(huì)覺(jué)得很簡(jiǎn)單,所以過(guò)過(guò)場(chǎng),直接上代碼。

服務(wù)器端代碼:

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()

運(yùn)行結(jié)果我就不截圖了,如果還不會(huì)的就復(fù)制下來(lái)運(yùn)行一遍。

上面只是最簡(jiǎn)單的入門(mén),一點(diǎn)都不好使,問(wèn)題多著。

下面看怎么實(shí)現(xiàn)全雙工。全雙工就是雙方可任意給對(duì)方發(fā)送消息。

全雙工明顯要用到多線程。我們?cè)谥骶€程之外創(chuàng)建兩個(gè)子線程,一個(gè)負(fù)責(zé)接收消息,另一個(gè)負(fù)責(zé)接受用戶輸入并發(fā)送消息。

服務(wù)器端代碼:

#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()

運(yùn)行結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論