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 6765293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:41:43+00:00 2026-05-26T14:41:43+00:00

I am new to UDP/networking programming, and I am trying to create a chat

  • 0

I am new to UDP/networking programming, and I am trying to create a chat board via UDP.

My ultimate aim is to do UDP hole-punching (something of a similar concept to Skype), so I need to listen on the same port as the port used to send data, otherwise the NAT will drop the incoming packet.

I have tried doing

server.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

but I am unable to receive data from a port that has been used to send data.

This is what my code for the listener looks like (it is on a separate thread) (it is adapted from another tutorial on the web):

        byte[] data = new byte[1024];
        IPEndPoint ipep = new IPEndPoint(IPAddress.Any, (int)e.Argument);
        UdpClient newsock = new UdpClient();
        newsock.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        newsock.Client.Bind(ipep);
        IPEndPoint from_ip = new IPEndPoint(IPAddress.Any, 0);
        Invoke(new Action(() => { WriteOnScreen("Done!"); }));
        while (true)
        {
            data = newsock.Receive(ref from_ip);
            string s = Encoding.ASCII.GetString(data, 0, data.Length);
            //more stuff to deal with s
        }

This is my sender:

        UdpClient server = new UdpClient();
        server.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        server.Connect(CurrIP, CurrPort);

The method server.Close() will be only called when the application is terminated by the user.

When I checked the local port of the sender, I realised that the listener fails to listen only when the local port of the receiver “(int)e.Argument” is equal to the local port of the sender. When I set the receiving port to a different port, I am able to receive packets.

Also, I can’t create more instances of UdpClient as my local port for each UdpClient will be different, and so I would be unable to do hole-punching.

I searched many places already, and no one seems to have this problem…

I believe that there is an easier way…

  • 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-26T14:41:43+00:00Added an answer on May 26, 2026 at 2:41 pm

    You need to use the same UdpClient for both sending and receiving.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to Linux programming in general. I am trying to communicate with a kernel
I'm new to Windows networking, and I am trying to find out which PORT
Im trying out the new additions of Socket-communication (TCP & UDP) in the Beta
I am quite new to socket programming. I was trying to make p2p communication
Here is my code Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); sck.Bind(new IPEndPoint(IPAddress.Any, 0));
I have a simple UDP server that creates a new thread for processing incoming
If I send a UDP packet containing 'foo' like this: socket = UDPSocket.new socket.setsockopt(Socket::SOL_SOCKET,
I am new to java. I receive the UDP data in byte array. Each
I'm very very new to programming in Python, but out of necessity I had
I'm fairly new to C programming and having some difficulties wrapping my brain around

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.