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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:46:23+00:00 2026-05-27T06:46:23+00:00

In the code below I have 3 functions (including main ). The receive_loop function

  • 0

In the code below I have 3 functions (including main). The receive_loop function polls the socket bound to 127.0.0.1 and if it gets any message, it prints it. The sender_loop waits for user input using scanf and when there is an input, it sends it through the socket bound to 127.0.0.1. The main forks and runs sender_loop in child and receive_loop in the parent.

Now if I compile the code and run two instances of this executable, whichever instance starts up first receives the messages sent by either instance. So lets say, instance1 was started first, then instance1 receives messages sent by instance1 AND instance2. I cannot figure out why this is happening and I would appreciate if someone could explain it please (in detail). Thanks.

// INCLUDES NOT SHOWN

// recieve_loop
int receive_loop(int sock_fd, struct sockaddr *peer) {
    int isDataReady = 0;
    char buff[128]  = {0};
    struct pollfd poll_struct;

    poll_struct.fd     = sock_fd; 
    poll_struct.events = POLLIN;

    printf("reciever up\n");
    while (1) {
        while (1) {
            if (isDataReady > 0) break;
            isDataReady = poll(&poll_struct, 1, 500);
        }
        socklen_t sock_len = sizeof(struct sockaddr);
        recvfrom(sock_fd,
                buff,
                sizeof(buff),
                0,
                (struct sockaddr *)peer,
                &sock_len);

        printf("%s\n", buff);
    }

    return 0;
}


// sender_loop
int sender_loop(int sock_fd, struct sockaddr *peer) {
    char buff[32] = {0};

    printf("sender up\n");
    while (1) {
        scanf("%s", buff);

        int bytes_sent = sendto(sock_fd, buff, sizeof(buff), 0, (struct sockaddr *)peer, sizeof(struct sockaddr_in));

        if (bytes_sent <= 0)
            printf("sending message failed\n");

    }

    return 0;
}

int main(int argc, const char *argv[]) {
    // socket file descriptor to send data through
    int sock_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

    // fill in the peer's address, loopback in this case
    struct sockaddr_in *peer = malloc(sizeof(struct sockaddr_in));
    peer->sin_family = AF_INET;
    peer->sin_port   = htons(11110);
    inet_pton(AF_INET, "127.0.0.1", &peer->sin_addr);

    bind(sock_fd, (struct sockaddr *)peer, sizeof(struct sockaddr_in)); 

    pid_t pid = fork();
    if (pid < 0) {
        printf("Couldn't fork, exiting");
        return 1;
    }
    if (pid == 0) {
        sender_loop(sock_fd, (struct sockaddr *)peer);
    } else {
        receive_loop(sock_fd, (struct sockaddr *)peer);
    }
    return 0;
}
  • 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-27T06:46:23+00:00Added an answer on May 27, 2026 at 6:46 am

    Your second instance is failing on bind() because it is trying to bind to a port already in use by the first instance.

    Change to this and see.

    int ret = bind(sock_fd, (struct sockaddr *)peer, sizeof(struct sockaddr_in));
    if (ret < 0)
    {
        perror("bind");
        exit(1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error, while I'm testing the code below: You have an error
I have below code in html. <li class=selected runat=server id=lihome><a href=/ISS/home.aspx title=Home><span>Home</span></a></li> Now I
I have below code behind in c# if (Session[cmpDictionaryTitle]!= null) { downloadLinks.Text += @<li><a
I have below code to insert a style into DOM (there is a use
I have code below: <select id=testSelect> <option value=1>One</option> <option value=2>Two</option> </select> <asp:Button ID=btnTest runat=server
Code below is working well as long as I have class ClassSameAssembly in same
I have below html code in my aspx. <input type=hidden id=medicalLink value='<a href=/forms/contactus.aspx >Contact
Hi I have the code below, once the first (only) item in the menu
I have below XAML code : <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml DataContext={Binding RelativeSource={RelativeSource Self}} WindowStartupLocation=CenterScreen
I have example of code below. <script type=text/javascript src=assets/scripts/somescript.php>. </script> So, will my browser

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.