mingw socket programming

Discussion in 'C Programming' started by Khookie, Dec 6, 2007.

  1. Khookie

    Khookie Guest

    Hi everyone

    I've been coding a web server recently, and wanted to figure out how
    to do it with mingw (code is below).

    It works fine when you first load a page (GET request), but when you
    submit (POST), it usually comes back with a "connection has been
    reset" error. Sometimes it does work though.

    Furthermore - when I inspect the request string, it's always like:

    --START--
    POST / HTTP/1.1
    Host: localhost
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:
    1.8.1.11) Gecko/20071127 Firefox/2.0.0.11
    Accept: text/xml,application/xml,application/xhtml+xml,text/
    html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
    Accept-Language: en-au
    Accept-Encoding: gzip,deflate
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive: 300
    Connection: keep-alive
    Referer: http://localhost/
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 19

    --END--

    I can't figure out how to get the POST string (i.e.
    something=something&something2=something2, etc.)...

    Any winsock insight would be appreciated.

    Code below:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #ifdef MINGW32
    #include <winsock2.h>
    #else
    #include <arpa/inet.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <netinet/in.h>
    #include <signal.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #endif

    int main() {
    #ifdef MINGW32
    WSADATA wsadata;
    if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
    printf("Error creating socket.");
    return -1;
    }
    #endif

    int listen_fd;
    if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    puts("Error opening socket.");
    return -1;
    }

    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(80);

    struct sockaddr_in client_addr;
    unsigned int client_addr_length = sizeof(client_addr);

    int on = 1;
    setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&on,
    sizeof(on));
    if ((bind(listen_fd, (struct sockaddr*)&server_addr,
    sizeof(server_addr))) < 0) {
    puts("Error binding socket.");
    return -1;
    }

    if ((listen(listen_fd, 64)) < 0) {
    puts("Error listening on socket.");
    return -1;
    }

    while (1) {
    int socket_fd = accept(listen_fd, (struct sockaddr *)&client_addr,
    &client_addr_length);

    char request[32768];
    #ifdef MINGW32
    recv(socket_fd, request, 32768, 0);
    #else
    read(socket_fd, request, 32768);
    #endif

    puts(request);

    char* content = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nA
    web server doing POST<br/><form method=post><input type=text
    name=something value=something><input type=submit value=Submit></
    form>";
    #ifdef MINGW32
    send(socket_fd, content, strlen(content), 0);
    closesocket(socket_fd);
    #else
    write(socket_fd, content, strlen(content));
    close(socket_fd);
    #endif

    }

    #ifdef MINGW32
    closesocket(listen_fd);
    WSACleanup();
    #endif
    return 0;
    }
     
    Khookie, Dec 6, 2007
    #1
    1. Advertising

  2. Khookie

    Guest

    In article <>,
    Khookie <> wrote:
    >Hi everyone
    >
    >I've been coding a web server recently, and wanted to figure out how
    >to do it with mingw (code is below).


    None of this is defined by the C programming language, which puts it
    beyond the scope of comp.lang.c.


    >Any winsock insight would be appreciated.


    comp.os.ms-windows.programmer.win32 would be a better place to ask
    about that.

    (Though from what you've said it seems to me that it may be a HTTP
    protocol problem and not a winsock problem.)


    dave
     
    , Dec 6, 2007
    #2
    1. Advertising

  3. Khookie

    Khookie Guest

    On Dec 6, 8:33 pm, wrote:
    > In article <>,
    >
    > Khookie <> wrote:
    > >Hi everyone

    >
    > >I've been coding a web server recently, and wanted to figure out how
    > >to do it with mingw (code is below).

    >
    > None of this is defined by the C programming language, which puts it
    > beyond the scope of comp.lang.c.
    >
    > >Any winsock insight would be appreciated.

    >
    > comp.os.ms-windows.programmer.win32 would be a better place to ask
    > about that.
    >
    > (Though from what you've said it seems to me that it may be a HTTP
    > protocol problem and not a winsock problem.)
    >
    > dave


    hey everyone, apologies for posting to the wrong group - will go to
    alt.winsock.programming - Chris
     
    Khookie, Dec 6, 2007
    #3
  4. Khookie

    Guest

    On Dec 6, 10:52 am, Khookie <> wrote:
    > On Dec 6, 8:33 pm, wrote:
    >
    >
    >
    >
    >
    > > In article <>,

    >
    > > Khookie <> wrote:
    > > >Hi everyone

    >
    > > >I've been coding a web server recently, and wanted to figure out how
    > > >to do it with mingw (code is below).

    >
    > > None of this is defined by the C programming language, which puts it
    > > beyond the scope of comp.lang.c.

    >
    > > >Any winsock insight would be appreciated.

    >
    > > comp.os.ms-windows.programmer.win32 would be a better place to ask
    > > about that.

    >
    > > (Though from what you've said it seems to me that it may be a HTTP
    > > protocol problem and not a winsock problem.)

    >
    > > dave

    >
    > hey everyone, apologies for posting to the wrong group - will go to
    > alt.winsock.programming - Chris- Hide quoted text -
    >
    > - Show quoted text -


    Hi
    I love the way you are trying in your programming work and i really
    want you to use this opportunity to introduce yourself to an online
    company where you can get instant assistance in all ways with this
    extend you have gone. There you meet alots of programmers in all
    fields of knowlegde applying for jobs by signing up free of charge,i
    expected you to join them because i saw so many you are far better
    than collecting contract project on daily basis. Here's the site,
    http://www.getafreelancer.com/affil...s/PHP-ASP/selling-using-text-message-sms.html
     
    , Dec 7, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam Smith
    Replies:
    0
    Views:
    1,209
    Sam Smith
    Oct 18, 2004
  2. Laszlo Nagy
    Replies:
    1
    Views:
    5,012
    Mark Wooding
    Jan 27, 2009
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    1,013
    Jean-Paul Calderone
    Jan 27, 2009
  4. Laszlo Nagy
    Replies:
    0
    Views:
    584
    Laszlo Nagy
    Feb 1, 2009
  5. xeno fears
    Replies:
    1
    Views:
    516
    Martin v. Löwis
    Oct 4, 2009
Loading...

Share This Page