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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:29:01+00:00 2026-05-27T06:29:01+00:00

I’m writing a simple server for my networks class and I am having trouble

  • 0

I’m writing a simple server for my networks class and I am having trouble getting data transferred to the client (provided by the prof) correctly.

Once I get through all the setup and the connection is established I start reading chunks of the file and writing them to the socket checking to see that the return from the read and write functions match. I’m also keeping a running total of the bytes that are read/written and comparing that to the total file size (via stat.st_size) and they all match up.

No matter how many times I request the same file the log on the server side always has the correct metrics. The client sporadically loses the end of the file. The difference between the actual and expected size is almost never the same from one invocation to the next and it appears to always be the end of the file that’s missing, no pieces from the middle. The size of the arrived file is also a multiple of 512 (the chunk size).

So, it seems that some number of whole chunks are making it and then the rest are getting lost somehow.:w

#define CHUNK_SIZE     512
/* other definitions */

int main()
{
   /* basic server setup: socket(), bind(), listen() ...  
      variable declarations and other setup  */

   while(1)
   {
      int cliSock = accept(srvSock, NULL, NULL);
      if(cliSock < 0)
         ; /* handle error */

      read(cliSock, filename, FILE_NAME_SIZE - 1);
      int reqFile = open(filename, O_RDONLY);
      if( reqFile == -1)
         ; /* handle error */

      struct stat fileStat;
      fstat(reqFile, &fileStat);
      int fileSize = fileStat.st_size;

      int bytesRead, totalBytesRead = 0;
      char chunk[CHUNK_SIZE];
      while((bytesRead = read(reqFile, chunk, CHUNK_SIZE)) > 0)
      {
         totalBytesRead += byteasRead;
         if(write(cliSock, chunk, bytesRead) != bytesRead)
         {
            /* perror(...) */
            /* print an error to the log file */
            bytesRead = -1;
            break;
         }
      }
      if (bytesRead == -1)
      {
         /* perror(...) */
         /* print an error to the log file */
         close(cliSock);
         continue;
      }

      /* more code to write transfer metrics etc to the log file */
   }
}

All of the removed error handling code is some flavor of printing an error message to the log file and getting back to the top of the loop.


Edit flipped a < that should have been >

  • 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-27T06:29:02+00:00Added an answer on May 27, 2026 at 6:29 am

    Presumably you are unceremoniously closing the socket with close() when you have written all the data you wanted to the socket (or perhaps just exiting the process, which does the same thing).

    This is not right – if the other side has sent some data that you haven’t read1, the connection will be reset. The reset can cause unread data to be lost.

    Instead, you should use shutdown() to gracefully shutdown the writing side of the socket, then wait for the client to close. Something like:

    ssize_t bytesRead;
    char chunk[CHUNK_SIZE];
    
    shutdown(cliSock, SHUT_WR);
    
    while((bytesRead = read(cliSock, chunk, CHUNK_SIZE)) != 0)
    {
        if (bytesRead < 0)
        {
            if (errno != EINTR)
            {
                /* perror() */
                /* print error in log file */
                break;
            }
        }
        else
        {
            /* maybe log data from client */
        }
    }
    
    close(cliSock);
    

    1. This can include EOF, if the other side has closed its writing channel.

    • 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
I am doing a simple coin flipping experiment for class that involves flipping a
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
I am writing an app with both english and french support. The app requests

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.