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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:21:00+00:00 2026-05-24T14:21:00+00:00

All of the tutorials and examples I find online always specify a port number

  • 0

All of the tutorials and examples I find online always specify a port number like 7000 or 4950 etc. What if those ports are open on one computer, but another? Seems like that case makes doing that a bad idea. Is there a way to say “find and use any open port”? My code now is like this –

//get server info, put into servinfo
if ((status = getaddrinfo("192.168.2.2", port, &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
    return false;
}

with port being 4950. This is for a tcp socket, but I’m assuming it will be the same general strategy for udp?

Also quick question – if I am using both tcp and udp connections in an application, should they use different ports? (didn’t feel like this deserved another question)

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

    Seems like that case makes doing that a bad idea

    Not so, see below.

    Is there a way to say “find and use any open port”?

    Sure, you could just not bind or pass NULL as port. But then, how would the clients know where to connect to ? You would have to publish this information somewhere.

    Back to bind(2). If you specify a port of 0 the kernel chooses an ephemeral port when bind is called.

    Here’s a quote from TLPI:

    There are other possibilities than binding a server’s socket to a
    well-known address. For example, for an Internet domain socket, the
    server could omit the call to bind()
    and simply call listen(), which
    causes the kernel to choose an ephem- eral port for that socket.

    Afterward, the server can use getsockname() to retrieve the address
    of its socket. In this scenario, the server must then publish that
    address
    so that clients know how to locate the server’s socket. Such
    publication could be done by registering the server’s address with a
    centralized directory service application that clients then contact in
    order to obtain the address.

    Back to your questions:

    Also quick question – if I am using both tcp and udp connections in an
    application, should they use different ports

    Not necessarily. They don’t “get mixed up”.

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

Sidebar

Related Questions

All tutorials I can find always tackle with toy uml use cases like the
In all the tutorials or examples I find, they show a colon after the
After browsing online for tutorials on Javascript show/hide I could only find examples on
All the tutorials and examples I've found of XSLT processing seem to assume your
Almost all of the examples in the jQuery tutorials that I've read, usually use
I am trying to find documentation, tutorials, examples on how to generate sounds. I
I have been watching Xcode tutorials on youtube and learning from examples online but
All, I'm reviewing some basic MySQL JOIN tutorials. Jeff Atwood gives an example on
I'm learning Eclipse PDT for PHP development. In pretty much all tutorials I've read
Of all the tutorials i have come across to connect to an external database

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.