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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:02:30+00:00 2026-05-16T03:02:30+00:00

I’m doing client/server interaction with sockets in C. What I’m trying to do is

  • 0

I’m doing client/server interaction with sockets in C. What I’m trying to do is have the client request a file to be read on the server, the server return a buffer of the file contents, and the client print out the file.

Though I was able to accomplish the server sending a buffer of a file to the client & the client printing it out successfully, I can’t seem to get the server to successfully read a filename sent by the client.

Here is what I mean:

///This is the structure of the message that gets sent back and forth
struct message { 
int code; //Just indicates 1 or 2 for readfile or writefile
int size;
char buffer[256]; //This will hold the filename when client sends readfile request
 };

Works:

char *filename = "test.c";
infile = open(filename, O_RDONLY);
//Send the file and everything back to the client

Doesn’t Work:

while( read(sockfd, &msg, sizeof(int) * 2) > 0) {
        if(msg.code == 1) { //Perform a read operation
            int infile, filesize, rr;
            int length;
            char output[256];
            size_t rb = 0;

            while(rb < msg.size) { 
                ssize_t r = read(sockfd, msg.buffer, msg.size - rb);
                if(r < 0) { 
                    error(sockfd, r, msg.buffer);
                    break;
                }
                rb += r;
            }

            msg.buffer[rb] = '\0';  
         //This has been printing out the correct amount
            printf("\nBytes read: %i", rb); 
         //This has also been printing out properly
            printf("\nmsg.buffer: %s", msg.buffer); 

            infile = open(msg.buffer, O_RDONLY);

I’ve edited to show the current state my program is in, and is still not working. Before, I had a strcpy that was improperly placed.

  • 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-16T03:02:30+00:00Added an answer on May 16, 2026 at 3:02 am

    Well I’ve about found out the answer, or at least something close to it. First off, a line break character was getting sent. On the client side, I used fgets(msg.buffer, 256, stdin); to get the message. When I checked the value using printf("File |%s|", msg.buffer), I saw that the second bar was on the next line. I took that line break and overwrote it with a null character at the end.

    I also changed to fopen and freads/writes. I added ./ to the beginning of the file statement but I doubt that’s needed.. it was just one of my earlier attempts. It now looks something like this:

                size_t rb = 0;
                FILE* file;
                char filename[256];
                while(rb < msg.size) { 
                    ssize_t r = read(sockfd, filename, msg.size - rb);
                    if(r < 0) { 
                        error(sockfd, r, msg.buffer);
                        goto end;
                    }
                    rb += r;
                }
                if(strlen(filename) > 253) { 
                    error(sockfd, rb, msg.buffer);
                    goto end;
                }
                strcpy(msg.buffer, "./");
                strcat(msg.buffer, filename);
                msg.buffer[rb + 1] = '\0';
    
                file = fopen(msg.buffer, "r");
                if(file == NULL) {
                    error(sockfd, rb, msg.buffer);
                    printf("Error opening file %s: %s\n", msg.buffer,strerror(errno));
                    fflush(stdout);
                    goto end;
                }
    

    Thanks to all who helped. I learned a lot (gdb was especially useful — I never used it before)

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

Sidebar

Ask A Question

Stats

  • Questions 498k
  • Answers 498k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer May I suggest using Content Management Systems (CMS) that support… May 16, 2026 at 12:08 pm
  • Editorial Team
    Editorial Team added an answer Have a look at django-roa. From the sound of it,… May 16, 2026 at 12:08 pm
  • Editorial Team
    Editorial Team added an answer unable to negotiate a content type? May 16, 2026 at 12:08 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, 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.