位置: IT常识 - 正文

Linux下socket实现网页抓取 Unicorn 博客频道 CSDN.NET

编辑:rootadmin
Linux下socket实现网页抓取 - Unicorn - 博客频道 - CSDN.NET Linux下socket实现网页抓取 分类: C/C++学习点滴 DO spiders DO linux编 Linux下socket实现网页抓取 分类: C/C++学习点滴 DO spiders DO linux编程 2007-10-06 21:33 951人阅读 评论(0) 收藏 举报

推荐整理分享Linux下socket实现网页抓取 Unicorn 博客频道 CSDN.NET,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

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

--WinSock--

需要初始化:

if( (Ret = WSAStartup(MAKEWORD(1,1), &wsaData) ) != 0 )

{

printf("WSAStartup failed with error %d/n", Ret);

return FALSE;

}

头文件:

--WinSock--

#include <winsock2.h> //header

#pragma comment (lib, "ws2_32.lib") //lib

--Linux--

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <netdb.h>

各个头文件的作用还需要进一步研究

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);

Linux下socket实现网页抓取  Unicorn  博客频道  CSDN.NET

if( WSACleanup() == SOCKET_ERROR )

{

printf("WSACleanup failed with error %d /n", WSAGetLastError() );

}

--Linux--

close(sock);

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

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <netdb.h>

char*host = "www.hao123.com";

intport = 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 host/n");

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.1/r/n");

strcat(message, "Host:www.hao123.com/r/n");

strcat(message, "Accept: */*/r/n");

strcat(message, "User-Agent: Mozilla/4.0(compatible)/r/n");

strcat(message, "connection:Keep-Alive/r/n");

strcat(message, "/r/n/r/n");

printf("%s",message);

if( connect(isock, (void *)&pin, sizeof(pin)) == -1 )

{

printf("Error connecting to socket/n");

exit(1);

}

if( send(isock, message, strlen(message), 0) == -1)

{

printf("Error in send/n");

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;

}

本文链接地址:https://www.jiuchutong.com/zhishi/304077.html 转载请保留说明!

上一篇:帝国cms附表如何分表(帝国cms使用手册)

下一篇:帝国cms怎么多条件搜索(帝国cms界面)

  • 钉钉不是主持人可以录屏吗(钉钉不是主持人可以邀请别人吗)

  • 腾讯电脑管家怎么禁止广告(腾讯电脑管家怎么用QQ登录)

  • 荣耀30pro充电一般多长时间充满(荣耀30pro充电一直是0)

  • 荣耀play3能升级EMUI10吗(荣耀play3可以升级)

  • wps个人版和企业版的区别是(wps个人版和企业版费用)

  • 文档怎么搜索关键字(文档里搜索)

  • qq下载的东西怎么取消(qq下载内容)

  • 计算机资源主要指(计算机资源主要指计算机硬件、_____________与数据)

  • 腾讯视频有些电影不能投屏(腾讯视频有些电影不能一起看)

  • 820相当于骁龙多少(820骁龙的手机)

  • 抖音收藏别人视频别人能看得到吗(抖音收藏别人视频别人能看到吗)

  • missing operating system怎么解决(missing operating system怎么办)

  • 芯片的纳米数是指什么(芯片的纳米数是什么参数)

  • 支付宝扫码付款后能加好友吗(支付宝扫码付款后怎么查看对方账号)

  • 格式化软盘即什么(格式化软件是什么意思)

  • ps中怎么换照片背景(ps怎么替换图片)

  • vue怎么编辑慢动作(vue怎么做gif动图)

  • 2003word选项在哪里(office2003选项在哪里)

  • 如何查找添加打印机(如何查找我添加的好友)

  • 荣耀9x有指纹解锁吗(荣耀9x指纹解锁失灵)

  • ipadair升级ios12卡吗(ipadair1升级到12很卡)

  • 菜鸟裹裹在哪里投诉(拼多多的菜鸟裹裹在哪里)

  • 支付宝短信校验服务如何关闭(支付宝短信校验码是什么意思)

  • 实况照片怎么发(实况照片怎么发送)

  • Windows 10无法连接到SENS服务(windows10无法连接到打印机,拒绝访问)

  • 使用python进行傅里叶FFT 频谱分析(python进行傅立叶变换)

  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设 电脑维修 湖南楚通运网络