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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:30:26+00:00 2026-05-21T17:30:26+00:00

I’m quite new to C and am having trouble with writing to streams using

  • 0

I’m quite new to C and am having trouble with writing to streams using sockets.

I’m using a network stream/socket to try to send messages in two directions (from host to client and client to host) however when I get the message back from the client it’s scrambled (due to an encoding issue I guess?).

Here’s the relevant parts of my code:

host.c

int client_sock_fd;

int main(int argc, const char* argv[]) {
    while (1) {
        char input[80];
        scanf("%s", input);
        // send to client, transmitted fine
        write(client_sock_fd, &input, sizeof(input));             

        char response[80];
        read(client_sock_fd, &response, sizeof(response));
        // Prints the missing character symbol, and 'random' letters
        printf("Response: %s\n", response);
    }
}

client.c

int host_sock_fd; // file descriptor for host

void writeResponse(

int main(int argc, const char* argv[]) {
    while (1) {
        char message[80];
        // the message is transmitted fine
        read(host_sock_fd, &message, sizeof(message));

        writeResponse("Response message");
    }
}

void writeResponse(char response[80]) {
    write(host_sock_fd, &response, sizeof(response));
}

I’m not really sure how to continue. I know if I simply write back the received message in client it works fine, so is this a problem with the amount of memory for each char?

  • 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-21T17:30:27+00:00Added an answer on May 21, 2026 at 5:30 pm

    I showed a lack of knowledge below, and I’m not going to delete it, but this is what causeing your problem in writeResponse(char response[80]) since response is treated as char * and not char[80]. But in general, don’t use the & when it is not needed, it causes mistakes just as the one you had. treat char[] as char * where it is about the level of referencing, there are differences between those two, but not in this case.

    It’s weird that it works at all, since you declared your buffers as char input[80]; etc., those variables are already char pointers and you should not pass their address as parameter to the read /write functions so it should be:

    write(client_sock_fd, input, sizeof(input)); 
    

    instead of:

    write(client_sock_fd, &input, sizeof(input)); 
    

    in all cases of read write. You may want to take a look at those examples

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.