Linux下利用Socket抓取网页内容

主要用来和WinSock进行下比较:

--WinSock--
需要初始化:
if( (Ret = WSAStartup(MAKEWORD(1,1), &wsaData) ) != 0 )
{
printf("WSAStartup failed with error %dn", Ret);
return FALSE;
}

头文件:
--WinSock--
#include //header
#pragma comment (lib, "ws2_32.lib") //lib
--Linux--
#include
#include
#include
#include
各个头文件的作用还需要进一步研究

gethostbyname(host)://从主机名返回地址
这个都是一样的,返回一个struct hostent *的指针。

地址结构:
--WinSock--
SOCKADDR_IN
--Linux--
sockaddr_in
实际上是一样的都是
struct sockaddr_in{
   shortsin_family;
   unsigned short sin_port;
   struct in_addr sin_addr;
   charsin_zero[8];
};
(
这个结构是sockaddr的等价结构
struct sockaddr
{
unsigned short sa_family; // address family, AF_XXX
char sa_data[14];   //14 bytes of protocol address
};
)
其中IP地址结构struct in_addr定义如下:
struct   in_addr {
    union   {
         struct{
             unsigned char   s_b1,
                              s_b2,
                              s_b3,
                              s_b4;
        } S_un_b;
             struct {
             unsigned short s_w1,
                              s_w2;
              } S_un_w;
               unsigned long S_addr;
     } S_un;
};

Socket:
--WinSock--
返回句柄SOCKET,就是socket描述符
--Linux--
比较直接返回int型socket描述符

函数接口都一样
函数例子:
socket (AF_INET, SOCK_STREAM, 0); //TCP

connect(sock, (const sockaddr * )&tcpaddr, sizeof(tcpaddr)); //返回值有不同
--WinSock--
If no error occurs, connect returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling

WSAGetLastError.
--Linux--
错误返回-1

send(sock_description, message, strlen(message), 0); //返回值不同
--WinSock--
If no error occurs, send returns the total number of bytes sent, which can be less than the number indicated by len. Otherwise, a value of

SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
--Linux--
错误返回-1

recv(sock_description, buffer, sizeof(buffer), 0);//返回值不同
--WinSock--
If no error occurs, recv returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a

value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
--Linux--
错误返回-1

结束:
--WinSock--
closesocket(sock);
if( WSACleanup() == SOCKET_ERROR )
{
printf("WSACleanup failed with error %d n", WSAGetLastError() );
}
--Linux--
close(sock);

下面是一个Linux下socket一个HTTP协议GET方法的应用:
#include
#include
#include

#include
#include
#include
#include

char* host = "www.hao123.com";
int port = 80;

int main(void)
{
char buffer[512];
int isock;
struct sockaddr_in pin;
struct hostent * remoteHost;
char message[512];
int done = 0;
int chars = 0;
int l = 0;

if( (remoteHost = gethostbyname(host)) == 0 )
{
   printf("Error resolving hostn");
   exit(1);
}

bzero(message,sizeof(message));
bzero(&pin,sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_port = htons(port);
pin.sin_addr.s_addr = ( (struct in_addr *)(remoteHost->h_addr) )->s_addr;

if( (isock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
   printf("Error opening socket!n");
   exit(1);
}
sprintf(message, "GET / HTTP/1.1rn");
strcat(message, "Host:www.hao123.comrn");
strcat(message, "Accept: */*rn");
strcat(message, "User-Agent: Mozilla/4.0(compatible)rn");
strcat(message, "connection:Keep-Alivern");
strcat(message, "rnrn");

printf("%s",message);

if( connect(isock, (void *)&pin, sizeof(pin)) == -1 )
{
   printf("Error connecting to socketn");
   exit(1);
}

if( send(isock, message, strlen(message), 0) == -1)
{
   printf("Error in sendn");
   exit(1);
}

while(done == 0)
{
   l = recv(isock, buffer, 1, 0);
   if( l < 0 )
    done = 1;
   switch(*buffer)
   {
    case 'r':
     break;
    case 'n':
     if(chars == 0)
      done = 1;
     chars = 0;
     break;
    default:
     chars++;
     break;
   }
    printf("%c",*buffer);
}

do
{
   l = recv(isock, buffer, sizeof(buffer) - 1, 0);
   if( l < 0 )
    break;
   *(buffer + l) = 0;
   fputs(buffer, stdout);
}while( l > 0 );

close(isock);
return 0;
}





#include
#include
struct hostent *gethostbyname(const char *name);
这个函数的传入值是域名或者主机名,例如" www.google.com","wpc "等等。
传出值,是一个hostent的结构(如下)。如果函数调用失败,将返回NULL。
struct hostent {
  char  *h_name;
  char  **h_aliases;
  int   h_addrtype;
  int   h_length;
  char  **h_addr_list;
  };
解释一下这个结构:
其中,
  char *h_name 表示的是主机的规范名。例如 www.google.com 的规范名其实是 www.l.google.com
  char   **h_aliases 表示的是主机的别名。 www.google.com 就是google他自己的别名。有的时候,有的主机可能有好几个别名,这些,其实都是为了易于用户记忆而为自己的网站多取的名字。
  int   h_addrtype 表示的是主机ip地址的类型,到底是ipv4(AF_INET),还是ipv6(AF_INET6)
  int   h_length 表示的是主机ip地址的长度
  int   **h_addr_lisst 表示的是主机的ip地址,注意,这个是以网络字节序存储的。千万不要直接用printf带%s参数来打这个东西,会有问题的哇。所以到真正需要打印出这个IP的话,需要调用inet_ntop()。
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) :
这个函数,是将类型为af的网络地址结构src,转换成主机序的字符串形式,存放在长度为cnt的字符串中。
这个函数,其实就是返回指向dst的一个指针。如果函数调用错误,返回值是NULL。

下面是例程,有详细的注释。

gethostbyname(此处)


#include
#include int main(int argc, char **argv){ char *ptr,**pptr; struct hostent *hptr; char str[32]; /* 取得命令后第一个参数,即要解析的域名或主机名 */ ptr = argv[1]; /* 调用gethostbyname()。调用结果都存在hptr中 */ if( (hptr = gethostbyname(ptr) ) == NULL ) {  printf("gethostbyname error for host:%sn", ptr);  return 0; /* 如果调用gethostbyname发生错误,返回1 */ } /* 将主机的规范名打出来 */ printf("official hostname:%sn",hptr->h_name); /* 主机可能有多个别名,将所有别名分别打出来 */ for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)  printf(" alias:%sn",*pptr); /* 根据地址类型,将地址打出来 */ switch(hptr->h_addrtype) {  case AF_INET:  case AF_INET6:   pptr=hptr->h_addr_list;   /* 将刚才得到的所有地址都打出来。其中调用了inet_ntop()函数 */   for(;*pptr!=NULL;pptr++)    printf(" address:%sn", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));   break;  default:   printf("unknown address typen");   break; } return 0;}
参与0

0同行回答

“答”则兼济天下,请您为题主分忧!

提问者

擅长领域: 服务器LinuxPower服务器

相关问题

相关资料

相关文章

问题状态

  • 发布时间:2013-07-23
  • 关注会员:0 人
  • 问题浏览:1316
  • X社区推广