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

  • Home
  • SEARCH
  • 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 8013603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:40:32+00:00 2026-06-04T19:40:32+00:00

This long code is sending a POST request using a Socket, the whole code

  • 0

This long code is sending a POST request using a Socket, the whole code works without any complain, but what i’m facing right now is, that it eating a lot of cpu power, uses too much memory (RAM)

I can see that, because my laptop is getting hot very fast and by looking checking at my mac.

i have tried to find the bug or where , that very large memory Issue is, but couldn’t. I have spent over a month trying to fix it by my own, but don’t know what i’m doing to be honest.

i was so desperate, that i putted all kind of freeing metod.. even that it wrong.. just to see if that made a change, but nothing did..

So now i don’t know what wrong or how to fix this, please help me out…

Going update the code with select, moment… trying to clean it up first

  • 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-04T19:40:33+00:00Added an answer on June 4, 2026 at 7:40 pm

    First thing: do NOT mix malloc() and delete[]. They might or might not refer to the same memory allocator. So use the malloc()/free() or new char[]/delete[] pairs.

    The problems is here (in Database() function): you’ve got a terrible memleak. Do not allocate memory like this for result passing. Better use buffers. You program is multithreaded so use the buffers on the stack. Do NOT call delete[] on anything that is not allocated by you (var declaration like “char Buf[100];” is not an allocation).

    New version (I omitted main() and strip() functions. Also the includes):

    #define MAX_ECHO_SIZE (1024)
    #define MAX_RESPONSE_SIZE (1024)
    
    void process_http(int sockfd, const char *host, const char *page, const char *poststr, char* OutResponse)
     {
        char* ptr;
        char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
        ssize_t n;
    
        snprintf(sendline, MAXSUB, 
             "POST %s HTTP/1.0\r\n"  // POST or GET, both tested and works. Both HTTP 1.0 HTTP 1.1 works, but sometimes 
             "Host: %s\r\n"     //oth HTTP 1.0 HTTP 1.1 works, but sometimes HTTP 1.0 works better in localhost type
             "Content-type: application/x-www-form-urlencoded\r\n"
             "Content-length: %d\r\n\r\n"
             "%s\r\n", page, host, (unsigned int)strlen(poststr), poststr);
    
        if (write(sockfd, sendline, strlen(sendline))>= 0) 
        {
            while ((n = read(sockfd, recvline, MAXLINE)) > 0) 
            {
                recvline[n] = '\0';
    
                if(fputs(recvline,stdout) ==EOF) { cout << ("fputs erros"); }
                ptr = strstr(recvline, "\r\n\r\n");
                strip(ptr, "\r\n\r\n");
    
                // check len for OutResponse here ?
                snprintf(OutResponse, 6000,"%s", ptr);
            }
        }
    }
    
    int Database( const char * hname,  const char * page,  const char * var, const char * poststr,  int  port, char* EchoResponse, int MaxEchoLen){
    
        char url[MAXLINE];
        char response[MAX_RESPONSE_SIZE];
    
        snprintf(url, MAXLINE, "%s=%s", var, poststr);
    
        short int sockfd ;
        struct sockaddr_in servaddr;
        struct hostent *hptr;
        char str[MAXLINE];
        char** pptr;
    
        hptr = gethostbyname(hname);
    
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
        if (!hptr) {
            cout << ("host not found\n");
            return -1; // "host not found";
        }
    
        if (hptr->h_addrtype == AF_INET && (pptr = hptr->h_addr_list) != NULL) {
            inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)); 
        }
        if (sockfd  >= 0  ) {
            bzero(&servaddr, sizeof(servaddr));
            servaddr.sin_family = AF_INET;
            servaddr.sin_port = htons(port);
            inet_pton(AF_INET, str, &servaddr.sin_addr);
    
            if (connect(sockfd, (SA *) & servaddr, sizeof(servaddr)) < 0) {
                return -2; // "Server down, connect error";
            }
            else {
                process_http(sockfd, hname, page, url, &response[0], MAX_RESPONSE_SIZE);
    
                int len = strlen(response)+1;
                if(len >= MaxEchoLen) { return -3; /* buffer too small*/ }
    
                // Copy the contents with
                strcpy(EchoResponse, response);
    
                /// You must not free all of char[] allocated on stack
                close(sockfd);
    
                return 0; // OK
            }
        }
    }
    
    void *multithreading1( void *ptr ) {
        char LocalEchoResponse[MAX_ECHO_SIZE];
    
        while (1) {
                int RetCode = Database("2.107.xx.xxx", "/ajax.php", "submit", "HEllo WORLD", 80, &LocalEchoResponse[0], MAX_ECHO_SIZE);
                /// check the error
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to measure time memory allocation using this code: long AForMemory = DateTime.Now.Ticks;
Sorry for this long post. The question is however small but requires full detail.
In this code long timestamp=1332782463298; Date d=new Date(timestamp); date=d.toLocaleString(); date is always current date.
How can I improve this code? What has made this long winded is the
Firstly sorry for the long piece of code pasted below. This is my first
Why is this code throwing up a SIGSEGV : int main() { unsigned long
I have found this code for reverse geocoding: var point = new GLatLng (lat[1],long[1]);
my code is like this int dateColumn = c.getColumnIndex(android.provider.CallLog.Calls.DATE); long callDate = c.getInt(dateColumn); SimpleDateFormat
I have this in my code echo $result['arr']['sub'].\t\t\t\t.$result['arr2']['sub2']; I am sending it through headers
I am sending an AJAX request using javascript to a servlet. The servlet is

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.