本文介绍了简单的udp代理解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找可以代理我的 udp 数据包的解决方案.我有一个客户端向服务器发送 udp 数据包.它们之间的连接非常糟糕,我丢失了很多数据包.一种解决方案是拥有一个新的代理服务器,它将所有数据包从客户端重定向到目标服务器.新的代理服务器与这两个位置都有良好的连接.

I am looking for solution that can proxy my udp packets. I have one client sending udp packets to a server. Connection between them is very bad and I get lot of packet loss. One solution is to have a new proxy server that will just redirect all packets from client to destination server. The new proxy server has good connection to both locations.

到目前为止我已经找到了简单的 UDP 代理/管道

So far I have found Simple UDP proxy/pipe

是否有一些工具可以用于此目的?

Are there some tools for such purpose ?

干杯

推荐答案

以下是为此目的编写的 Python 代码:

Here is Python code written for this purpose:

import socket
from threading import Thread

class Proxy(Thread):
    """ used to proxy single udp connection
    """
    BUFFER_SIZE = 4096
    def __init__(self, listening_address, forward_address):
        print " Server started on", listening_address
        Thread.__init__(self)
        self.bind = listening_address
        self.target = forward_address

    def run(self):
        # listen for incoming connections:
        target = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        target.connect(self.target)

        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            s.bind(self.bind)
        except socket.error, err:
            print "Couldn't bind server on %r" % (self.bind, )
            raise SystemExit
        while 1:
            datagram = s.recv(self.BUFFER_SIZE)
            if not datagram:
                break
            length = len(datagram)
            sent = target.send(datagram)
            if length != sent:
                print 'cannot send to %r, %r !+ %r' % (self.target, length, sent)
        s.close()


if __name__ == "__main__":
    LISTEN = ("0.0.0.0", 8008)
    TARGET = ("localhost", 5084)
    while 1:
        proxy = Proxy(LISTEN, TARGET)
        proxy.start()
        proxy.join()
        print ' [restarting] '

我用这两个脚本来测试它.

I used this two scripts to test it.

import socket

target = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
target.connect(("localhost", 8008))
print 'sending:', target.send("test data: 123456789")

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("localhost", 5084))
while 1:
    datagram = s.recv(1024)
    if not datagram:
        break
    print repr(datagram)

s.close()

这篇关于简单的udp代理解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 09:55