首先分析http协议的报头数据

   1: GET /index.html HTTP/1.1
   2: Host: localhost:8000
; Ubuntu; Linux i686; rv:10.0.1) Gecko/20100101 Firefox/10.0.1
;q=0.9,*/*;q=0.8
;q=0.5
   6: Accept-Encoding: gzip, deflate
   7: Connection: keep-alive

 

得到第一行数据GET /index.html /HTTP/1.1

建立一个简单的web程序,大致流程与笔记一种类型

直接贴代码了

服务端代码:

/**
** web_server.c
** copyright Xu Dongdong 2013.5.9  Open Source Software License.
*/
   5:  
#include <stdio.h>
/*exit */
/*memset*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
  14:  
int port = 8000;
  16:  
 ;
char ret_buf[32768];
int num_buf);
  20:  
char*argv[])
  22: {
  23:     
char * )malloc(4001);
  25:  
int rc;
  27:  
struct sockaddr_in serv_addr;
struct sockaddr_in cli_addr;
  30:  
int sockfd;
struct hostent* entity; 
int total_recv, total_sent,bytes_sent,total_size,size;
int sockfd_tmp;
int addr_size;
  36:  
char *buf;
int i,len;
  39:  
  40:     sockfd = socket(AF_INET, SOCK_STREAM,0);
if(sockfd == -1)
  42:     {
);
  44:         exit(1);
  45:     }
sizeof(serv_addr));
  47:     serv_addr.sin_family = AF_INET;
  48:     serv_addr.sin_addr.s_addr= htonl(INADDR_ANY);
  49:     serv_addr.sin_port = htons(port);
  50:  
  51:  
sizeof(serv_addr)) == -1)
  53:     {
);
  55:         exit(1);
  56:     }
  57:  
if(listen(sockfd,20) == -1)
  59:     {
);
  61:         exit(1);
  62:     }
  63:  
);
  65:  
while(1)
  67:     {
struct sockaddr_in);
do{
struct sockaddr*)&cli_addr,&rc);
while( sockfd_tmp == -1  && errno == EINTR);
if(sockfd_tmp == -1)
  73:         {
);
  75:             exit(1);
  76:         }
  77:  
struct in_addr),AF_INET);
  79:         i =  recv(sockfd_tmp,recvBuffer,4000,0) ;
if(i == -1) 
  81:         {
);
break;
  84:         }
break;
'\0' ;
  87:     
,recvBuffer);
  89:  
  90:         buf = read_file(recvBuffer, total_recv);
  91:         size = strlen(buf);
  92:         total_sent = 0;
  93:  
do{
  95:             bytes_sent = send(sockfd_tmp ,buf+total_sent, strlen(buf + total_sent) ,0);
if(bytes_sent == -1)
  97:             {
break;
  99:             }
 100:             total_sent += bytes_sent;
while(total_size < size) ;
 102:  
 103:         close(sockfd_tmp);
 104:     }
 105:  
return EXIT_SUCCESS;
 107: }
 108:  
int num_buf)
 110: {
int i ;
char * cp,  *cp2 ;
 113:     FILE * file;
 114:     cp =buf +5 ;
);
if(cp2 != NULL )
 117:     {
'\0' ;
 119:     }
 120:  
);
if(file == NULL )
 123:     {
return error_return;
 125:     }
 126:     i= fread(ret_buf ,1 ,32768 ,file);
 127:  
if(i==0)
 129:     {
 130:         fclose(file);
return error_return;
 132:     }
 133:  
'\0';
 135:     fclose(file);
return ret_buf;
 137: }

 

客户端代码:

/**
** client.c
** copyright Xu Dongdong 2013.5.9  Open Source Software License.
*/
   5:  
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
   9:  
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
  14:  
//local host
int port = 8000 ;
  17:  
char*argv[])
  19: {
char buf[8192];
char message[256];
  22:  
struct sockaddr_in cli_addr;
  24:  
int sockfd;
struct hostent *serv_host_name;
  27:  
  28:  
if((serv_host_name = gethostbyname(host_name)) ==0)
  30:     {
);
  32:         exit(1);
  33:     }
  34:  
  35:  
  36:     sockfd = socket(AF_INET, SOCK_STREAM,0);
if(sockfd == -1)
  38:     {
);
  40:         exit(1);
  41:     }
  42:  
sizeof(cli_addr));
  44:     cli_addr.sin_family = AF_INET;
  45:     cli_addr.sin_addr.s_addr= htonl(INADDR_ANY);
struct in_addr *)(serv_host_name -> h_addr) )-> s_addr);
  47:     cli_addr.sin_port = htons(port);;
  48:  
sizeof(cli_addr)) == -1)
  50:     {
);
  52:         exit(1);
  53:     }
  54:  
);
, message);
  57:  
if(send(sockfd,message,strlen(message), 0) == -1)
  59:         {
);
  61:               exit(1);
  62:         }
  63:  
);
  65:  
if(recv(sockfd,buf,8192,0) == -1)
  67:         {
);
  69:               exit(1);
  70:         }
,buf);
  72:  
  73:         close(sockfd);
  74:     }
  75:  

直接使用gcc编译运行即可

可以使用浏览器作为客户端进行访问,具体的url为: http://localhost:8000/index.html

 

作为实例我们给出最简单你的html代码 index.html

   1: <html>
   2: <head>
   3: </head>
   4: <body>
   5: index.html
   6: </body>
   7: </html>

相关文章: