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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:15:38+00:00 2026-05-13T09:15:38+00:00

My problem is quite infuriating, actually. I’ll show you the code first. /* **

  • 0

My problem is quite infuriating, actually. I’ll show you the code first.

/*
** listener.c -- a datagram sockets "server" demo
*/
//Original Code: Brian Hall (beej@beej.us)
//Commented and modified by Vishal Kotcherlakota (PID A07124450)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define MYPORT "4960"   // the port users will be connecting to
#define YOURPORT "4961"
#define MAXBUFLEN 10000 

// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
    //If the socket address struct says it's an IPv4...
    if (sa->sa_family == AF_INET) {
        //...return the IPv4 variable.
        return &(((struct sockaddr_in*)sa)->sin_addr);
    }
    //otherwise, assume it's IPv6, and get the IPv6 variable
    return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int main(int argc, char *argv[])
{
    int sockfd, sockfdAck; //socket file descriptor (handle)
    struct addrinfo hints, *servinfo, *p, *q;
    int rv;
    int numbytes;
    unsigned int seqNum, stateNum=0, ackNum;
    struct sockaddr_storage their_addr;
    struct timeval recvTime, timeStamp, latency;
    char buf[MAXBUFLEN], junk[MAXBUFLEN];
    size_t addr_len;
    char *ackBack;
    char s[INET6_ADDRSTRLEN];

    if (argc != 2)
    {
        fprintf(stderr, "usage: <executable> <hostname>\n");
        exit(0);
    }

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_PASSIVE; // use my IP

    if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }

    // loop through all the results and bind to the first we can
    for(q = servinfo; q != NULL; q = q->ai_next) {
        if ((sockfd = socket(q->ai_family, q->ai_socktype,
                q->ai_protocol)) == -1) {
            perror("listener: socket");
            continue;
        }

        if (bind(sockfd, q->ai_addr, q->ai_addrlen) == -1) {
            close(sockfd);
            perror("listener: bind");
            continue;
        }

        break;
    }

    if (q == NULL) {
        fprintf(stderr, "listener: failed to bind socket\n");
        return 2;
    }

    freeaddrinfo(servinfo);

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
    hints.ai_socktype = SOCK_DGRAM;

    if ((rv = getaddrinfo(argv[1], BACKPORT, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }

    // loop through all the results and bind to the first we can
    for(p = servinfo; p != NULL; p = p->ai_next) {
        if ((sockfdAck = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("listener: socket");
            continue;
        }

        break;
    }

    if (p == NULL) {
        fprintf(stderr, "listener: failed to bind socket\n");
        return 2;
    }

    freeaddrinfo(servinfo);
        printf("listener: waiting to recvfrom...\n");
    while (1)
    {

        addr_len = sizeof their_addr;
        if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
            (struct sockaddr *)&their_addr, &addr_len)) == -1) {
            perror("recvfrom");
            exit(1);
        }

        /*printf("listener: got packet from %s\n",
            inet_ntop(their_addr.ss_family,
                get_in_addr((struct sockaddr *)&their_addr),
                s, sizeof s));
        printf("listener: packet is %d bytes long\n", numbytes);
        */
        buf[numbytes] = '\0';
        sscanf(buf,"%u %s",&seqNum, junk);
        if (seqNum == stateNum + 1)
        {
            stateNum = seqNum;
            printf("Ok, state is now %u.\n", stateNum);
        }
        ackBack = malloc(20*sizeof(char));
        sprintf(ackBack, "%u acknowledged\0", stateNum);
        numbytes = sendto(sockfdAck, ackBack, strlen(ackBack), 0, p->ai_addr, p->ai_addrlen);
        if (numbytes == -1);
        {
            perror("sendto");
            exit(1);
        }
        free(ackBack);
    }

    return 0;
} 

Please forgive the sloppy code; I’m desperately trying to finish this assignment on time. The goal is to develop an ARQ protocol using datagram sockets. This code should work, but when I run it, I get an error sendto: Success, meaning that my sendto() call failed. I can’t find documentation for this anywhere, and I’m getting to be extremely desperate.

  • 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-13T09:15:38+00:00Added an answer on May 13, 2026 at 9:15 am

    It has nothing to do with having to bind() – in fact take a look at this syntax:

    if (numbytes == -1) ;        // semicolon !
    {
            perror("sendto");
            exit(1);
    }
    

    You have a condition without the body, and then the body without the condition, which always executes (as you can observe).

    Add the printf of numbytes and you will see it is set correct, there is no error.

    To avoid this kind of hard-to-see errors, I generally put the opening brace immediately after the condition – then you would have spotted this immediately. But of course this is a matter of the coding convention for the company/project.

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

Sidebar

Ask A Question

Stats

  • Questions 257k
  • Answers 257k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes use that structure. Using text to store relational data… May 13, 2026 at 10:44 am
  • Editorial Team
    Editorial Team added an answer Yes, this is perfectly acceptable. A MustOverride member can often… May 13, 2026 at 10:44 am
  • Editorial Team
    Editorial Team added an answer This looks a bit like an IIS log error you've… May 13, 2026 at 10:44 am

Related Questions

My problem is quite simple, I have a CI server which runs msbuild and
My problem is quite simple - I think. I'm doing an ASP.NET MVC project.
My problem is one that you would think is quite common, but I haven't
My question is quite relevant to something asked before but I need some practical
My problem is that this application is deployed with a lot of different payment

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.