资源简介
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
- 下一篇:编译原理 电梯程序
评论
共有 条评论