2013-04-22 80 views
2

想要在windows7中使用c進行客戶端 - 服務器編程,它應該使用http POST方法將字符串發送到服務器。在POST方法paramater應包括IP地址等:使用http進行C編程的Socket編程post

我從http://souptonuts.sourceforge.net/code/http_post.c.html得到這個代碼,並改變了它在Windows上運行,但仍然1的錯誤來了:

#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); 

} 

錯誤這是對MinGW的編譯器的未來是:

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 

回答

2

你得到的代碼是在基於Linux的系統,但在MinGW的(Windows)中不幸標識符h_errno之前服用。

的問題是它在窗戶前面定義的頭文件,那麼你就不能使用這條線

extern int h_errno; 

#define h_errno WSAGetLastError() 

 

只需使用其他標識代替h_errno ,甚至只是刪除該行!

+0

是,它在Linux,但我把它用在頭文件中的上述變化更改爲Windows ...我想你所說的話,並更名爲h_errnoo,現在它顯示這些錯誤: – 2013-04-22 09:51:18

+0

剛刪除該行,看看會發生什麼?我想你不會有任何問題,只是一些鏈接錯誤。 – deepmax 2013-04-22 09:52:00

+0

刪除了,現在它顯示了以下錯誤:$ GCC httppost.c -o httppost.exe 未定義的參考'@的gethostbyname 4' 未定義的參考'h_errnoo「 未定義的參考'hstrerror」 未定義的參考' inet_ntop ' 未定義參考'插座@ 12' 未定義參考'htons @ 4' 未定義參考'inet_pton' 未定義參考'連接@ 12' – 2013-04-22 09:56:59