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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:01:36+00:00 2026-05-14T17:01:36+00:00

I want to ask, why I cannot transfer file from server to client? When

  • 0

I want to ask, why I cannot transfer file from server to client?
When I start to send the file from server, the client side program will have problem.
So, I spend some times to check the code,
But I still cannot find out the problem
Can anyone point out the problem for me?

CLIENTFILE.C

#include stdio.h
#include stdlib.h
#include time.h
#include netinet/in.h
#include fcntl.h
#include sys/types.h
#include string.h
#include stdarg.h
#define PORT 5678
#define MLEN 1000
int main(int argc, char *argv [])
{

        int sockfd;
        int number,message;
        char outbuff[MLEN],inbuff[MLEN];
        //char PWD_buffer[_MAX_PATH];
        struct sockaddr_in servaddr;
        FILE *fp;
        int numbytes;  
        char buf[2048];



        if (argc != 2)
                fprintf(stderr, "error");

        if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
                fprintf(stderr, "socket error");

        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(PORT);

        if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
                fprintf(stderr, "connect error");

        if ( (fp = fopen("/home/na/nall9047/write.txt", "w")) == NULL){
                perror("fopen");
                exit(1);
        }
        printf("Still NO PROBLEM!\n");

        //Receive file from server
        while(1){
                numbytes = read(sockfd, buf, sizeof(buf));
                printf("read %d bytes, ", numbytes);

                if(numbytes == 0){
                        printf("\n");
                        break;
                }
                numbytes = fwrite(buf, sizeof(char), numbytes, fp);
                printf("fwrite %d bytes\n", numbytes);
        }

        fclose(fp);
        close(sockfd); 
        return 0;
}

SERVERFILE.C

#include stdio.h
#include fcntl.h
#include stdlib.h
#include time.h
#include string.h
#include netinet/in.h
#include errno.h
#include sys/types.h
#include sys/socket.h
#includ estdarg.h
#define PORT 5678
#define MLEN 1000
int main(int argc, char *argv [])
{

        int listenfd, connfd;
        int number, message, numbytes;
        int h, i, j, alen;
        int nread;
        struct sockaddr_in servaddr; 
        struct sockaddr_in cliaddr;
        FILE *in_file, *out_file, *fp;
        char buf[4096];



        listenfd = socket(AF_INET, SOCK_STREAM, 0);
        if (listenfd < 0)
                 fprintf(stderr,"listen error") ;

        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family      = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port        = htons(PORT);

        if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
                fprintf(stderr,"bind error") ;



        alen = sizeof(struct sockaddr);
        connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &alen);

        if (connfd < 0)
                fprintf(stderr,"error connecting") ;

        printf("accept one client from %s!\n", inet_ntoa(cliaddr.sin_addr));

        fp = fopen ("/home/na/nall9047/read.txt", "r"); // open file stored in server

        if (fp == NULL) {
                printf("\nfile NOT exist");
        }

        //Sending file
        while(!feof(fp)){

                numbytes = fread(buf, sizeof(char), sizeof(buf), fp);
                printf("fread %d bytes, ", numbytes);
                numbytes = write(connfd, buf, numbytes);
                printf("Sending %d bytes\n",numbytes);
        }

        fclose (fp);    
        close(listenfd);
        close(connfd);
        return 0;
}
  • 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-14T17:01:37+00:00Added an answer on May 14, 2026 at 5:01 pm

    A couple of problems i see:

    • You’re read()ing and write()ing a socket handle. Works on some OS’s, but not others. You’ll want to recv() and send() instead if you care at all about portability (or if your OS is one of the ones read/write breaks on).

    • You’re close()ing a socket handle without shutdown()ing it first. That could well cause the last bytes sent to be discarded.

    • fread is a function, not a variable. You probably want your loop to look like

      while (!feof(fp)) {

    UPDATE: Where are you setting the address to connect to?

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What you have is a maximum weighted bipartite matching problem:… May 14, 2026 at 6:28 pm
  • Editorial Team
    Editorial Team added an answer There is no such thing as "property assignment" in Ruby.… May 14, 2026 at 6:28 pm
  • Editorial Team
    Editorial Team added an answer Again with the disclaimer that my experience of Inform 7… May 14, 2026 at 6:28 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

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.