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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:19:28+00:00 2026-05-25T12:19:28+00:00

Ive written the following echo server using UDP but i have no idea that

  • 0

Ive written the following echo server using UDP but i have no idea that why it is giving me Segmentation Fault in sendto function, it receives fine but has problem sending data back to client. Ive been trying to find the problem for a few hours now but got no where. Can somebody please point out the fault or what i may be doing wrong.
Thanks

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

#define true 1
#define false 0

int main(int argc,char **args)
{
    int BUF_LENGTH=101;
    int port_no=1800;
    struct sockaddr_in serv_addr,rmt_addr;
    //rmt_addr=malloc(sizeof(struct sockaddr_in));
    char *buffer=malloc(BUF_LENGTH);
    int byte_recv=0;
    int rmt_length=0;

    int sock_id;
    sock_id=socket(AF_INET,SOCK_DGRAM,0);

    if(sock_id<0)
    {
        printf("Error creating socket : %d",sock_id);
        return -1;
    }

    serv_addr.sin_family=AF_INET;
    serv_addr.sin_port=htons(port_no);
    serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");

    bind(sock_id,(struct sockaddr*)&serv_addr,sizeof(serv_addr));

    printf("Created\n");
    while(true)
    {
        printf("Waiting\n");
        byte_recv=recvfrom(sock_id,buffer,BUF_LENGTH,0,(struct sockaddr*)&rmt_addr,&rmt_length);

        printf("%s\n",buffer);
        if(byte_recv<0)
        {
            printf("Error receiving: %d",byte_recv);
            error("recvfrom");
            return -2;
        }

        printf("%d:%d %s\n",rmt_length,rmt_addr.sin_port,inet_ntoa(rmt_addr.sin_addr));

        byte_recv=sendto(sock_id,buffer,BUF_LENGTH,0,(struct sockaddr*)&rmt_addr,rmt_length); //The segmentation fault comes here

        printf("Bytes sent: %d \n",byte_recv);
        if(byte_recv<0)
            error("sendto");
    }

    free(buffer);
    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-25T12:19:28+00:00Added an answer on May 25, 2026 at 12:19 pm

    These lines:

    error("recvfrom");
    
    error("sendto");
    

    don’t do what you think they do. You probably meant to say perror.

    Additionally, you aren’t initializing rmt_length correctly. Try this:

    int rmt_length=sizeof(rmt_addr);
    

    Finally, you are echoing more bytes back than the server receives. Try this:

    byte_recv=sendto(sock_id,buffer,byte_recv,0,(struct sockaddr*)&rmt_addr,rmt_length);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a photoshop plugin for a file format Ive written in c++ that
I have to show the filenames using given template. I've written the following code:
I've written the following IComparer but I need some help. I'm trying to sort
I've written lots of Excel macros in the past using the following development process:
I have written the following constraint for a column I've called 'grade': CONSTRAINT gradeRule
I've written the following custom formset, but for the life of me I don't
I have written the following HTML trying to center two div's next to each
I've written the following, but Java doesn't like it! int limit = Math.round(Math.sqrt(inputNumber)); Suggestions
I've written the following routine to manually traverse through a directory and calculate its
I've written the following code to create a three-column layout where the first and

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.