# -*- coding: utf-8 -*- import socket ''' 使用UDP协议时,不须要创建链接,只须要知道对方的IP地址和端口号,就能够直接发数据包。可是,能不能到达就不知道了。 虽然用UDP传输数据不可靠,但它的优势是和TCP比,速度快,对于不要求可靠到达的数据,就可使用UDP协议。 咱们来看看如何经过UDP协议传输数据。和TCP相似,使用UDP的通讯双方也分为客户端和服务器。服务器首先须要绑定端口 绑定端口和TCP同样,可是不须要调用listen()方法,而是直接接收来自任何客户端的数据 ''' # ipv4 SOCK_DGRAM指定了这个Socket的类型是UDP s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 绑定 客户端口和地址: s.bind(('172.18.153.13', 9999)) print 'Bind UDP on 9999...' while True: # 接收数据 自动阻塞 等待客户端请求: data, addr = s.recvfrom(1024) print 'Received from %s:%s.' % addr s.sendto('Hello, %s!' % data, addr)
#!/bin/bash while : do pid=`ps -ef|grep socat|grep listen|awk '{print $2}' | wc -l` sleep 10 { if [ "$pid" -le 8 ] then socat tcp4-listen:9010,reuseaddr,fork udp:172.18.171.15:43010 >> 9010.log 2>&1 & socat tcp4-listen:9011,reuseaddr,fork udp:172.18.171.15:43011 >> 9011.log 2>&1 & socat tcp4-listen:9012,reuseaddr,fork udp:172.18.171.15:43012 >> 9012.log 2>&1 & socat tcp4-listen:9013,reuseaddr,fork udp:172.18.171.15:43013 >> 9013.log 2>&1 & socat tcp4-listen:9014,reuseaddr,fork udp:172.18.171.15:43014 >> 9014.log 2>&1 & socat tcp4-listen:9015,reuseaddr,fork udp:172.18.171.15:43015 >> 9015.log 2>&1 & socat tcp4-listen:9016,reuseaddr,fork udp:172.18.171.15:43016 >> 9016.log 2>&1 & socat tcp4-listen:9017,reuseaddr,fork udp:172.18.171.15:43017 >> 9017.log 2>&1 & fi }||{ if [ "$pid" -le 8 ] then ps -ef|grep socat|grep listen|awk '{print $2}'|xargs kill -9 sleep 5 socat tcp4-listen:9010,reuseaddr,fork udp:172.18.171.15:43010 >> 9010.log 2>&1 & socat tcp4-listen:9011,reuseaddr,fork udp:172.18.171.15:43011 >> 9011.log 2>&1 & socat tcp4-listen:9012,reuseaddr,fork udp:172.18.171.15:43012 >> 9012.log 2>&1 & socat tcp4-listen:9013,reuseaddr,fork udp:172.18.171.15:43013 >> 9013.log 2>&1 & socat tcp4-listen:9014,reuseaddr,fork udp:172.18.171.15:43014 >> 9014.log 2>&1 & socat tcp4-listen:9015,reuseaddr,fork udp:172.18.171.15:43015 >> 9015.log 2>&1 & socat tcp4-listen:9016,reuseaddr,fork udp:172.18.171.15:43016 >> 9016.log 2>&1 & socat tcp4-listen:9017,reuseaddr,fork udp:172.18.171.15:43017 >> 9017.log 2>&1 & fi } done
原文出处:https://www.cnblogs.com/wsjhk/p/11064992.htmlhtml