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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:13:04+00:00 2026-05-29T04:13:04+00:00

I am new to network programming, and have been learning this by writing small

  • 0

I am new to network programming, and have been learning this by writing small programs that make use of the Socket API. Currently, I am writing a simple echo server, that uses fork to create a copy of it, as soon as it gets a connect request, this adds up as in improvement over the previous Iterative server (here). However, after I start the server and fire up the client, and type a message on its console, it quits unexpectedly. Running the program under Gdb shows that SIGPIPE was delivered. But as far as I know as the socket is still valid, a SIGPIPE shouldn’t have occured. Any kind of help involved is appreciated.
Here is the client code

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

#define MAXCOUNT 1024

int main(int argc, char* argv[])
{
    int sfd,i;
    struct sockaddr_in saddr;
    char buff[MAXCOUNT];
    char mesg[MAXCOUNT];
    sfd = socket(AF_INET,SOCK_STREAM,0);
    memset(&saddr,0,sizeof(saddr));
    saddr.sin_family = AF_INET;
    inet_pton(AF_INET,"127.0.0.1",&saddr.sin_addr);
    saddr.sin_port = htons(5008);
    connect(sfd,(struct sockaddr*) &saddr,sizeof(saddr));
    fgets(buff,MAXCOUNT,stdin);
    send(sfd,buff,strlen(buff),0);
    if (recv(sfd,mesg,MAXCOUNT,0) == -1) {
        perror("Nothing to read\n");
        exit(1);
    }
    printf("%s\n",mesg);
    exit(0);    
}

Here is the server code

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

#define MAXCOUNT 1024

int main(int argc, char* argv[])
{
    int sfd,nsfd,cn;
    pid_t c;
    char buf[MAXCOUNT];
    socklen_t clen;
    struct sockaddr_in caddr,saddr;

    sfd = socket(AF_INET,SOCK_STREAM,0);
    memset(&saddr,0,sizeof(saddr));
    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    saddr.sin_port = htons(5008);

    bind(sfd,(struct sockaddr*) &saddr,0);

    listen(sfd,2);
    for (; ;) {
        clen = sizeof(caddr);
        nsfd = accept(sfd,(struct sockaddr*) &caddr, &clen);
        if( (c = fork()) == 0) {
            close(sfd);
            memset(buf,0,sizeof(buf));
            cn = recv(nsfd,buf,sizeof(buf),0);
            if ( cn == 0) {
                perror("Reading from the client socket failed\n PROGRAM CRASH :\n");
                exit(1);
            }
            buf[cn] = '\0';
            send(nsfd,buf,strlen(buf),0);
            close(nsfd);
            exit(0);
        }
    }
    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-29T04:13:04+00:00Added an answer on May 29, 2026 at 4:13 am
    send(sfd,buff,strlen(buff),0);
    if (recv(sfd,mesg,MAXCOUNT,0) == -1) {
        perror("Nothing to read\n");
        exit(1);
    }
    printf("%s\n",mesg);
    

    The %s format specifier is for C-style strings, not arbitrary data. And since you throw away the return value from recv, you have no way to know how many bytes you got.

    Your client also doesn’t shut down the socket gracefully or make sure it has received all the data the server may send. So it’s possible that you’re triggering an abnormal termination. The server closes the connection when it’s done sending, so the client should keep trying to receive until it detects that the connection has closed.

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

Sidebar

Related Questions

I am new to wcf programming and I have been working on a small
So, I'm pretty new to all this network programming, and I have a few
I am new to C++ network programming but have experience with Java sockets etc.
I am working on a network programming and I have this code void WorkHandler::workLoop(){
i am new to network programming in python. I wanted to know that what
This is a continuation of the this question. I am new to network programming,
I am new to network programming but old to Python. I have a simple
Let me state up front, that sockets programming is fairly new to me. Also,
I'm new to Neural Networks, and programming generally. I've written a neural network in
Firstly, I'm relatively new to network programming. I want to intercept and delay HTTP

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.