#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
#include "iostream.h"
#include "stdio.h"
initsock()
{WSADATA wsData;
if(WSAStartup(MAKEWORD(2,2),&wsData)==0)
cout<<"success"<<endl;;
return 0;}
initaddress(sockaddr_in *pa)
{
pa->sin_family=AF_INET;
pa->sin_port=60002;
hostent *hs=gethostbyname("www.baidu.com");
if( hs==NULL){cout<<"NO TIME"<<endl;return 0;}
in_addr ip=*(in_addr *)hs->h_addr_list[0];
pa->sin_addr=ip;
cout<<"www.baidu.com"<<inet_ntoa(ip)<<endl;
return 0;
}
int main(int argc, char* argv[])
{initsock();
sockaddr_in addr;
initaddress(&addr);
SOCKET licon;
char buf[1024]={0};
licon=socket(AF_INET,SOCK_STREAM,0);
int q=connect(licon,(sockaddr *)&addr,sizeof(addr));
if(q==SOCKET_ERROR)
{cout<<GetLastError();}
sprintf(buf,"GET http://www.baidu.com/index.html HTTP/1.0\r\n");
send(licon,buf,strlen(buf),0);
sprintf(buf,"host: www.baidu.com\r\n");
send(licon,buf,strlen(buf),0);
sprintf(buf,"\r\n");
send(licon,buf,strlen(buf),0);
int ret;
ret=recv(licon,buf,sizeof(buf),0);
cout<<"OOOOO"<<buf<<")))))))))))))"<<endl;
return 0;
}
11 个解决方案
#1
可能是IP端口错了,或者是服务器不让连接
#2
应该是端口不对,你确定baidu开了这个端口么
#3
pa->sin_port=60002;
应改为 pa->sin_port = htons(60002);
应改为 pa->sin_port = htons(60002);
#4
pa->sin_addr=ip;
好像应改为pa->sin_addr.s_addr=ip;
好像应改为pa->sin_addr.s_addr=ip;
#5
10060表示连接超时,就是连接不上,对应的ip地址和端口服务器没响应
你确定你可以这样连接到百度? 或者说你端口正确?
你确定你可以这样连接到百度? 或者说你端口正确?
#6
#7
pa->sin_port=htons(60002);
#8
pa->sin_addr=ip; //对的
#9
请高手!!改成80也不行啊
#10
OK 了
#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
#include "iostream.h"
#include "stdio.h"
initsock()
{
WSADATA wsData;
if(WSAStartup(MAKEWORD(2,2),&wsData)==0)
cout<<"success"<<endl;;
return 0;}
initaddress(sockaddr_in *pa)
{
pa->sin_family=AF_INET;
pa->sin_port= htons(80); // 60002;
hostent *hs=gethostbyname("www.baidu.com");
if( hs==NULL)
{
cout<<"NO TIME"<<endl;return 0;
}
in_addr ip=*(in_addr *)hs->h_addr_list[0];
pa->sin_addr=ip;
cout<<"www.baidu.com"<<inet_ntoa(ip)<<endl;
return 0;
}
int main(int argc, char* argv[])
{
initsock();
sockaddr_in addr;
initaddress(&addr);
SOCKET licon;
char buf[1024]={0};
licon=socket(AF_INET,SOCK_STREAM,0);
int q=connect(licon,(sockaddr *)&addr,sizeof(addr));
if(q==SOCKET_ERROR)
{
cout<<GetLastError();
}
sprintf(buf,"GET http://www.baidu.com/index.html HTTP/1.0\r\n");
send(licon,buf,strlen(buf),0);
sprintf(buf,"host: www.baidu.com\r\n");
send(licon,buf,strlen(buf),0);
sprintf(buf,"\r\n");
send(licon,buf,strlen(buf),0);
int ret;
ret=recv(licon,buf,sizeof(buf),0);
cout<<"OOOOO"<<buf<<")))))))))))))"<<endl;
return 0;
}
#11
pa->sin_port=60002;
注意端口的网络字节序
注意端口的网络字节序
#1
可能是IP端口错了,或者是服务器不让连接
#2
应该是端口不对,你确定baidu开了这个端口么
#3
pa->sin_port=60002;
应改为 pa->sin_port = htons(60002);
应改为 pa->sin_port = htons(60002);
#4
pa->sin_addr=ip;
好像应改为pa->sin_addr.s_addr=ip;
好像应改为pa->sin_addr.s_addr=ip;
#5
10060表示连接超时,就是连接不上,对应的ip地址和端口服务器没响应
你确定你可以这样连接到百度? 或者说你端口正确?
你确定你可以这样连接到百度? 或者说你端口正确?
#6
#7
pa->sin_port=htons(60002);
#8
pa->sin_addr=ip; //对的
#9
请高手!!改成80也不行啊
#10
OK 了
#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
#include "iostream.h"
#include "stdio.h"
initsock()
{
WSADATA wsData;
if(WSAStartup(MAKEWORD(2,2),&wsData)==0)
cout<<"success"<<endl;;
return 0;}
initaddress(sockaddr_in *pa)
{
pa->sin_family=AF_INET;
pa->sin_port= htons(80); // 60002;
hostent *hs=gethostbyname("www.baidu.com");
if( hs==NULL)
{
cout<<"NO TIME"<<endl;return 0;
}
in_addr ip=*(in_addr *)hs->h_addr_list[0];
pa->sin_addr=ip;
cout<<"www.baidu.com"<<inet_ntoa(ip)<<endl;
return 0;
}
int main(int argc, char* argv[])
{
initsock();
sockaddr_in addr;
initaddress(&addr);
SOCKET licon;
char buf[1024]={0};
licon=socket(AF_INET,SOCK_STREAM,0);
int q=connect(licon,(sockaddr *)&addr,sizeof(addr));
if(q==SOCKET_ERROR)
{
cout<<GetLastError();
}
sprintf(buf,"GET http://www.baidu.com/index.html HTTP/1.0\r\n");
send(licon,buf,strlen(buf),0);
sprintf(buf,"host: www.baidu.com\r\n");
send(licon,buf,strlen(buf),0);
sprintf(buf,"\r\n");
send(licon,buf,strlen(buf),0);
int ret;
ret=recv(licon,buf,sizeof(buf),0);
cout<<"OOOOO"<<buf<<")))))))))))))"<<endl;
return 0;
}
#11
pa->sin_port=60002;
注意端口的网络字节序
注意端口的网络字节序