您的位置:程序门 -> vc/mfc -> 网络编程



问一下,使用socket中的send和recv函数到底能不能得到网页的html代码?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


问一下,使用socket中的send和recv函数到底能不能得到网页的html代码?
发表于:2008-02-26 15:25:43 楼主
问一下,使用socket中的send和recv函数到底能不能得到网页的html代码?

比如放在recv函数中的参数变量中。

如果能,怎么做?

如下是我的一个代码例子,但得出来的是什么也没有,请各位帮忙看看:

//   initialize   winsock
wsadata   wsadata;
        int   iresult   =   wsastartup(makeword(2,2),   &wsadata);
        if   (iresult   !=   no_error)
{
afxmessagebox(l"error   at   wsastartup().");
}

//create   a   socket   for   connecting   to   server
socket   connectsocket;
connectsocket   =   socket(af_inet,sock_stream,ipproto_tcp);

if(connectsocket   ==   invalid_socket)
{
afxmessagebox(l"invalid   socket.");
int   i   =   wsagetlasterror();
cstring   str;
str.format(l"%ld",i);
afxmessagebox(str);

//this   function   terminates   use   of   the   ws2.dll.
wsacleanup();
//
//wsaenetdown
}
//   the   sockaddr_in   structure   specifies   the   address   family,
        //   ip   address,   and   port   of   the   server   to   be   connected   to.
sockaddr_in   clientservice;  
        clientservice.sin_family   =   af_inet;
clientservice.sin_addr.s_addr   =   inet_addr("59.60.28.117");//www.google.com.cn
clientservice.sin_port   =   htons(   80   );

//inaddr_none

//   connect   to   server.
if(connect(connectsocket,(sockaddr*)   &clientservice,   sizeof(clientservice))   ==   socket_error   )
{
afxmessagebox(l"connect   error.");
wsacleanup();
}

//   declare   and   initialize   variables.
        int   bytessent;
        int   bytesrecv   =   socket_error;
        char   sendbuf[32]   =   "client:   sending   data.";
        char   recvbuf[32]   =   "";

//   send   and   receive   data.
bytessent   =   send(   connectsocket,sendbuf,strlen(sendbuf),0);
if(bytessent   ==   socket_error)
{
afxmessagebox(l"send   error.");
wsacleanup();
}
cstring   sendstr;
sendstr.format(l"%d",bytessent);
afxmessagebox(l"send   bytes:");
afxmessagebox(sendstr);

//
bytesrecv   =   recv(connectsocket,recvbuf,32,0   );
cstring   recvstr;
recvstr.format(l"%s",recvbuf);
afxmessagebox(recvstr);

//afxmessagebox((lpctstr)recvbuf);

        if   (   bytesrecv   ==   0   ¦ ¦   bytesrecv   ==   wsaeconnreset   )  
{
            afxmessagebox(   l"connection   closed.");            
        }    
wsacleanup();


发表于:2008-02-26 16:26:581楼 得分:0
简单回答,能够得到。
发表于:2008-02-26 16:32:172楼 得分:0
连上www.google.com.cn以后发送如下串过去:
batchfile code
get / http/1.1 accept: */* content-type: application/x-www-form-urlencoded ua-cpu: x86 accept-encoding: gzip, deflate user-agent: mozilla/4.0 compatible; msie 7.0; windows nt 5.2; .net clr 1.1.4322; .net clr 2.0.50727; .net clr 3.0.04506.30) host: www.google.com.cn connection: closed cache-control: no-cache

最后是两个换行符,表示http协议的结束符。
发表于:2008-02-26 16:34:193楼 得分:0
既然是测试,建议还是换成连接www.google.cn,连接www.google.com.cn是一个跳转
batchfile code
http/1.1 301 moved permanently location: http://www.google.cn/ set-cookie: pref=id=70012a421866faae:nw=1:tm=1204014672:lm=1204014672:s=htzutgaz6kq78enr; expires=thu, 25-feb-2010 08:31:12 gmt; path=/; domain=.google.com content-type: text/html server: gws transfer-encoding: chunked content-encoding: gzip date: tue, 26 feb 2008 08:31:12 gmt cache-control: private, x-gzip-ok="" ...
发表于:2008-02-26 16:36:294楼 得分:0
ie   还不是用的   socket啊!
发送   http协议  
发表于:2008-02-26 16:47:195楼 得分:0
谢谢!

还有一个问题如果网址类似于这样的呢,例如:http://www.google.com.cn   在代码中怎么表示?

而不是如这样表示:59.60.28.117  

发表于:2008-02-26 16:57:366楼 得分:0
标准的请求为如下格式,http当中协议可以省,默认为http,这个协议除了说明所使用的协议外,也有一个相应的默认端口,除非有指明端口。
协议://主机名.域名:端口/路径/文件名.扩展名

先分离成:
协议{默认为http}
主机名.域名{必须}
端口{默认http为80}
/路径/文件名.扩展名{默认为一个斜杠(/)}

解析:主机名.域名
连接:解析出来的ip地址的相应端口
发送get请求:
get   /路径/文件名.扩展名   http   /1.0
...

接收数据
发表于:2008-02-26 17:01:267楼 得分:0
奇怪。

我怎么看不到recvstr输出的结果。?


afxmessagebox(recvstr);   弹出的对话框是个空,什么内容也没有。


请问怎么回事?


快速检索

最新资讯
热门点击