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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:14:33+00:00 2026-06-12T13:14:33+00:00

Here i am trying to create a simple client and a server using pipes.

  • 0

Here i am trying to create a simple client and a server using pipes. I fork a process to make the child act as a client and parent as the server. Below is the code:

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

void errorMsg(char* msg)
{
    printf("\n%s\n", msg);
//  exit(0);
}

int main()
{
    int servfd[2];
    int clntfd[2];
    int chldpid;    

    if(pipe(servfd) < 0)
        errorMsg("Unable to create server pipe.!!");
    if(pipe(clntfd) < 0)
        errorMsg("Unable to create client pipe.!!!");
        
    if((chldpid = fork()) == 0)
    {
        char* txt = (char*) calloc(256, sizeof(char));
        close(servfd[1]);
        close(clntfd[0]);
        printf("@Client: Enter a string: ");
        //scanf("%s", txt);  //or fgets
        printf("Entered.!!");
        int n;
        txt = "Anything that you want will not be considered no matter what you do!!";
        char txt1[256];
        write(clntfd[1], txt, 256);
        
        //if(txt1[strlen(txt1) - 1] = '\n')
        //{ printf("asdasdas");
        //txt[strlen(txt) - 1] = '\0';}

        //int i = 0;
        //for(i = 0; i < 256; i++)
            //printf("%c", txt1[i]);

    while((n = read(servfd[0], txt1, 256)) > 0)
                    printf("\nAt client: %d bytes read\n\tString: %s\n", n, txt1);
}
    else    
    {
        //printf("Parent:   \n\n");
        close(servfd[0]);
        close(clntfd[1]);
        char* txt = NULL;
        int n, n1;  
        n = read(clntfd[0], &txt, 256);
        printf("Server read: %d", n);
        n1 = write(servfd[1], &txt, 256);
        printf("Server write: %d", n1);
        wait(chldpid);
    }
    exit(0);            
}

Question 1:

This is what is happening. When i run the program, it only prints Anything that yo (exactly 16 chars) and nothing else. When i tried to see the complete contents of txt1 using the for loop shown in comments, i found that there is null value (God knows from where) after yo in txt1. After it there are normal contents as they should be. Any idea why this is happening?

Edit:

The number of bytes read and written, when i try to print them at appropriate places, are all correct. It prints 256 bytes read. However, the size of txt1 by strlen comes out to be ’16’. Also, the program hangs after printing part of the string.

Question 2:

When i try to get a string from user using scanf or fgets also shown in comments, the program terminates as soon as i press enter. No clue about that too as of why that could be happening.

Any insight on the behaviors would be helpful. Sorry for multiple questions. Thanks for your time.! I am using ubuntu 12.04, if that could be of any help.

  • 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-12T13:14:34+00:00Added an answer on June 12, 2026 at 1:14 pm

    I have added various comments and corrections to your code. it now works as intended.
    Your main issue was as pointed out by codeaddict that you did not allocate buffers. I was surprized that you didn’t crash with a SIGSEGV.

    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    void errorMsg(char* msg)
    {
        printf("\n%s\n", msg);
    //  exit(0);
    }
    
    // move this into global space and make it const (non modifiable, easyer to debug)
    const char text_to_send[] = "Anything that you want will not be considered no matter what you do!!";
    
    int main()
    {
        int servfd[2];
        int clntfd[2];
        int chldpid;    
    
        if(pipe(servfd) < 0)
            errorMsg("Unable to create server pipe.!!");
        if(pipe(clntfd) < 0)
            errorMsg("Unable to create client pipe.!!!");
    
        if((chldpid = fork()) == 0)
        {
            char txt[256]; // You have to actually allocate a buffer (aka enough memory to hold your string. You have allocated a pointer to a buffer, but no actual buffer)
            close(servfd[1]);
            close(clntfd[0]);
            printf("@Client: Enter a string: ");
    
            scanf("%s", txt); // since you now actually have a buffer to put the input into this no longer fails
    
            printf("Entered.!!\n");
            int n;
            char txt1[256];
            write(clntfd[1], text_to_send, sizeof(text_to_send)); // write only as much as you actually have to say, not the whole size of your buffer
    
            while((n = read(servfd[0], txt1, 256)) > 0)
                printf("\nAt client: %d bytes read\n\tString: %s\n", n, txt1);
    
            // this is not nessesary at this point, but it is good style to clean up after yourself
            close(servfd[0]);
            close(clntfd[1]);
        }
        else    
        {
            //printf("Parent:   \n\n");
            close(servfd[0]);
            close(clntfd[1]);
            char txt[256]; // same here, you need to actually allocate a buffer.
            int n, n1;  
            n = read(clntfd[0], txt, 256); // read into txt, not &txt. you want to read into your buffer pointed to by txt, not into the part of memory that contains the pointer
            printf("Server read: %d\n", n);
            n1 = write(servfd[1], txt, n); // do not send the whole buffer, just as much as you have actually useful information in it
            printf("Server write: %d\n", n1);
    
            // close the loose file descriptors, else your child will read on them forever
            close(servfd[1]);
            close(clntfd[0]);
    
            int status;
            wait(&status); // this is called like this. if you want to use the pid you call waitpid(chldpid, &status, 0);
        }
        exit(0);            
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a simple SSL client and server in Ruby. But
I'm studying webrat and cucumber and trying to create simple example. Here is my
I am trying to create a simple drawing game like DrawSomething. The problem here
Ok so I am trying create a login script, here I am using PHP5
I'm trying to create a simple WebSocket example using the HTML5/JS API. Based on
I'm trying to create a client-server program that send and receive a string (its
I am trying to write a set of simple client/sever programs that make use
I am trying to develop a very simple client / server where the client
I am trying to create a simple Android client for a web service I
I'm trying to implement a simple FTP client using winsock. I'm having problems trying

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.