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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:58:45+00:00 2026-05-29T08:58:45+00:00

I am just learning socket programming and I am trying to write an echo

  • 0

I am just learning socket programming and I am trying to write an echo client that reads from stdin and writes to a socket and then reads the server response from the socket to stdout. The problem is that I don’t know how long stdin will be or how long the server’s response will be. The code I am trying to use is as follows (creating the socket and connecting to the server are left out):

length = BUF_SIZE;
while (length == BUF_SIZE) {  // length will equal BUF_SIZE if buf is full, when length < BUF_SIZE we have reached an EOF                                 
   // Reads from STDIN to buf                                                                                                                              
   if ((length = read(STDIN_FILENO, buf, BUF_SIZE)) < 0){
     fprintf(stderr, "Error in reading from STDIN");
     return 4;
   }
   // Writes from buf to the socket                                                                                                                        
   if ((write(sock, buf, BUF_SIZE)) < 0){
     fprintf(stderr, "Error writing to socket");
     return 5;
   }
 }

if ((status = shutdown(sock, 1)) < 0){ // Shuts down socket from doing more receives                                                                      
   fprintf(stderr, "Error shutting down socket for writing");
   return 6;
}

length = BUF_SIZE; 
while (length == BUF_SIZE){
   // Read from socket to buf                                                                                                                              
   if ((length = read(sock, buf, BUF_SIZE)) < 0){
     fprintf(stderr, "Error reading from socket");
     return 7;
   }
   // Write from buf to STDOUT                                                                                                                             
   if ((write(STDOUT_FILENO, buf, BUF_SIZE)) < 0){
     fprintf(stderr, "Error writing to STDOUT");
     return 8;
   }
}

close(sock);
exit(0);

BUF_SIZE is defined as 100. When I run my program the program typically connects to the server and sends the proper message, but what it writes to stdout is either nothing or gibberish.

What am I doing wrong?

  • 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-29T08:58:46+00:00Added an answer on May 29, 2026 at 8:58 am

    Your while loop is only going to work the first time through. read()/write() are only going to return the amount that they actually read/write which may well not be equal to BUF_SIZE. Let’s say you read ten bytes from the socket and then you write a hundred to stdout – the last 90 are going to be garbage.

    Something along these lines will get you somewhat closer to what you want.

    while (1)
    {
        if ((length = read(STDIN_FILENO, buf, BUF_SIZE)) < 0)
        {
            fprintf(stderr, "Error in reading from STDIN");
            return 4;
        }
    
        if ((write(sock, buf, length)) < 0)
        {
            fprintf(stderr, "Error writing to socket");
            return 5;
        }
    
        if ((length = read(sock, buf, BUF_SIZE)) < 0)
        {
            fprintf(stderr, "Error reading from socket");
            return 7;
        }
    
        if ((write(STDOUT_FILENO, buf, length)) < 0)
        {
            fprintf(stderr, "Error writing to STDOUT");
            return 8;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just learning the basics of socket programming for C++, but I've heard people
I have just started learning about socket programming and learned about winsock and achieved
I'm learning C# asynchronous socket programming, and I've learned that it's a good idea
Im just learning mod_rewrite and regex stuff, and what I'm trying to do is
I'm just learning asp.net mvc and I'm trying to figure out how to move
I am just learning python and Qt these days. So please consider that this
Just learning CakePHP and its Bake feature. I tried to run ../cake/console/cake bake from
I just began learning network programming. My first program is very simple: once connected,
just learning as3 for flex. i am trying to do this: var someNumber:String =
Just learning about mercurial's --style and --template options that can be used on hg

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.