Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6470791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:09:03+00:00 2026-05-25T06:09:03+00:00

Can someone spot what I did wrong here? Seems to only create one instance

  • 0

Can someone spot what I did wrong here? Seems to only create one instance of the sockets not two.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace UdpProxy
{
    class Program
    {
        public static UdpClient server = null;

        static void Main(string[] args)
        {
            int localPort = 7900;
            IPEndPoint remoteSender = new IPEndPoint(IPAddress.Any, 4001);

            IPAddress tempAddress;

            IPAddress.TryParse("OUT_GOING_IP/HOST_GOES_HERE", out tempAddress);
            remoteSender.Address = tempAddress;
            remoteSender.Port = 7900;

            // Display some information
            Console.WriteLine("Welcome! Starting Upd proxy server.");
            Console.WriteLine("Local port: " + localPort);
            Console.WriteLine("Remote ip: " + remoteSender.Address.ToString());
            Console.WriteLine("Remote port: " + remoteSender.Port);
            Console.WriteLine("Press any key to quit.");

            // Create UDP client
            UdpClient client = new UdpClient(localPort);
            UdpState state = new UdpState(client, remoteSender);
            state.setRemote(remoteSender);
            // Start async receiving
            client.BeginReceive(new AsyncCallback(DataReceivedClient), state);



            // Wait for any key to terminate application
            Console.ReadKey();
            client.Close();
        }

        private static void DataReceivedClient(IAsyncResult ar)
        {
            UdpClient c = (UdpClient)((UdpState)ar.AsyncState).c;
            IPEndPoint ipEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).e; //local ip and random port.
            IPEndPoint remoteIPEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).remote;
            byte[] receiveBytes = c.EndReceive(ar, ref ipEndPoint);

            // Convert data to ASCII and print in console
            string receivedText = BitConverter.ToString(receiveBytes);
            Console.WriteLine("Client 2 Server = " + receivedText);


            if (server == null)
            {
                // Create UDP client
                server = new UdpClient(new IPEndPoint(IPAddress.Any, 0));
                UdpState stateServer = new UdpState(server, remoteIPEndPoint);
                server.BeginReceive(new AsyncCallback(DataReceiveServer), stateServer);
                server.Connect(remoteIPEndPoint);

            }

            server.Send(receiveBytes, receiveBytes.Length);

            // Restart listening for udp data packages
            c.BeginReceive(new AsyncCallback(DataReceivedClient), ar.AsyncState);
        }


        private static void DataReceiveServer(IAsyncResult ar)
        {
            UdpClient c = (UdpClient)((UdpState)ar.AsyncState).c;
            IPEndPoint ipEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).e; //local ip and random port.
            byte[] receiveBytes = c.EndReceive(ar, ref ipEndPoint);

            // Convert data to ASCII and print in console
            string receivedText = BitConverter.ToString(receiveBytes);
            Console.WriteLine("Server 2 Client = " + receivedText);


            c.Connect(ipEndPoint);
            c.Send(receiveBytes, receiveBytes.Length);

            // Restart listening for udp data packages
            c.BeginReceive(new AsyncCallback(DataReceiveServer), ar.AsyncState);
        }
    }
}

supporting helper class

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace UdpProxy
{
    /// <summary>
    /// Simple implementation of the UdpState class mentioned on 
    /// http://msdn.microsoft.com/en-us/library/c8s04db1(v=VS.80).aspx
    /// </summary>
    internal class UdpState
    {
        internal UdpState(UdpClient c, IPEndPoint e)
        {
            this.c = c;
            this.e = e;
        }

        internal void setRemote(IPEndPoint remote)
        {
            this.remote = remote;
        }

        internal UdpClient c;
        internal IPEndPoint e;
        internal IPEndPoint remote;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:09:04+00:00Added an answer on May 25, 2026 at 6:09 am

    Fixed it here is the solution if anyone wants to learn how I fixed it.. Please note this is probably the only UDP Proxy on all of google if you stumbled upon this.. that is coded in C#.. easily ported to VB.NET with online .NET converter

    Be happy this code works 😉

    Sure it’s not efficient because it doesn’t use events.. like ReceiveAsync/EndReceive.

    Only downfall to not using Aysnchronize events.. is that you see below the working code.. will have to be stuck in a infinite loop.. and it will burn your CPU cycles.. easily fixed with a Thread.Sleep(10).. (don’t set to high or you will have udp lag)

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net.Sockets;
    using System.Net;
    
    namespace UdpProxy
    {
        class Program
        {
            public static IPEndPoint m_listenEp = null;
            public static EndPoint m_connectedClientEp = null;
            public static IPEndPoint m_sendEp = null;
            public static Socket m_UdpListenSocket = null;
            public static Socket m_UdpSendSocket = null;
    
    
            static void Main(string[] args)
            {
    
                // Creates Listener UDP Server
                m_listenEp = new IPEndPoint(IPAddress.Any, 7900);
                m_UdpListenSocket = new Socket(m_listenEp.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
                m_UdpListenSocket.Bind(m_listenEp);
    
                //Connect to zone IP EndPoint
                m_sendEp = new System.Net.IPEndPoint(IPAddress.Parse("REMOTE_IP_GOES_HERE"), 7900);
                m_connectedClientEp = new System.Net.IPEndPoint(IPAddress.Any, 7900);
    
                byte[] data = new byte[1024];
    
                while (true)
                {
                    if (m_UdpListenSocket.Available > 0)
                    {
    
                        int size = m_UdpListenSocket.ReceiveFrom(data, ref m_connectedClientEp); //client to listener
    
                        if (m_UdpSendSocket == null)
                        {
                            // Connect to UDP Game Server.
                            m_UdpSendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                        }
    
                        m_UdpSendSocket.SendTo(data, size, SocketFlags.None, m_sendEp); //listener to server.
    
                    }
    
                    if (m_UdpSendSocket != null && m_UdpSendSocket.Available > 0)
                    {
                        int size = m_UdpSendSocket.Receive(data); //server to client.
    
                        m_UdpListenSocket.SendTo(data, size, SocketFlags.None, m_connectedClientEp); //listner
    
                    }
                }
    
    
                // Wait for any key to terminate application
                Console.ReadKey();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can someone help me spot the problem here ? #!/bin/sh find . -name '*ABC*'
Can someone spot the problem with this implementation? I can open it up in
Can someone explain to me the advantages of using an IOC container over simply
Can someone explain what are the benefits of using the @import syntax comparing to
I'm trying to create some 'friend assemblies' using the [InternalsVisibleTo()] attribute, but I can't
Can someone spot my mistake?... pred = [NSPredicate predicateWithFormat:@ICAO contains[cd] %1$@ OR IATA contains[cd]
I am struggling with preg_replace and am really hoping someone can spot my error.
This may be too obscure a question, but perhaps someone can spot what I'm
I'm not sure why am I getting this strange result. Can someone please shed
Can someone show me how to implement a recursive lambda expression to traverse a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.