找回密码
 注册
查看: 2301|回复: 4

怎样用C语言实现HTTP协议的编程??

[复制链接]
发表于 2005-10-13 20:45:11 | 显示全部楼层 |阅读模式
其实就是想与一个服务器实现在HTTP协议上面的通信.
用POST给服务器发送一段参数.然后接收服务器的返回信息..
我在Google上找了好久,都没找到有这类的文章.哪位大侠能帮我一下.
不胜感激!!!
发表于 2005-10-14 08:41:07 | 显示全部楼层

server.c 需要 你自己修改一下

server.c


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>

#define MYPORT 80   

#define BACKLOG 10   

void sigchld_handler(int s)
{
    while(wait(NULL) > 0);
}

int main(void)
{
    int sockfd, new_fd;  
    struct sockaddr_in my_addr;   
    struct sockaddr_in their_addr;
    int sin_size;
    struct sigaction sa;
    int yes=1;

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
        perror("setsockopt");
        exit(1);
    }
   
    my_addr.sin_family = AF_INET;      
    my_addr.sin_port = htons(MYPORT);     
    my_addr.sin_addr.s_addr = INADDR_ANY;
    memset(&(my_addr.sin_zero), '\0', 8 ) ;

    if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
        perror("bind");
        exit(1);
    }

    if (listen(sockfd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }

    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    if (sigaction(SIGCHLD, &sa, NULL) == -1) {
        perror("sigaction");
        exit(1);
    }

    while(1) {  // main accept() loop
        sin_size = sizeof(struct sockaddr_in);
        if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
            perror("accept");
            continue;
        }
        printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
        if (!fork()) { // this is the child process
            close(sockfd); // child doesn't need the listener
            if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
                perror("send");
            close(new_fd);
            exit(0);
        }
        close(new_fd);
    }

    return 0;
}
回复

使用道具 举报

 楼主| 发表于 2005-10-14 13:32:01 | 显示全部楼层

Re: server.c 需要 你自己修改一下

[quote:7d9e58afa8="0xfffff"]server.c


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>

#define MYPORT 80   

#define BACKLOG 10   

void sigchld_handler(int s)
{
    while(wait(NULL) > 0);
}

int main(void)
{
    int sockfd, new_fd;  
    struct sockaddr_in my_addr;   
    struct sockaddr_in their_addr;
    int sin_size;
    struct sigaction sa;
    int yes=1;

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
        perror("setsockopt");
        exit(1);
    }
   
    my_addr.sin_family = AF_INET;      
    my_addr.sin_port = htons(MYPORT);     
    my_addr.sin_addr.s_addr = INADDR_ANY;
    memset(&(my_addr.sin_zero), '\0', 8 ) ;

    if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
        perror("bind");
        exit(1);
    }

    if (listen(sockfd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }

    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    if (sigaction(SIGCHLD, &sa, NULL) == -1) {
        perror("sigaction");
        exit(1);
    }

    while(1) {  // main accept() loop
        sin_size = sizeof(struct sockaddr_in);
        if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
            perror("accept");
            continue;
        }
        printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
        if (!fork()) { // this is the child process
            close(sockfd); // child doesn't need the listener
            if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
                perror("send");
            close(new_fd);
            exit(0);
        }
        close(new_fd);
    }

    return 0;
}[/quote]


这个好像是WEB服务器吧
我是想以客户端的形式给一个己经存在的服务器发送参数,用POST方式,而不是接受一个连接请求然后给客户端发送数据...
回复

使用道具 举报

发表于 2005-10-14 14:18:31 | 显示全部楼层
客户端的应该是这样的:
include <stdio.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netdb.h>

#define  HTTPDATALEN    1024
#define  SERVERIP      "*.*.*.*"//此为服务器的IP地址
#define  SERVERHOST    "**"//此为服务器的名字
#define  SERVERGET     "/*/*"//此为要访问的网站
#define  SERVERPORT    80
#define  DEBUG

int DoInitialize();
int SendRequest(int socket);
int GetResponse(int socket);

int main (int argc, char ** argv)
{
        int ret;
        int socksd;

        socksd = DoInitialize();

        if (ret <0)
        {
                close (socksd);
                return -1;
        }

        ret = SendRequest(socksd);

        if (ret <0)
        {
                close (socksd);
                return -1;
        }

        ret = GetResponse(socksd);

        if (ret <0)
        {
                close (socksd);
                return -1;
        }

        close (socksd);
}

int DoInitialize()
{
        int sock,conn_ret,ret;
        struct sockaddr_in addr;
        struct hostent *servhost;
        struct sockaddr_in server;
        struct servent *service;
                   
        if (inet_aton(SERVERIP,&addr.sin_addr)==0)
        {

#ifdef DEBUG
                 printf("sockaddr init error!%s\n","exit");
#endif
               return -1;
            }
               servhost = gethostbyaddr((char *)&addr.sin_addr,4,AF_INET);

#ifdef DEBUG
                printf("sockaddr init ok!%s\n","continue");
#endif

   
        bzero((char *)&server, sizeof(server));

        server.sin_family = AF_INET;

        bcopy(servhost->h_addr, (char *)&server.sin_addr,
              servhost->h_length);
            
        server.sin_port = htons(SERVERPORT);//
#ifdef DEBUG
        printf("sockaddr init ok!%s\n","continue");
#endif
            

    // init sock
        sock = socket(AF_INET, SOCK_STREAM, 0);

        if (sock <0)
        {
                printf("socket init error!%s\n","exit");
                return -1;

        }
#ifdef DEBUG
           printf("socket init ok!%s\n","continue");
#endif
          // connect sock
        conn_ret = connect (sock, (struct sockaddr*) &
                     server, sizeof (server));

        // connect sock error
        if (conn_ret <0)
        {
#ifdef DEBUG
                printf("connnection error!%s\n","exit");
#endif
           return -1;
        }
#ifdef DEBUG
                 printf("connnection ok!%s\n","continue");
#endif                    

        return sock;

}

int SendRequest( int sock)
{
        int nwbytes;
        char *sendmsg;
        char strReq[HTTPDATALEN*64];
       

        memset(strReq, 0, sizeof(strReq));

         // set the request to accord with http protocal
        strcat(strReq, "GET ");
        strcat(strReq, SERVERGET);
        strcat(strReq, " HTTP/1.0\r\n");

        strcat(strReq, "Content-length: ");
        strcat(strReq, "0");
        strcat(strReq, "\r\n");

        strcat(strReq, "Content-type: ");
        strcat(strReq, "application/x-www-form-urlencoded");
        strcat(strReq, "\r\n");

        strcat(strReq, "Host: ");
        strcat(strReq, SERVERHOST);
        strcat(strReq, "\r\n");

        strcat(strReq, "User-Agent: ");
        strcat(strReq,
        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
        strcat(strReq, "\r\n");

        strcat(strReq, "\r\n");

        strcat(strReq, "\r\n");

        sendmsg = strReq;
          
          // send the request to server
        nwbytes = write(sock,sendmsg,strlen(sendmsg));
          
          
        if(nwbytes<0)
        {
#ifdef DEBUG
                printf("send message error!%s\n","exit");
                printf("close the sock !\n");
#endif          
                close (sock);
                return -1;

        }

#ifdef DEBUG
        printf("send message ok!%s\n","continue");
#endif          

        return 0;

}

int GetResponse(int sock)
{
        unsigned int j;
        int ret;
        //http://pc065:8080/applyld/counterStatic_Init.do
        char rbuf[1024];
        char strRes[HTTPDATALEN*64];       
   
#ifdef DEBUG
        printf("============= REQUEST BEGIN ==============%s\n","=");
#endif  
      
        memset(strRes, 0, sizeof(strRes));
        strcpy(strRes, "");

        while ((ret=read(sock, rbuf, sizeof(rbuf)-1)) > 0)
        {
                 rbuf[ret] = '\0';
                 strcat(strRes, rbuf);
                 memset(rbuf, 0, sizeof(rbuf));
         }
          
#ifdef DEBUG
         printf("the response data is:\n");          
          

          for (j=0;j<(HTTPDATALEN*64);j++)
          {
                  printf("%c",strRes[j]);
          }

#endif          
            
          if(strstr(strRes,"OK")||strstr(strRes,"200"))
          {

                  printf("sucess!\n\n");

          }
          else
          {
                  printf("have problem !\n\n");
          }
#ifdef DEBUG
          printf("============= REQUEST END ==============%s\n","=");
          printf("close the sock !\n");
#endif          
          //close sock
          close (sock);

          return 0;
}


已经测试过,可以使用
回复

使用道具 举报

 楼主| 发表于 2005-10-14 19:27:48 | 显示全部楼层
谢谢!!!
非常感谢!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2025-2-7 20:07 , Processed in 0.021283 second(s), 16 queries .

© 2001-2025 Discuz! Team. Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表