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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:15:57+00:00 2026-05-27T16:15:57+00:00

So I programmed a multi threaded web server, here is one function from the

  • 0

So I programmed a multi threaded web server, here is one function from the program. This function takes output file descriptor (fd), content type, pointer to data to be served (*buf) and size of the data (numbytes). It always gets stuck at 5775 bytes! I’ve tried using write() instead of send(), but no avail! I tried to send whole buf at a time, and even tried to transfer it in chunks, but wget shows that it gets stck at 5775 bytes! Here is the code:

int return_result(int fd, char *content_type, char *buf, int numbytes)
{
    char out_buf[BUF_SIZE], numb[6];
    int buf_len, total = 0, buf_size;
    long int i = 0;
    sprintf(numb, "%d", numbytes);
    strcpy(out_buf, "HTTP/1.1 200 OK \nContent-Type: ");
    strcat(out_buf, content_type);
    strcat(out_buf, "\nContent-Length: ");
    strcat(out_buf, numb);
    strcat(out_buf, "\nConnection: Close\n  \n");
    printf("\nSending HTTP Header\n %d bytes sent!",
           send(fd, out_buf, strlen(out_buf), 0));
    char *start = NULL, *str = NULL, *temp = NULL;
    start = buf;
    printf("\n Start Pointer Val = %ld", &start);
    while (start != NULL) {
        printf("\n While Loop");
        if (i + 2048 * sizeof(char) < numbytes) {
            printf("\n If 1");
            str = (char *)malloc(sizeof(char) * 2048);
            memcpy(str, start, sizeof(char) * 2048);
            i = i + 2048 * sizeof(char);
            buf_size = send(fd, str, 2048, 0);
            free(str);
            printf("\n Sent %d bytes total : %d", buf_size, total =
                   total + buf_size);

            temp = start + sizeof(char) * 2048;
            start = temp;

        } else {

            i = numbytes - i * sizeof(char);
            if (i > 0) {
                printf("\n If 2");
                printf("\n Value of i %d", i);
                str = (char *)malloc(sizeof(char) * i);
                memcpy(str, start, sizeof(char) * i);
                printf("Total bytes finally sent:%d", total =
                       total + send(fd, str, i, 0));
                if (total == numbytes) {
                    printf("\nTransfer Complete!");
                }
                free(str);

            }
            start = NULL;
        }
    }
    printf("out of loop!");
    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-27T16:15:57+00:00Added an answer on May 27, 2026 at 4:15 pm

    Try something like this (printf() statements omitted for clarity):

    int send_buf(in fd, void *buf, int numbytes)
    {
        char *start = (char*) buf;
        while (numbytes > 0)
        {
            int sent = send(fd, start, numbytes, 0);
            if (sent <= 0)
            {
                if ((sent == -1) && (errno == EAGAIN))
                {
                    fd_set wfds;
                    FD_ZERO(&wfds);
                    FD_SET(fd, &wfds);
                    if (select(fd + 1, NULL, &wfds, NULL, NULL) == 1)
                        continue;
                }
                return -1;
            }
    
            start += sent;
            numbytes -= sent;
        }
    
        return 0;
    }
    
    int return_result(int fd, char *content_type, void *buf, int numbytes) 
    { 
        char out_buf[BUF_SIZE], 
    
        int len = sprintf(out_buf,
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: %s\r\n"
            "Content-Length: %d\r\n"
            "Connection: Close\r\n"
            "\r\n",
            content_type,
            numb); 
    
        if (send_buf(fd, out_buf, len) != 0)
            return -1;
    
        if (send_buf(fd, buf, numbytes) != 0)
            return -1;
    
        return 0; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: I've written a multi-threaded application in Win32, which I start from C# code
Having programmed through emacs and vi for years and years at this point, I
Can any one tell me that why delay happen when I run this script
These days there are two main hardware environments for parallel programming, one is multi-threading
I have recently heard about the Web Workers spec that defines API for multi-threading
i programmed a Chat-Program like ICQ with C# and WCF. Now i want to
In every platform there are various versions of a given library: multi-threaded, debug, dynamic,
MSDN on migrating legacy multithreaded applications (from this page on exception handling in threads):
According to this tutorial asynchronous disk file io can easily be achieved using AIO
I programmed a bluetooth Television-Remote control for cellphones in J2ME using javax.microedition.lcdui.* (Gauge, List,

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.