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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:04:20+00:00 2026-06-14T05:04:20+00:00

I am writing a simple client and server program in C. I am able

  • 0

I am writing a simple client and server program in C. I am able to send date from client to server. But, I am not able to send acknowledge from server to client.

/*******************udpserver.c*****************/
int main(int argc, char *argv[])
{
    /* Variable and structure definitions. */
    int sd, rc;
    struct sockaddr_in serveraddr, clientaddr;
    clientaddrlen = sizeof(clientaddr);
    int serveraddrlen = sizeof(serveraddr);
    char buffer[100];
    char *bufptr = buffer;
    int buflen = sizeof(buffer);

    if((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("UDP server - socket() error");
        exit(-1);
    }

    printf("UDP server - socket() is OK\n");

    memset(&serveraddr, 0x00, serveraddrlen);
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(0);
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    if((rc = bind(sd, (struct sockaddr *)&serveraddr, serveraddrlen)) < 0) {
        perror("UDP server - bind() error");
        close(sd);
        exit(-1);
    }

    int addr_len = sizeof(serveraddr);
    if (getsockname(sd, (struct sockaddr *) &serveraddr, &addr_len)<0) {
        perror("Error getting socket name.\n");
        return -1;
    }
    printf("Using IP %s and port %d\n", inet_ntoa(serveraddr.sin_addr), ntohs(serveraddr.sin_port));
    printf("UDP server - Listening...\n");

    rc = recvfrom(sd, bufptr, buflen, 0, (struct sockaddr *)&clientaddr, &clientaddrlen);
    if(rc < 0) {
        perror("UDP Server - recvfrom() error");
        close(sd);
        exit(-1);
    }

   printf("UDP Server received the following:\n \"%s\" message\n", bufptr);

   printf("UDP Server replying to the UDP client...\n");
   rc = sendto(sd, bufptr, buflen, 0, (struct sockaddr *)&clientaddr, clientaddrlen);
   if(rc < 0) {
       perror("UDP server - sendto() error");
       close(sd);
       exit(-1);
   }
   printf("UDP Server - sendto() is OK...\n");

   close(sd);
   exit(0);
}

My UDPClient program:

/****************udpclient.c********************/
int main(int argc, char *argv[])
{
    /* Variable and structure definitions. */
    int sd, rc;
    struct sockaddr_in serveraddr, clientaddr;
    int serveraddrlen = sizeof(serveraddr);
    char server[255];
    char buffer[100];
    char *bufptr = buffer;
    int buflen = sizeof(buffer);
    struct hostent *hostp;
    memset(buffer, 0x00, sizeof(buffer));
    /* 36 characters + terminating NULL */
    memcpy(buffer, "Hello! A client request message lol!", 37);

    if((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("UDP Client - socket() error");
        exit(-1);
    }
    else
        printf("UDP Client - socket() is OK!\n");

    if(argc != 3) {
        /*Use default hostname or IP*/
        printf("UDP Client - Usage <Server hostname or IP>\n");
        exit(0);
    }

    memset(&serveraddr, 0x00, sizeof(struct sockaddr_in));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(atoi(argv[2]));

    hostp = gethostbyname(argv[1]);
    if(hostp == (struct hostent *)NULL) {
        printf("HOST NOT FOUND --> ");
        printf("h_errno = %d\n", h_errno);
        exit(-1);
    }
    else {
        printf("UDP Client - gethostname() of the server is OK... \n");
        printf("Connected to UDP server\n");
    }
    memcpy(&serveraddr.sin_addr, hostp->h_addr, sizeof(serveraddr.sin_addr));

    rc = sendto(sd, bufptr, buflen, 0, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
    if(rc < 0) {
        perror("UDP Client - sendto() error");
        close(sd);
        exit(-1);
    }
    else
        printf("UDP Client - sendto() is OK!\n");

    printf("Waiting a reply from UDP server...\n");

    rc = recvfrom(sd, bufptr, buflen, 0, (struct sockaddr *)&serveraddr, &serveraddrlen);

    if(rc < 0) {
        perror("UDP Client - recvfrom() error");
        close(sd);
        exit(-1);
    } else {
        printf("UDP client received the following: \"%s\" message\n", bufptr);
    }

    close(sd);
    exit(0);
}

When running the two programs, I am getting the following output:

UdpServer:

$ ./UdpServer
UDP server - socket() is OK
Using IP 0.0.0.0 and port 49932
UDP server - Listening...
UDP Server received the following:
"Hello! A client request message lol!" message
UDP Server replying to the UDP client...
UDP Server - sendto() is OK...

UdpClient:

$ ./UdpClient MyPC 49932 
UDP Client - socket() is OK!
UDP Client - gethostname() of the server is OK...
Connected to UDP server
UDP Client - sendto() is OK!
Waiting a reply from UDP server...

UdpClient program is stuck at this point. Could anyone please explain what the problem is?

  • 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-06-14T05:04:21+00:00Added an answer on June 14, 2026 at 5:04 am

    You might like to use select() to make the process wait until data is avaibale for reading:

    ...
    {
      fd_set rfds;
      int retval;
    
      FD_ZERO(&rfds);
      FD_SET(sd, &rfds);
    
      retval = select(sd+1, &rfds, NULL, NULL, NULL);
      if (retval == -1)
        perror("select()");
      else
        printf("Data is available for reading now.\n");
    
      ...
    }
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple client server program that I made but the main issue
I am writing simple client-server program. Client send some messages to server using UDP
i am writing a simple client/server chat program with indy 10 (blocking mode) and
I am writing a simple C# tcp client and server program. The server will
Hi I am writing a simple client-server program. In this program I have to
I am writing a simple SMTP server and client. Server is in two parts
I'm writing a simple program that will run entirely client-side. (Desktop programming? do people
I'm writing a simple console client-server app using MSMQ. I'm attempting to run it
While writing a simple server-client application, this question came in my mind. When someone
I am writing a simple client program socket programming when compiling the code using

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.