1 #!/usr/bin/python 2 #coding=utf-8 3 #服务器端 4 from socket import * 5 from time import ctime 6 HOST="192.168.1.33" 7 PORT=1235 8 BUFSIZE=1024 9 ADDR=(HOST,PORT) 10 11 sockob=socket(AF_INET,SOCK_DGRAM) 12 sockob.bind(ADDR) 13 while 1: 14 print "waiting for message..." 15 data,addr=sockob.recvfrom(BUFSIZE) 16 print "client addr",addr 17 sockob.sendto("[%s]:%s"%(ctime(),data),addr) 18 sockob.close()
1 #!/usr/bin/python 2 #coding=utf-8 3 #客户端 4 from socket import * 5 import sys 6 HOST=sys.argv[1] 7 PORT=int(sys.argv[2]) 8 BUFSIZE=1024 9 ADDR=(HOST,PORT) 10 11 sockob=socket(AF_INET,SOCK_DGRAM) 12 #sockob.bind(ADDR) #这里不能加这个是因为操作在同一台机器. 13 while 1: 14 data=raw_input(‘>‘,) 15 if not data: 16 break 17 sockob.sendto(data,ADDR) #sendto的返回值是字符数量 18 data,ADDR=sockob.recvfrom(BUFSIZE) 19 print data 20 21 sockob.close()
时间: 2024-10-07 09:20:33