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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:40:50+00:00 2026-05-28T03:40:50+00:00

Here is what I have so far (stripped error checking): struct sockaddr_in addr, ss,

  • 0

Here is what I have so far (stripped error checking):

struct sockaddr_in addr, ss, dest;
int port, s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK);

memset((char*) &addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
inet_aton("127.0.0.1", &addr.sin_addr);
bind(s, (struct sockaddr*) &addr, sizeof(addr));

unsigned int len = sizeof(ss);
getsockname(s, (struct sockaddr*) &ss, &len);
port = ss.sin_port;

memset((char*) &dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(port);
inet_aton("127.0.0.1", &dest.sin_addr);
sendto(s, "test", 5, 0, (struct sockaddr*) &dest, sizeof(dest));

char buf[5];
recv(s, buf, 5, 0);

The last sentence fails with a message of Resource temporarily unavailable (because of the O_NONBLOCK flag).

In the snippet I let the OS to bind a random port, and then I obtain it with getsockname. If I use a fixed port instead and remove the call to getsockname then it works.

PS: I’m on a linux machine.

  • 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-28T03:40:51+00:00Added an answer on May 28, 2026 at 3:40 am

    You forgot to use ntohs when you captured the OS-assigned port. Here’s what I ended up with (I did a couple small things to make the code more concise too):

    #include <netinet/in.h>
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    
    int main()
    {
      struct sockaddr_in addr = {}, ss, dest = {};
      int port, s = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
    
      addr.sin_family = AF_INET;
      addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
      bind(s, (struct sockaddr*) &addr, sizeof(addr));
    
      unsigned int len = sizeof(ss);
      getsockname(s, (struct sockaddr*) &ss, &len);
      port = ntohs(ss.sin_port);
    
      dest.sin_family = AF_INET;
      dest.sin_port = htons(port);
      dest.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
      sendto(s, "test", 5, 0, (struct sockaddr*) &dest, sizeof(dest));
    
      char buf[5];
      int got = recv(s, buf, 5, 0);
    
      printf("got: %d, errno: %s\n", got, strerror(errno));
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a total newb to LINQ. Here is the code I have so far:
Here is what I have tried thus far: > cd /Applications/MAMP/bin/php5/bin/ > ./pear channel-discover
I really wonder why this question hasn't popped up here so far. I have
Here is what I have so far: type Maybe<'a> = option<'a> let succeed x
Ok here's what I have so far.. thanks to Paolo. And it works fine,
The title is self explanatory, here is what I have so far, but the
Here's what I have so far: var bestReason = from p in successfulReasons group
Here’s what I have so far: FtpWebRequest reqFTP; string returnString = ; Uri serverFile
Here is What I have So Far declare @Today smalldatetime Set @Today = GETDATE()
Here is what I have so far: My string: $str = <ul> <li><a name=valuehere1

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.