[python]代码库
#! /usr/bin/env python
#服务器程序(tsUserv.py)
from socket import *
from time import ctime
PORT =21567
HOST = ''
BUFSIZ = 1024
ADDR = (HOST,PORT)
udpServSock = socket(AF_INET,SOCK_DGRAM)
udpServSock.bind(ADDR)
while True:
print 'Waiting for message...'
data,addr = udpServSock.recvfrom(BUFSIZ)
if not data:
break
udpServSock.sendto('[%s] %s' % (ctime(),data),addr)
print 'Got message from ...',addr
udpServSock.close()
=====================================================
#! /usr/bin/env python
#客户端程序(tsUclnt.py)
from socket import *
HOST = '192.168.1.190'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)
udpClntSock = socket(AF_INET,SOCK_DGRAM)
while True:
data = raw_input('> ')
if not data:
break
udpClntSock.sendto(data,ADDR)
data,ADDR = udpClntSock.recvfrom(BUFSIZ)
if not data:
break
print data
udpClntSock.close()