T
T
tex6202020-10-05 18:23:48
C++ / C#
tex620, 2020-10-05 18:23:48

How to do udp proxying in c#?

I made a udp client on port 10000, you need to proxy all client packets to a certain targetip: port. There are no problems with this.
But when the server returns a response, how do you know which client to send it to?

public class UdpProxy
    {
        private IPEndPoint target;
        private UdpClient client;

        int localport;
        private Task proxy;
        public UdpProxy(string targetaddress, ushort localport)
        {
            this.target = IPEndPoint.Parse(targetaddress);
            this.client = new UdpClient(new IPEndPoint(IPAddress.Any, localport));
            this.localport = localport;
        }

        public void StartProxy()
        {
            this.proxy = Task.Factory.StartNew(() => {
                IPEndPoint _IPEndPoint = null;
                while (true) { 
                    try
                    {
                        if (client.Available > 0)
                        {
                            byte[] _bytes = client.Receive(ref _IPEndPoint);
                            
                            if (_IPEndPoint == target)
                            {
                                //server to client
                                //???
                            } else
                            {
                                //client to server
                                client.Send(_bytes, _bytes.Length, target);
                            }
                        }
                        Thread.Sleep(5);
                    } catch (Exception e) { }
                }
            });
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Dubrovin, 2020-10-05
@z3apa3a

for each ip:port pair of the client, open a separate listening socket (on a separate port) towards the server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question