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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:18:52+00:00 2026-05-13T12:18:52+00:00

I am building a server-client program with c and having some problen wtih sendina

  • 0

I am building a server-client program with c and having some problen wtih sendina a whole file with send() and recv() function. I need to send the whole file with these but all these functions can do is send a string. The main problem is with sending the exe or other binary formet data. Should I use htons or htonl func? I dunno how to use those along these as I am merely a noob.

I am stuck with this and love your help. Thanks!!

Here is the server side recv function ::

if (socket_type != SOCK_DGRAM)
    {

            fi = fopen (final,"wb");
            retval = recv(msgsock, recv_buf, strlen(recv_buf), 0);
            /*recv_buf[retval] = '\0';
            fprintf (fi,"%s",recv_buf);*/

            int i;
            i=atoi(recv_buf);
            char *q;
            q=(char *)malloc(i*sizeof(char));
            retval = recv(msgsock, q, strlen(q), 0);
            //printf ("%s",q);
            fwrite(q,i,1,fi);
            fclose(fi);

    }
    else
    {
        retval = recvfrom(msgsock,recv_buf, sizeof(recv_buf), 0, (struct sockaddr *)&from, &fromlen);
        printf("Server: Received datagram from %s\n", inet_ntoa(from.sin_addr));
        printf ("SOCK_DGRAM");
    }

    if (retval == SOCKET_ERROR)
    {
        fprintf(stderr,"Server: recv() failed: error %d\n", WSAGetLastError());
        closesocket(msgsock);
        //continue;
    }
    else
        printf("Server: recv() is OK.\n");

    if (retval == 0)
    {
        printf("Server: Client closed connection.\n");
        closesocket(msgsock);
            //continue;
    }
    printf("Server: Received %d bytes, data from client\n", retval);

The client side sending function :::

void send_command()
{
int bytesent;
FILE *file_out;
//file_out = fopen(file_path,”rb”);
char str_all[100000];//flag [30]=”end”;

///////////////////////getsize//////////////
char fsize[5];
int filesize;
file_out = fopen(file_path, "rb");
fseek(file_out, 0, SEEK_END);
filesize = ftell(file_out);
rewind (file_out);
itoa (filesize,fsize,10);
/////////////////////////////////////////////
send (ConnectSocket, fsize, strlen (fsize), 0);

char *r = (char *)malloc (filesize * sizeof(char));

fread(r,filesize,1,file_out);
bytesent = send( ConnectSocket, r, strlen(r), 0 );
printf("\nClient: Bytes sent: %ld\n", bytesent);
fclose (file_out);

/*while (fscanf(file_out,"%s",&str_all) != EOF)
{
    bytesent = send( ConnectSocket, str_all, strlen(str_all), 0 );
    printf("\nClient: Bytes sent: %ld\n", bytesent);
    //Sleep(500);
}*/

/*printf("%s",flag);
send( ConnectSocket, flag, strlen(flag), 0 );*/
WSACleanup();
//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-13T12:18:52+00:00Added an answer on May 13, 2026 at 12:18 pm

    You’ll need to implement that functionality yourself, or use a library that does so. send(2) just sends a raw byte buffer. If you want to send a file, you’ll need to use some sort of protocol that both the client and the server understand.

    The simplest possible protocol might be to have the client send the filename, the length of the file, and then the raw file data. For example (error checking omitted for clarity):

    // Server:
    const char *filename = ...;
    uint32_t filenameLen = strlen(filename);
    uint32_t filenameLenNet = htonl(filenameLen);
    send(fd, &filenameLenNet, sizeof(filenameLenNet), 0);
    send(fd, filename, filenameLen, 0);
    
    // You should probably use a 64-bit file length; also, for large files, you
    // don't want to read the entire file into memory, you should just stream it
    // from disk to network in reasonably-sized chunks.
    const void *fileData = ...;
    uint32_t fileLen = ...;
    uint32_t fileLenNet = htonl(fileLen);
    send(fd, &fileLenNet, sizeof(fileLenNet), 0);
    send(fd, fileData, fileLen, 0);
    
    // Client:
    char *filename;
    uint32_t filenameLen;
    recv(fd, &filenameLen, sizeof(filenameLen), 0);
    filenameLen = ntohl(filenameLen);
    filename = malloc(filenameLen + 1);
    recv(fd, filename, filenameLen, 0);
    filename[filenameLen] = 0;
    uint32_t fileLen;
    recv(fd, &fileLen, sizeof(fileLen), 0);
    fileLen = ntohl(fileLen);
    void *fileData = malloc(fileLen);
    recv(fd, fileData, fileLen, 0);
    // Write file Data to the output file.  Again, for large files, it would be
    // better to stream it in in chunks instead of reading it all in at once.
    

    Also, be careful about partial sends/receives. Make sure you check the return values from send and recv, since they can (and do) often only send or receive a subset of the data you intent to send or receive. When that happens, you have to recover by resending or rereceiving in a loop until you’ve processed all the data you intended or an error occurs.

    As an alternative to using your own protocol, you can use an existing protocol with a library such as libftp or libcurl.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • 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 The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

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.