socket编程---获取网页上的超链接

//post
#include<stdio.h>
#include<winsock2.h>
#include<windows.h>
#include<iostream>
#include<fstream>
#pragma comment(lib, "ws2_32.lib")

#define name "www.baidu.com" 
#define host "baidu.com"
#define wz "http://baidu.com"
#define max 20480

using namespace std;

void fun(char rcvBuf[]);

int main()
{
	int num,sum;

	SOCKET s;
	WSADATA wsa;
	struct sockaddr_in serv;

	char sndBuf[1024], rcvBuf[max];

	WSAStartup(MAKEWORD(2, 1), &wsa);

	struct hostent* host_addr = gethostbyname(name);
    if (host_addr == NULL)
    {
        cout<<"Unable to locate host"<<endl;
        return -103;
    }


	if ((s = socket(AF_INET, SOCK_STREAM, 0))<0)
	{
		perror("socket error!");
		exit(1);
	}

	memset(&serv,0,sizeof(serv));
	serv.sin_family = AF_INET;
	serv.sin_port = htons(80);
	serv.sin_addr.s_addr = *((int*)*host_addr->h_addr_list);
	//serv.sin_addr.S_un.S_addr = inet_addr("115.239.210.26");

	if ((connect(s, (struct sockaddr *)&serv, sizeof(serv)))<0)
	{
		perror("connet error!");
		exit(1);
	}

	memset(sndBuf, 0, 1024);
	memset(rcvBuf, 0, max);

	//头信息
    strcat(sndBuf, "POST ");
    strcat(sndBuf, wz);
    strcat(sndBuf, " HTTP/1.0\r\n");
    strcat(sndBuf, "Host: ");
    strcat(sndBuf, host);
    strcat(sndBuf, "\r\n");
    strcat(sndBuf, "Connection: keep-alive\r\n");

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

	puts(sndBuf);
  
	if ((num = send(s,sndBuf,1024, 0))<0)
   {
		perror("send error!");
		exit(1);
	}
	
	puts("send success!\n");

	do{
		sum = recv(s, rcvBuf, max-1, 0);//如果rcvBuf的存储空间减小,则分几次copy
		if (sum<0)
		{
			perror("recv error!");
			system("pause");
			exit(1);
		}
		else if (sum>0)
		{
			cout<<rcvBuf<<endl;
			fun(rcvBuf);//2014.11.04
			memset(rcvBuf, 0, max);
		}
	} while (sum>0);
	
	puts("\nread success!\n");

	closesocket(s);

	system("pause");
	WSACleanup();
	
	return 0;

}

void fun(char rcvBuf[])
{
	int i=0;
	ofstream out_sort;
	out_sort.open("F:\\new\\wz.txt",ios::out||ios::app);
	
	if(out_sort.fail())  
    {  
        cout<<"打开文件失败!"<<endl;  
        exit(1);  
    }  
	
	while(rcvBuf[i]!='\0'&&i<max)
	{
		if(rcvBuf[i]=='h'&&rcvBuf[i+1]=='r'&&rcvBuf[i+2]=='e'&&rcvBuf[i+3]=='f'&&rcvBuf[i+4]=='='&&rcvBuf[i+5]=='"')
		{
			i+=6;
			while(rcvBuf[i]!='"')
			{
				out_sort<<rcvBuf[i]; 
				i++;
				if(i==max)
					break;
			}
			out_sort<<"\n";
		}
		i++;
	}
	out_sort.close();  
}

套接字与web服务器连接状态信息:


获取的网页信息:


获取的超链接地址:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值