socket编程学习

基于udp的socket

udp是无连接的,那边先启动不会报错

udp的小例子:

1 from socket import  *
2 ip_port=('127.0.0.1',8888)
3 buffer_size=1024
4 myudp_server=socket(AF_INET,SOCK_DGRAM)
5 myudp_server.bind(ip_port)
6 while True:
7     message,addr=myudp_server.recvfrom(buffer_size)
8     print(message,addr)
9     myudp_server.sendto(message,addr)
udp-server
 1 from socket import *
 2 ip_port=('127.0.0.1',8888)
 3 mycli=socket(AF_INET,SOCK_DGRAM)
 4 
 5 while True:
 6     myinput=input('>>').strip()
 7     if not myinput:continue
 8     mycli.sendto(myinput.encode('utf8'),ip_port)
 9     mes,addr=mycli.recvfrom(1024)
10     print(mes.decode('utf8'))
udp-client

 

posted @ 2018-07-10 22:31  hexintong  阅读(86)  评论(0编辑  收藏  举报