2

Want to do client-server programming using c in windows7, it should send string to server using http POST method. The paramater in POST method should include the ip-address etc:

I got this code from http://souptonuts.sourceforge.net/code/http_post.c.html and changed it for running it on windows, but still 1 error is coming:

#ifdef WIN32
  #include <winsock2.h>
  #include <ws2tcpip.h>
  #include <windows.h>
#else
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
  #include <unistd.h>
  #include <sys/wait.h>
  #include <netdb.h>
  #include <assert.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>

#define SA      struct sockaddr
#define MAXLINE 4096
#define MAXSUB  200


#define LISTENQ 1024

extern int h_errno;

ssize_t process_http(int sockfd, char *host, char *page, char *poststr)
{
    char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
    ssize_t n;
    snprintf(sendline, MAXSUB,
         "POST %s HTTP/1.0\r\n"
         "Host: %s\r\n"
         "Content-type: application/x-www-form-urlencoded\r\n"
         "Content-length: %d\r\n\r\n"
         "%s", page, host, strlen(poststr), poststr);

    write(sockfd, sendline, strlen(sendline));
    while ((n = read(sockfd, recvline, MAXLINE)) > 0) {
        recvline[n] = '\0';
        printf("%s", recvline);
    }
    return n;

}
int main(void)
{
    int sockfd;
    struct sockaddr_in servaddr;

    char **pptr;
    //********** You can change. Puy any values here *******
    char *hname = "souptonuts.sourceforge.net";
    char *page = "/chirico/test.php";
    char *poststr = "mode=login&user=test&password=test\r\n";
    //*******************************************************

    char str[50];
    struct hostent *hptr;
    if ((hptr = gethostbyname(hname)) == NULL) {
        fprintf(stderr, " gethostbyname error for host: %s: %s",
            hname, hstrerror(h_errno));
        exit(1);
    }
    printf("hostname: %s\n", hptr->h_name);
    if (hptr->h_addrtype == AF_INET
        && (pptr = hptr->h_addr_list) != NULL) {
        printf("address: %s\n",
               inet_ntop(hptr->h_addrtype, *pptr, str,
                 sizeof(str)));
    } else {
        fprintf(stderr, "Error call inet_ntop \n");
    }

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    // bzero(&servaddr, sizeof(servaddr));
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(80);
    inet_pton(AF_INET, str, &servaddr.sin_addr);

    connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
    process_http(sockfd, hname, page, poststr);
    close(sockfd);
    exit(0);

}

The error which is coming on MinGW compiler is:

httppost.c:33:12: error: conflicting types for 'WSAGetLastError'
In file included from httppost.c:5:0:
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/../../../../include/winsock2.h:594:32: n
e: previous declaration of 'WSAGetLastError' was here
Aman Chawla
  • 304
  • 3
  • 8
  • 23

2 Answers2

2

The code you've got is under linux based systems, but in MinGW (Windows) unfortunately the identifier h_errno is taken before.

The problem is this line

extern int h_errno;

it's defined previously in windows header files, then you can not use it:

#define h_errno WSAGetLastError()

 

Just use another identifier instead of h_errno, or even just remove that line!

masoud
  • 55,379
  • 16
  • 141
  • 208
  • Yes, Its in Linux but I changed it to Windows using the above changes in header file...I tried what you said and changed the name to h_errnoo, Now it is showing these errors: – Aman Chawla Apr 22 '13 at 09:51
  • Just remove that line and see what happens? I think you will not have any problem just some link errors. – masoud Apr 22 '13 at 09:52
  • Removed that, now it is showing the following errors: $ gcc httppost.c -o httppost.exe undefined reference to `gethostbyname@4' undefined reference to `h_errnoo' undefined reference to `hstrerror' undefined reference to `inet_ntop' undefined reference to `socket@12' undefined reference to `htons@4' undefined reference to `inet_pton' undefined reference to `connect@12' – Aman Chawla Apr 22 '13 at 09:56
  • c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: C:\Users\a mit\AppData\Local\Temp\cc4wXaUX.o: bad reloc address 0x20 in section `.eh_frame' c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation collect2.exe: error: ld returned 1 exit status – Aman Chawla Apr 22 '13 at 09:57
  • Now, you have to add some libraries such as `libws2_32.a` to your project. – masoud Apr 22 '13 at 09:58
  • yes, done the same.....check this $ gcc httppost.c -o httppost.exe -D_WIN32_WINNT=0x0501 -lwsock32 -lAdvapi32 -lws2_32 -lmswsock – Aman Chawla Apr 22 '13 at 10:01
  • Now, these errors are coming: C:\Users\amit\AppData\Local\Temp\ccQS6cTg.o:httppost.c:(.text+0x114): undefined reference to `hstrerror' C:\Users\amit\AppData\Local\Temp\ccQS6cTg.o:httppost.c:(.text+0x19a): undefined reference to `inet_ntop' C:\Users\amit\AppData\Local\Temp\ccQS6cTg.o:httppost.c:(.text+0x247): undefined reference to `inet_pton' c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: C:\Users\a mit\AppData\Local\Temp\ccQS6cTg.o: bad reloc address 0x20 in section `.eh_frame' – Aman Chawla Apr 22 '13 at 10:02
  • Since `inet_pton` is not implemented in MinGW, you should add it yourself. See [this answer](http://stackoverflow.com/a/15370175/952747). – masoud Apr 22 '13 at 10:07
1

Maybe you should try the wininet library.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa383630(v=vs.85).aspx

angeek86
  • 108
  • 5