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

  • Home
  • SEARCH
  • 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 8477463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:28:30+00:00 2026-06-10T18:28:30+00:00

I been reading a lot on how to implement UDP hole punching but fore

  • 0

I been reading a lot on how to implement UDP hole punching but fore some reason I cannot make it work.

For those that are not familiar of what udp hole punching is here is my own definition:

The goal is to be able to transfer data between two clients (Client A
and client B) with the help of a server. So client A connects to the server and sends its info. Client B does the same. The server has the nessesary info so that Client A is able to send data to Client B and vise versa . Therefore the server gives that info to both clients. Once both clients have that info about each other it is possible to start sending and receiving data between those clients without the help of the server.

My goal is to be able to do what I just described (udp hole punching). Before doing so I think it will be helpful to be able to connect from the server to the client. In order to do so I plan to send the server the info about the client. Once the server receives that info attempt to connect to the client from scratch. Once I am able to perform that I should have everything I need to start implementing the real udp hole punching.

Here is how I have things set up:

enter image description here

The top router has the server and bottom router connected to LAN ports. The bottom router (NAT) is connected to the top router via it’s WAN port. And the client computer is connected to the bottom router to one of its LAN ports.

So in that connection the client is able to see the server but the server is not able to see the client.

So the algorithm I have done in pseudo code is:

  • Client connects to server.
  • Client send some UDP packages to the server in order to open some ports on the NAT
  • Send information to the server on what ports the client is listening to.
  • Once the server receives that info attempt to connect to the client from scratch.

Here is the implementation in code:

Server:

static void Main()
{     
    /* Part 1 receive data from client */
    UdpClient listener = new UdpClient(11000);
    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 11000);
    string received_data;
    byte[] receive_byte_array = listener.Receive(ref groupEP);       
    received_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length);

    // get info
    var ip = groupEP.Address.ToString();
    var port = groupEP.Port;

    /* Part 2 atempt to connect to client from scratch */
    // now atempt to send data to client from scratch once we have the info       
    Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    IPEndPoint endPointClient = new IPEndPoint(IPAddress.Parse(ip), port);
    sendSocket.SendTo(Encoding.ASCII.GetBytes("Hello"), endPointClient);
}

Client:

static void Main(string[] args)
{
    /* Part 1 send info to server */
    Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,  ProtocolType.Udp);
    IPAddress send_to_address = IPAddress.Parse("192.168.0.132");
    IPEndPoint sending_end_point = new IPEndPoint(send_to_address, 11000);
    sending_socket.SendTo(Encoding.ASCII.GetBytes("Test"), sending_end_point);

    // get info
    var port = sending_socket.LocalEndPoint.ToString().Split(':')[1];

    /* Part 2 receive data from server */
    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, int.Parse(port));
    byte[] buffer = new byte[1024];
    sending_socket.Receive(buffer);
} 

For some reason it worked a few times! It works when the client receives data successfully on the line: sending_socket.Receive(buffer);

Things to note:
If on the server on the second part I used the instance variable listner instead of creating the new variable sendSocket and send the bytes through that variable the client is able to receive the data being sent. Remember that the second part of the server is going to be implemented by a second client B that’s why I am initializing variables again from scratch…


Edit:

Here is a different way of looking at the same problem. When I initialize a new object instead of using the same object the client does not receives the response.

I have a object of type UdpClient. I am able to send data with that object to the other peer. If I create another object of the same type with the same properties and attempt to send data it does not work! I might be missing to initialize some variables. I am able to set private variables with reflection so I should not have a problem. anyways here is the server code:

public static void Main()
{
    // wait for client to send data
    UdpClient listener = new UdpClient(11000);
    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 11000);        
    byte[] receive_byte_array = listener.Receive(ref groupEP);

    // connect so that we are able to send data back
    listener.Connect(groupEP);

    byte[] dataToSend = new byte[] { 1, 2, 3, 4, 5 };

    // now let's atempt to reply back

    // this part does not work!
    UdpClient newClient = CopyUdpClient(listener, groupEP);
    newClient.Send(dataToSend, dataToSend.Length);

    // this part works!
    listener.Send(dataToSend, dataToSend.Length);
}

static UdpClient CopyUdpClient(UdpClient client, IPEndPoint groupEP)
{
    var ip = groupEP.Address.ToString();
    var port = groupEP.Port;
    var newUdpClient = new UdpClient(ip, port);
    return newUdpClient;
}

the client code basically sends data to the server and then waits for a response:

    string ipOfServer = "192.168.0.132";
    int portServerIsListeningOn = 11000;

    // send data to server
    Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    IPAddress send_to_address = IPAddress.Parse(ipOfServer);
    IPEndPoint sending_end_point = new IPEndPoint(send_to_address, portServerIsListeningOn);
    sending_socket.SendTo(Encoding.ASCII.GetBytes("Test"), sending_end_point);

    // get info
    var port = sending_socket.LocalEndPoint.ToString().Split(':')[1];

    // now wait for server to send data back
    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, int.Parse(port));
    byte[] buffer = new byte[1024];
    sending_socket.Receive(buffer); // <----- keeps waiting in here :(

note that the client is behind a router (NAT) otherwise I will not have this problem. The reason why I will like to copy udpClient is so that I can send that variable to another computer enabling the other computer to send data to the client.

So my question is why is the original object listener able to send data but newClient is not able to? The client keeps waiting at line sending_socket.Receive(buffer); even after the server executes the line: newClient.Send(dataToSend, dataToSend.Length);. the client successfully receives data when listener sends the data but not newClient. Why is this if both variables have the same destination IP and port? how do the variables differ?

Note:
If the server and client are on the same network then the copy works and variable newClient is able to send data to the client. To simulate this problem the client must be behind a NAT (router). An example of such network may consist of two routers. let’s call them router X and router Y. You also need a Server call that S. and a client C. so S can be connected to one of the LAN ports of X. C can be connected to one of the LAN ports of Y. Finally connect the WAN port of Y to one of the LAN ports of X.

  • 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-06-10T18:28:32+00:00Added an answer on June 10, 2026 at 6:28 pm

    Finally found the answer! Here is the implemenation with just a client and a server. My next attempt will be to use 3 computers. anyways hope this helps:

    Server code:

    class Program
    {
        static byte[] dataToSend = new byte[] { 1, 2, 3, 4, 5 };
    
        // get the ip and port number where the client will be listening on
        static IPEndPoint GetClientInfo()
        {
            // wait for client to send data
            using (UdpClient listener = new UdpClient(11000))
            {
                IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 11000);
                byte[] receive_byte_array = listener.Receive(ref groupEP);
    
                return groupEP;
            }
        }
    
        static void Main(string[] args)
        {
            var info = GetClientInfo(); // get client info
    
            /* NOW THAT WE HAVE THE INFO FROM THE CLIENT WE ARE GONG TO SEND
               DATA TO IT FROM SCRATCH!. NOTE THE CLIENT IS BEHIND A NAT AND
               WE WILL STILL BE ABLE TO SEND PACKAGES TO IT
            */
    
            // create a new client. this client will be created on a 
            // different computer when I do readl udp punch holing
            UdpClient newClient = ConstructUdpClient(info);
    
            // send data
            newClient.Send(dataToSend, dataToSend.Length);            
        }
    
        // Construct a socket with the info received from the client
        static UdpClient ConstructUdpClient(IPEndPoint clientInfo)
        {          
            var ip = clientInfo.Address.ToString();
            var port = clientInfo.Port;
    
            // this is the part I was missing!!!!
            // the local end point must match. this should be the ip this computer is listening on
            // and also the port            
            UdpClient client = new UdpClient(new IPEndPoint( IPAddress.Any, 11000));
    
            // lastly we are missing to set the end points. (ip and port client is listening on)
    
            // the connect method sets the remote endpoints
            client.Connect(ip, port);
    
            return client;
        }
    }
    

    client code:

    string ipOfServer = "192.168.0.139";
    int portServerIsListeningOn = 11000;
    
    // send data to server
    Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    IPAddress send_to_address = IPAddress.Parse(ipOfServer);
    IPEndPoint sending_end_point = new IPEndPoint(send_to_address, portServerIsListeningOn);
    sending_socket.SendTo(Encoding.ASCII.GetBytes("Test"), sending_end_point);
    
    // get info
    var port = sending_socket.LocalEndPoint.ToString().Split(':')[1];
    
    // now wait for server to send data back
    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, int.Parse(port));
    byte[] buffer = new byte[1024];
    sending_socket.Receive(buffer); // <----- we can receive data now!!!!!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading a lot about openFrameworks and Processing, But still can't make
I have been reading a lot on design patterns lately and some of them
I've been reading a lot of tutorials around the internet, but even the ones
I've been reading a lot about SOA's lately, but most of the content is
So, i've been reading a lot on SOA's lately and been trying to implement
I've been reading a lot about Hash Tables and how to implement on in
I'm reading a lot since some weeks to implement an encrypt/decrypt algoritm for my
I've been reading a lot about closures and I think I understand them, but
I've been reading a lot of articles about inheritance in javascript. Some of them
I have been reading a lot about QR codes and how the code itself

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.