資源簡介
windows網絡編程課程設計源代碼
代碼片段和文件信息
//?getIp.cpp?:?定義控制臺應用程序的入口點。
//
//?winPrgrmSkts.cpp?:?Defines?the?entry?point?for?the?application.
#include?“stdafx.h“
#include
#include
#include
#include
#pragma?comment(lib“ws2_32.lib“)
int?main(int?argcchar?**argv){
//聲明和初始化變量
WSADATA?wsaData;
int?iResult;
DWORD?dwError;
int?i=0;
struct?hostent?*remoteHost;
char?*host_name;
struct?in_addr?addr;
char?**pAlias;
//驗證參數的合法性
if(argc?!=2){
?printf(“usage:GetHostIp?hostname:\n“);
?return?1;
}//if(argc?!=2)
//初始化windows?socket
iResult?=?WSAStartup(MAKEWORD(22)&wsaData);
if(iResult?!=0?){
printf(“WSAStartup?failed:%d\n“iResult);
return?1;
}//if(iResult?!=0?){}
host_name?=?argv[1];
//host_name=“www.google.com“;
printf(“Calling?gethostname?with?%s\n?“host_name);
????remoteHost?=?gethostbyname(host_name);
//對返回結果進行判斷
if(remoteHost==NULL){
dwError?=?WSAGetLastError();
??if(dwError?!=0){
????if(dwError?==?WSAHOST_NOT_FOUND){
printf(“Host?not?found\n“);
return?1;
}else?if(dwError==WSANO_DATA){
printf(“No?data?record?found\n“);
return?1;
}else?{
printf(“Function?failed?with?error:%d\n“dwError);
return?1;
}
}//if(dwError?!=0)
}//if(remoteHost==NULL)
- 上一篇:c++ 最小二乘算法擬合球心 使用Vs2008
- 下一篇:編譯原理 電梯程序
評論
共有 條評論