依睛(IT blog) 我回来了,PHP<-->C/C++ LINUX

笨鸟

统计

积分与排名

友情连接

最新评论

HTTP协议的C写的实例

如果哪有问题请指正, 自己写的不太好.

/* *************************************
*  HttpClient connection Server
*  
* http protocol
*/


#include 
< windows.h >
#include 
< stdio.h >

#pragma comment(lib, 
" ws2_32 " )
#define  LEN                 1024

/* *********************************
*
*  init_win32 
*
*********************************
*/

void  init_win32()
{
    WORD                    wdVersionRequired;
    WSADATA                 wsaData;

    wdVersionRequired 
=  MAKEWORD( 2 2 );
    WSAStartup(wdVersionRequired, 
& wsaData);
}


SOCKET socket_create(sock)
{
    sock 
=  socket(AF_INET, SOCK_STREAM,  0 );
    
if  (SOCKET_ERROR  ==  sock)
    
{
        
return  FALSE;
    }

    
return  sock;

}


/* *********************************
*
*  get remote IP address 
*
*********************************
*/

char   *  getIP( char   *  get_url)
{
    
int                          WSA_return;
    WSADATA                     WSAData;
    
/*  ½á¹¹Ö¸Õë  */  
    HOSTENT 
* host_entry;
    
char   *                       ip;

    WSA_return
= WSAStartup( 0x0101 , & WSAData);

    
if (WSA_return == 0 )
    
{
        
/*  ¼´Òª½âÎöµÄÓòÃû»òÖ÷»úÃû  */
        host_entry
= gethostbyname(get_url);
        printf(
" %s\n " , get_url);
        
if (host_entry != 0 )
        
{
           ip 
=  inet_ntoa( * ( struct  in_addr  * ) * host_entry -> h_addr_list); 
            printf(
" ½âÎöIPµØÖ·:  " );
            printf(
" %d.%d.%d.%d " ,
            (host_entry
-> h_addr_list[ 0 ][ 0 ] & 0x00ff ),
            (host_entry
-> h_addr_list[ 0 ][ 1 ] & 0x00ff ),
            (host_entry
-> h_addr_list[ 0 ][ 2 ] & 0x00ff ),
            (host_entry
-> h_addr_list[ 0 ][ 3 ] & 0x00ff ));
           
return  ip;
        }

    }

    WSACleanup();
    
return  NULL;
}


/* *********************************
*
*  socket connect
*
*********************************
*/

int  sockCon(SOCKET sock, char   *  get_url)
{
    
int                          TimeOut = 5000 // Á¬½Ó³¬Ê±
     int                          ret;
    
short                        port = 80 ;
    
char   *                       get_ip;
    
struct  sockaddr_in      saServer;
    unsigned 
long                ul  =   1 ;
    unsigned 
long                ul2 = 0 ;
    

    memset(
& saServer,  0 sizeof (saServer));
    
/*  »ñÈ¡Ô¶³Ì IP  */
    get_ip 
=  getIP(get_url);
    saServer.sin_family 
=  AF_INET;
    saServer.sin_addr.S_un.S_addr 
=  inet_addr(get_ip);
    saServer.sin_port 
=  htons(port);
    
    
/*  ÉèÖ÷¢Ëͳ¬Ê±6Ãë  */
    
if  (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, ( char * ) & TimeOut,  sizeof ( int )) == SOCKET_ERROR)
    
{  
        
return  FALSE;
    }


   
/*  ½ÓÊÕ³¬Ê±6Ãë  */
    
if  (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, ( char * ) & TimeOut,  sizeof (TimeOut)) == SOCKET_ERROR)
        
return  FALSE;
     
    
/*  ÉèÖ÷Ç×èÈû·½Ê½Á¬½Ó  */
    ret
= ioctlsocket(sock, FIONBIO, (unsigned  long   * ) & ul);

    
if  (ret == SOCKET_ERROR)
        
return  FALSE;

 
if  (connect(sock, ( struct  sockaddr * ) & saServer,  sizeof ( struct  sockaddr))  !=   0 )
    
{
        
struct  timeval              timeout;
        fd_set                      r;

        FD_ZERO(
& r);
        FD_SET(sock, 
& r);

        timeout.tv_sec
= 10 ;   // Á¬½Ó³¬Ê±15Ãë
        timeout.tv_usec = 0 ;

        ret 
=  select(sock + 1 , NULL,  & r, NULL,  & timeout);
        printf(
" select: %d\n " , ret);

        
if  (ret  <=   0 )
        
{
            printf(
" connect select timeout!!!\n " );
            closesocket(sock);
            
return  FALSE;
        }


         
// Ò»°ã·ÇËø¶¨Ä£Ê½Ì׽ӱȽÏÄÑ¿ØÖÆ£¬¿ÉÒÔ¸ù¾Ýʵ¼ÊÇé¿ö¿¼ÂÇ ÔÙÉè»Ø×èÈûģʽ
        ret = ioctlsocket(sock, FIONBIO, (unsigned  long * ) & ul2);
        
        
if  (ret == SOCKET_ERROR)
        
{
            closesocket(sock);
            
return  FALSE;
        }

        
else
        
{
            printf(
" connect server success !\n " );
        }

    }
   
    
return   0 ;
}


/* *********************************
*
*  socket receiving station
*
*********************************
*/

int  socket_send(SOCKET sock,  char   *  protocolHead,  int  length)
{
    
if ( 0   ==  send(sock, protocolHead, length,  0 ))   // send server
         return  FALSE;
    
return   0 ;
}


/* *********************************
*
*  socket receiving station
*
*********************************
*/

socket_rv(SOCKET sock, 
char   *  recvBuf,  int  rvlength)
{
    fd_set                      fds_r;
    
int                          status;
    
struct  timeval              tm  =   { 20 , 7 } ;

    FD_ZERO(
& fds_r);
    FD_SET(sock,
& fds_r);

    status
= select(sock +   1 & fds_r,  0 0 & tm);  // socketIdÔÚÕâÀïÊÇ×î´óµÄfd
     if (status  >   0   &&  FD_ISSET(sock,  & fds_r))
    
{
        printf(
" Socket is readablefd=[%d]\n " ,sock);
        
while ( 1 )
        
{
            memset(recvBuf, 
0 sizeof (recvBuf));
            
if  (recv(sock, recvBuf, rvlength,  0 ) == 0 )
            
{
                
break ;
            }

            printf(
" \n%s\n " , recvBuf);
        }

    }

    
return   0 ;
}


/* *********************************
*
*  socket close
*
*********************************
*/

sock_close(SOCKET sock)
{
    WSACleanup();
    closesocket(sock);

}


/* *********************************
*
*  main function
*
*********************************
*/

int  main()
{
    SOCKET                      sock 
=   0 ;
    
int                          rvlength = 4096 ;
    
int                          senlenght;
    
char   *                       get_url = " www.google.cn " ;
    
char                         recvBuf[ 4096 ] = "" ;
    
char                         protocolHead[] = " GET www.google.cn HTTP/1.1\r\n "
                                                
" Accept: */*\r\n "
                                                
" Accept-Language: zh-cn,zh; q=0.5\r\n "  
                                                
" User-Agent: EasyAgent/1.01\r\n "
                                                
" Connection: Keep-Alive\r\n "
                                                
" Accept-Encoding: gzip, deflate\r\n "
                                                
" Host: www.google.cn\r\n\r\n " //  ±íʾHTTP ½áÊø·û\r\n\r\n

    senlenght 
=   sizeof (protocolHead);
    init_win32();
    sock 
=  socket_create(sock);

    sockCon(sock, get_url);
    socket_send(sock, protocolHead, senlenght);
    socket_rv(sock, recvBuf, rvlength);
    
    sock_close(sock);

    
return   0 ;
}




posted on 2008-12-18 15:46 向左向右走 阅读(432) 评论(1)  编辑 收藏 引用 所属分类: C/C++学习资料库

评论

# re: HTTP协议的C写的实例 2008-12-18 17:54 戴尔笔记本

挺好的思路,学习  回复  更多评论   

只有注册用户登录后才能发表评论。