1

I've tested this code for sending an HTTP request and receiving a response.

#include <stdio.h> /* printf, sprintf */
#include <stdlib.h> /* read, write, close */
#include <string.h> /* memcpy, memset */
#include <sys/socket.h> /* socket, connect */
#include <netinet/in.h> /* struct sockaddr_in, struct sockaddr */
#include <netdb.h> /* struct hostent, gethostbyname */

void error(const char *msg) { perror(msg); exit(0); }

int main(int argc,char *argv[])
{
    /* first what are we going to send and where are we going to send it? */
    int portno =        80;
    char *host =        "api.somesite.com";
    char *message_fmt = "POST /apikey=%s&command=%s HTTP/1.0\n\n";

    struct hostent *server;
    struct sockaddr_in serv_addr;
    int sockfd, bytes, sent, received, total;
    char message[1024],response[4096];

    if (argc < 3) { puts("Parameters: <apikey> <command>"); exit(0); }

    /* fill in the parameters */
    sprintf(message,message_fmt,argv[1],argv[2]);
    printf("Request:\n%s\n",message);

    /* create the socket */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) error("ERROR opening socket");

    /* lookup the ip address */
    server = gethostbyname(host);
    if (server == NULL) error("ERROR, no such host");

    /* fill in the structure */
    memset(&serv_addr,0,sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(portno);
    memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length);

    /* connect the socket */
    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
        error("ERROR connecting");

    /* send the request */
    total = strlen(message);
    sent = 0;
    do {
        bytes = write(sockfd,message+sent,total-sent);
        if (bytes < 0)
            error("ERROR writing message to socket");
        if (bytes == 0)
            break;
        sent+=bytes;
    } while (sent < total);

    /* receive the response */
    memset(response,0,sizeof(response));
    total = sizeof(response)-1;
    received = 0;
    do {
        bytes = read(sockfd,response+received,total-received);
        if (bytes < 0)
            error("ERROR reading response from socket");
        if (bytes == 0)
            break;
        received+=bytes;
    } while (received < total);

    if (received == total)
        error("ERROR storing complete response from socket");

    /* close the socket */
    close(sockfd);

    /* process response */
    printf("Response:\n%s\n",response);

    return 0;
}

source : Simple C example of doing an HTTP POST and consuming the response

I have compiled this, with GCC 4.7.3 (for DOS + DPMI) this worked perfectly, I got a Web file, post, etc.... , but at the end, I received SIGTRAP signal (Breakpoint).

I don't have other threads, I have not placed a breakpoint, only this main() process on DOS

I've GDBed this code, and I have this :

enter image description here

This is pcpkc.c [ln.1070]

and PROFILE_START() function in timer.c [ln.765]

Can anyone help me why this signal is here?

Community
  • 1
  • 1
  • `total = sizeof(response)-1;` How do you know that is the size of the message you are going to receive? – joop Jul 28 '15 at 15:05
  • Olaf : Excuse me, it's in C language (GCC 4.7.3) joop : I study this code, the author is Jerry Jeremiah, it's a example for send and receive http data (source:http://stackoverflow.com/questions/22077802/simple-c-example-of-doing-an-http-post-and-consuming-the-response). But I think that 'response' is a char with 4096 bytes allocated, -1 for no take account of descriptor [0]. And after, read() put datas on 'response' – Sébastien Favier Jul 28 '15 at 20:47
  • (Correction : for sending an HTTP request and receiving a response. * ) – Sébastien Favier Jul 28 '15 at 21:13
  • Are these files (pcpkc.c and timer.c) your code, or library code? – Useless Aug 06 '15 at 16:01
  • Library is Wattcp32 http://www.watt-32.net/ – Sébastien Favier Aug 10 '15 at 11:18

0 Answers0