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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:23:00+00:00 2026-06-13T13:23:00+00:00

I am trying to write a client/server program in C for a homework assignment.

  • 0

I am trying to write a client/server program in C for a homework assignment. What should happen is:
Connection is made
Server waits for client to send a message
client asks user for a message, assigns it to a string, and then sends it to the server. This is done in the following code on the client:

while(1){
/* prepare message */
    printf ("Please enter the message: ");
    bzero (buffer, BUFFERLENGTH); //BUFFERLENGTH is defined as 256
    fgets (buffer, BUFFERLENGTH, stdin);
/* send message */
    n = write (sockfd, buffer, strlen(buffer));
    if (n < 0) 
         error ("ERROR writing to socket");
    bzero (buffer, BUFFERLENGTH);
}

now, my problem is that this code is sending a bit of gibberish. As far as I can tell, buffer is not being emptied, or is not always BUFFERLENGTH long, so is random junk is being read from the stack?

The server receives the message and then prints it. If the client program scans in "actual string I input" then the server will output something similar to:

actual string I input @&^%~& although the terminal output is more full of nonprintable characters, and also line breaks.
Code for the server is:

n = read (*newsockfd, buffer, BUFFERLENGTH -1);
printf("buffer = %s\n",buffer);
if (n < 0) 
    error ("ERROR reading from socket");

This does not happen when I remove the while loop though!

I’ve read about bzero and I know it’s deprecated, and I should use memset; however I tried doing that, with: memset(buffer,0,BUFFERLENGTH); and the problem still happens.

Any explanation of why buffer has gibberish appended to it?


Edit

buffer is declared in the client by: char buffer[BUFFERLENGTH];
`

  • 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-13T13:23:01+00:00Added an answer on June 13, 2026 at 1:23 pm

    printf("%s", ...) expects a 0-terninated string.

    So you should append a 0 to the end of the buffer you received.

    Update:

    Assuming buffer is defined like:

    char buffer[BUFFERLENGTH];
    

    To add a 0 to the last array entry do like so:

    buffer[BUFFERLENGTH - 1] = 0;
    

    A more generic approach whould (although more costly) be to zero out the whole receive buffer prior to reading into it:

    memset(buffer, 0, BUFFERLENGTH);
    n = read (*newsockfd, buffer, BUFFERLENGTH - 1);
    if (n < 0) 
      error ("ERROR reading from socket");
    else
      printf("buffer = %s\n", buffer);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a client/server program with threads. I close the socket once
I'm trying to write a basic client/server echo program, to test the usage of
I'm trying to write a server-client socket program in C. The objective is for
I'm trying to write a simple server-client program, but I have a problem: I
I'm trying to write a server program in C, using another client, I get
I'm trying to write a small client server program. The Server is in C#,
I am trying to write a client-server app. Basically, there is a Master program
i'm trying to write simple tcp\ip client-server. here is server code: internal class Program
I am trying to write a basic client-server program to allow the client to
I'm trying to write a client-server application in Java with an XML-based protocol. But

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.