18,356
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA data = {0};
WSAStartup(0x202, &data);
SOCKET sck = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr_local = {0};
sockaddr_in addr_remote= {0};
addr_local.sin_family = AF_INET;
addr_remote.sin_family = AF_INET;
addr_remote.sin_port = htons(80);
//www.csdn.net这个域名可能对应多个ip
hostent* pEnt = ::gethostbyname("www.csdn.net");
DWORD **ppIPList = NULL;
int nIPListLen = 0;
if (pEnt)
{
ppIPList = (DWORD**)pEnt->h_addr_list;
int i = 0;
while(ppIPList[i]) ++i;
nIPListLen = i;
}
int nBadCnt = 0;
SOCKET sck = socket(AF_INET, SOCK_STREAM, 0);
if (nIPListLen > 0)
{
//随机选一个ip
addr_remote.sin_addr.s_addr = *ppIPList[GetTickCount()%nIPListLen];
}
bind(sck, (const sockaddr*)&addr_local, sizeof(addr_local));
int nRet = connect(sck, (const sockaddr*)&addr_remote, sizeof(addr_remote));
if (nRet > 0)
{
printf("连接成功\n");
}
else
{
printf("连接失败\n");
}
closesocket(sck);
WSACleanup();
return 0;
}
CInternetSession session;
CInternetFile *file=NULL;
try
{
file=(CHttpFile *)session.OpenURL("http://www.csdn.net");
}
catch(CInternetException *e)
{
file=NULL;
e->Delete();
AfxMessageBox("Open URL Error!");
return;
}
DWORD dwRet
((CHttpFile *)file)->QueryInfoStatusCode(dwRet);
if(dwRet==200)
AfxMessageBox("可以连接");