Python半双工聊天程序

服务器程序:

 1 #!/usr/bin/python
 2 
 3 import socket
 4 
 5 HOST=''
 6 PORT=8008
 7 ADDR=(HOST,PORT)
 8 BUFSIZ=8096
 9 
10 SerSock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
11 SerSock.bind(ADDR)
12 
13 while True:
14     data,addr=SerSock.recvfrom(BUFSIZ)
15     if not data:
16         continue
17     print ">%s"%data
18     while True:
19         data=raw_input(">")
20         if not data:
21             continue
22         else:
23             break
24     SerSock.sendto(data,addr)
25     
26 
27 SerSock.close()

客户端程序:

#!/usr/bin/python

import socket

HOST='localhost'
PORT=8008
ADDR=(HOST,PORT)
BUFSIZ=8096

CliSock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

while True:
    data=raw_input(">")
    if not data:
        continue
    CliSock.sendto(data,ADDR)
    data,addr=CliSock.recvfrom(BUFSIZ)
    if not data:
        break
    print ">%s"%data

CliSock.close()

 

posted @ 2015-06-09 18:09  tmyyss  阅读(349)  评论(0编辑  收藏  举报