【问题标题】:Socket programming in C using http post使用 http post 在 C 中进行套接字编程
【发布时间】:2013-04-22 09:40:20
【问题描述】:

想在 windows7 中使用 c 进行客户端-服务器编程,它应该使用 http POST 方法将字符串发送到服务器。 POST 方法中的参数应包括 ip-address 等:

我从 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

【问题讨论】:

    标签: c sockets http-post


    【解决方案1】:

    你得到的代码是在基于 linux 的系统下,但不幸的是,在 MinGW (Windows) 中,标识符 h_errno 是之前使用的。

    问题出在这一行

    extern int h_errno;
    

    之前已经在windows头文件中定义了,那么就不能使用了:

    #define h_errno WSAGetLastError()
    

     

    只需使用另一个标识符而不是 h_errno,甚至直接删除该行!

    【讨论】:

    • 是的,它在Linux中,但我使用上述头文件中的更改将其更改为Windows...我尝试了您所说的并将名称更改为h_errnoo,现在它显示这些错误:跨度>
    • 删除那行,看看会发生什么?我认为您不会有任何问题,只是一些链接错误。
    • 删除了,现在它显示以下错误: $ 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 @987654327 @htons@4' 未定义引用 inet_pton' undefined reference to connect@12'
    • c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: C: \Users\a mit\AppData\Local\Temp\cc4wXaUX.o:“.eh_frame”c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../ 部分中的错误重定位地址 0x20。 ./../../mingw32/bin/ld.exe:最终链接失败:无效操作collect2.exe:错误:ld返回1退出状态
    • 现在,您必须在项目中添加一些库,例如 libws2_32.a
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多