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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:38:36+00:00 2026-06-01T01:38:36+00:00

I want to program an application to send a file with sockets: Here my

  • 0

I want to program an application to send a file with sockets:

Here my Server:

   void str_server(int sock)
    {
            char buf[1025];
            const char* filename="test.text";
            FILE *file = fopen(filename, "rb");
            err_abort("Test");
            while (!feof(file))
            {
                int rval = fread(buf, 1, sizeof(buf), file);
                send(sock, buf, rval, 0);
            }
    }

and here my client:

void RecvFile(int sock, const char* filename)
{
    int rval;
    char buf[0x1000];
    FILE *file = fopen(filename, "wb");
    while ((rval = recv(sock, buf, sizeof(buf), 0)) > 0)
    {
        fwrite(buf, 1, rval, file);
    }
    close(sock);
}

My problem is that my client create a file….but dont write the content in the file!

  • 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-01T01:38:37+00:00Added an answer on June 1, 2026 at 1:38 am

    Add some error handling to your code, that should help you track down the problem. Also note that send(), recv(), fread() and fwrite() are not guaranteed to write/read the entire buffer you specify, so you should take that into account as well.

    Also, since TCP is a byte stream, the server needs to indicate when the file ends so the client knows when to stop reading. If you don’t send the file size before sending the actual file, the only option is to close the socket when the transfer is done.

    Try something like this:

    int send_all(int sock, const void *buf, int len)
    {
        const char *pbuf = (const char *) buf;
    
        while (len > 0)
        {
            int sent = send(sock, pbuf, len, 0);
            if (sent < 1)
            {
                // if the socket is non-blocking, then check
                // the socket error for WSAEWOULDBLOCK/EAGAIN
                // (depending on platform) and if true then
                // use select() to wait for a small period of
                // time to see if the socket becomes writable
                // again before failing the transfer...
    
                printf("Can't write to socket");
                return -1;
            }
    
            pbuf += sent;
            len -= sent;
        }
    
        return 0;
    }
    
    void str_server(int sock) 
    { 
        char buf[0x1000]; 
        const char* filename = "test.text";
        struct stat s;
    
        if (stat(filename, &s) == -1)
        {
            printf("Can't get file info"); 
            return;
        }
    
        FILE *file = fopen(filename, "rb"); 
        if (!file)
        {
            printf("Can't open file for reading"); 
            return;
        }
    
        // if you need to handle files > 2GB,
        // be sure to use a 64bit integer, and
        // a host-to-network function that can
        // handle 64bit integers...
        long size = s.st_size;
        long tmp_size = htonl(size);
        if (send_all(sock, &tmp_size, sizeof(tmp_size)) == 0)
        {
            while (size > 0)
            { 
                int rval = fread(buf, 1, min(sizeof(buf), size), file); 
                if (rval < 1)
                {
                    printf("Can't read from file");
                    break;
                }
    
                if (send_all(sock, buf, rval) == -1)
                    break;
    
                size -= rval;
            }
        }
    
        fclose(file);
    } 
    

    int write_all(FILE *file, const void *buf, int len)
    {
        const char *pbuf = (const char *) buf;
    
        while (len > 0)
        {
            int written = fwrite(pbuf, 1, len, file);
            if (written < 1)
            {
                printf("Can't write to file");
                return -1;
            }
    
            pbuf += written;
            len -= written;
        }
    
        return 0;
    }
    
    int read_all(int sock, void *buf, int len)
    {
        char *pbuf = (char *) buf;
        int total = 0;
    
        while (len > 0)
        {
            int rval = recv(sock, pbuf, len, 0);
            if (rval < 0)
            {
                // if the socket is non-blocking, then check
                // the socket error for WSAEWOULDBLOCK/EAGAIN
                // (depending on platform) and if true then
                // use select() to wait for a small period of
                // time to see if the socket becomes readable
                // again before failing the transfer...
    
                printf("Can't read from socket");
                return -1;
            }
    
            if (rval == 0)
            {
                printf("Socket disconnected")
                return 0;
            } 
    
            pbuf += rval;
            len -= rval;
            total += rval;
        }
    
        return total;
    }
    
    void RecvFile(int sock, const char* filename) 
    { 
        int rval; 
        char buf[0x1000];
    
        FILE *file = fopen(filename, "wb"); 
        if (!file)
        {
            printf("Can't open file for writing");
            return;
        }
    
        // if you need to handle files > 2GB,
        // be sure to use a 64bit integer, and
        // a network-to-host function that can
        // handle 64bit integers...
        long size = 0;
        if (read_all(sock, &size, sizeof(size)) == 1)
        {
            size = ntohl(size);
            while (size > 0)
            {
                rval = read_all(sock, buf, min(sizeof(buf), size));
                if (rval < 1)
                    break;
    
                if (write_all(file, buf, rval) == -1)
                    break;
            } 
        }
    
        fclose(file); 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application on a server and I want it to send a
I want to program an Add-Window to my application, much like the Add Contact-window
I want to implement program by c# application. first of all I need web
I developed a WCF application, and I want to package it in a program
I need to change how my C# application connects to a server to send
I have a Java program and I want to send a command from my
I want to send an embedded email from my application. I have it sending
I want to make the most simplest application that can communicate via windows send
i have develop a eblast application The program is use to send a email
I'm writing a program in vb 6. I want to send data to my

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.