码迷,mamicode.com
首页 > 编程语言 > 详细

python udp编程实例

时间:2014-10-11 12:09:05      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:python   socket   udp   

 

与python tcp编程对比见 http://blog.csdn.net/aspnet_lyc/article/details/39854569

c++ udp/tcp 编程见 http://blog.csdn.net/aspnet_lyc/article/details/38946915

                                    http://blog.csdn.net/aspnet_lyc/article/details/34444111

 

server

import socket

PORT      = 9999
BIND_ADDR = ''
MAXLINE   = 1024
buf      = []

listenfd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
listenfd.bind((BIND_ADDR, PORT))

while True:
	buf, client_addr = listenfd.recvfrom(MAXLINE)
	print 'recieve data from %s: %s' % (client_addr, buf)
	buf = 'Hello, this is server'
	listenfd.sendto(buf, client_addr)


client

import socket

PORT     = 9999
SADDR    = '127.0.0.1'
MAXLINE  = 1024
buf     = 'Hello, this is client'

sockfd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sockfd.sendto(buf, (SADDR, PORT))
buf,server_addr = sockfd.recvfrom(MAXLINE)
print 'recieve data from server: %s' % buf


 

 

python udp编程实例

标签:python   socket   udp   

原文地址:http://blog.csdn.net/aspnet_lyc/article/details/39991139

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!