gethostbyname

【网络编程】域名解析

妖精的绣舞 提交于 2020-11-16 01:50:46
项目中有这种场景:C/S通过RPC方式通信。服务端提供一个域名(如:www.xxx.xxx.com),客户端每次请求需要将该域名转换成相应的IP,然后才可以发起请求。这里做一个小小的总结。 #include <stdio.h> #include <unistd.h> #include <netdb.h> #include <arpa/inet.h> #include <errno.h> int main(int argc, char *argv[]) { // 域名解析 char host_name[256] = "www.test.xxx.xxx.com"; // xxx应替换成实际域名 struct hostent *hptr; hptr = gethostbyname(host_name); if(NULL == hptr) { perror("dsn error"); exit(1); } char ip[256] = {0,}; if(NULL == inet_ntop(hptr->h_addrtype, hptr->h_addr, ip, sizeof(ip))) // 项目中一般是这个,首选IP { perror("inet_ntop error."); exit(1); } printf("ip:%s\n", ip); return 0; } 编译并运行: gcc

PHP: gethostbyname bug

只谈情不闲聊 提交于 2020-03-01 01:54:51
问题 I am using gethostbyname() to get the ip address of domains in an application. In some cases invalid addresses like '50.9.49' are checked also. echo gethostbyname('50.9.49'); // returns 50.9.0.49 In this cases gethostbyname should return false or the unmodified invalid ip address. however the functions returns the modified IP address 50.9.0.49 . Looks like a bug in php. The quick fix seems to be to check for invalid numerical addresses before, are there any other suggestions? 回答1: PHP's

gethostbyname, gethostbyaddr

☆樱花仙子☆ 提交于 2020-02-09 01:29:29
gethostbyname取ip地址, gethostbyaddr取域名 #include <iostream> // #ifndef WIN32_LEAN_AND_MEAN // #define WIN32_LEAN_AND_MEAN // #endif #include <winsock2.h> #include <windows.h> #include <conio.h> #pragma comment(lib,"ws2_32.lib") using std::cout; using std::endl; using std::cerr; int main(int argc,char *argv[],char *envp[]) { int iretcode = 0; const char *pip_addr = "www.baidu.com";//or "220.181.38.148";// WSADATA wsa = {0}; iretcode = WSAStartup(MAKEWORD(2,2),&wsa); if(iretcode != 0) return iretcode;// WSAStartup function error. /////////////////////////////////////////////// // get address dwip

网络信息检索

大城市里の小女人 提交于 2020-01-28 13:53:31
网络信息检索函数 man gethostbyname 1 #include <netdb.h> 2 extern int h_errno; //错误号 3 4 //name一般为域名,通过域名获取主机相关信息 5 struct hostent *gethostbyname(const char *name); 6 7 #include <sys/socket.h> /* for AF_INET */ 8 struct hostent *gethostbyaddr(const void *addr, 9 socklen_t len, int type); 10 11 void sethostent(int stayopen); 12 13 void endhostent(void); //释放hostent结构体的变量值 14 15 void herror(const char *s); //打印出错信息 16 17 const char *hstrerror(int err); //打印出错信息 18 19 /* System V/POSIX extension */ 20 struct hostent *gethostent(void); 21 //返回值:成功返回结构体指针hostent, 错误返回一个空指针。 22 23 The hostent structure is

gethostbyname

时光毁灭记忆、已成空白 提交于 2020-01-23 18:25:46
#include "stdafx.h" #include <stl_h.hpp> #include <CInitSocket.hpp> #include <winsock2.h> #include <ws2tcpip.h> #pragma comment(lib,"ws2_32.lib") CInitSocket init; using namespace std; int main(int argc, char **argv) { char *ptr,**pptr; struct hostent *hptr; char str[32]; /* 取得命令后第一个参数,即要解析的域名或主机名 */ ptr = "shell.cjb.net"; /* 调用gethostbyname()。调用结果都存在hptr中 */ if( (hptr = gethostbyname(ptr) ) == NULL ) { printf("gethostbyname error for host:%s\n", ptr); return 0; /* 如果调用gethostbyname发生错误,返回1 */ } /* 将主机的规范名打出来 */ printf("official hostname:%s\n",hptr->h_name); /* 主机可能有多个别名,将所有别名分别打出来 */ for(pptr

C语言gethostbyname用法及使用inet_ntoa时的一个问题

北战南征 提交于 2020-01-09 20:11:21
gethostbyname 用法 众所周知我们可以使用gethostbyname方法通过域名获取目标地址。 struct hostent { char * h_name ; /* official name of host */ char * * h_aliases ; /* alias list */ int h_addrtype ; /* host address type */ int h_length ; /* length of address */ char * * h_addr_list ; /* list of addresses */ } # define h_addr h_addr_list[0] /* for backward compatibility */ struct hostent * gethostbyname ( const char * name ) ; 通过该函数可以返回一个包含域名服务器地址信息的结构体。下面的代码说明一下用法。 # include <stdio.h> # include <netdb.h> # include <arpa/inet.h> # define URL "blog.csdn.net" int main ( ) { int i = 0 ; int fd = 0 ; struct sockaddr_in

Alternative to gethostbyname

℡╲_俬逩灬. 提交于 2020-01-02 03:46:08
问题 I can't use gethostbyname to grab a host's IP address, it is a deprecated function that only works 10% of the fricken' time on Windows! I can't find any adequate resources on other ways to find a host's IP address using other functions (MSDN recommended getaddrinfo but that doesn't seem like what I want.) 回答1: Actually, getaddrinfo is what you want. It's just a long-winded way to do it, not to mention the fact that sometimes multiple IP addresses are found (e.g. one IPv4 address and one IPv6

gethostbyname xcode issues

风格不统一 提交于 2019-12-31 05:19:47
问题 Hey guys, I'm new at this stuff, but I'm trying to use gethostname() to work using xcode and the ipad. I've tried a couple of things, but they all seem to freeze up. So when I just do: host = gethostbyname("website.com"); that works fine. I'd like to be able to change the website from within the program though. I've tried: const char *server = [Website_NSString UTF8String]; host = gethostbyname(server); const char *server = [Website_NSString cStringUsingEncoding:NSASCIIStringEncoding]; host =

Traceback (most recent call last): File “”, line 1, in AttributeError: module 'socket' has no attribute 'close'

烂漫一生 提交于 2019-12-24 19:48:01
问题 I need to write a program that retrieves the IP addresses of a list of domain names. The simple example can be shown here: >>> import socket >>> socket.gethostbyname('google.com') '172.217.160.14' >>> socket.close() After I try to close the socket, I get this error: Traceback (most recent call last): File "", line 1, in AttributeError: module 'socket' has no attribute 'close' How can I close this socket? I need to close it because my actual program has loop where in each domain name in the

Perl Host to Ip Resolution

为君一笑 提交于 2019-12-11 04:46:54
问题 I am wanting to resolve a host name to an ip address which is all fine using Socket with the following: $ip = gethostbyname($host) or die "Can't resolve ip for $host.\n"; $ip = inet_ntoa(inet_aton($host)); This works fine until it hits a host name that no longer resolves to an IP and the code just stops. How can I get my script to continue processing the remainder ip the host names to be resolved. Ideally I would simply set the $ip variable to equal "" . I have tried even without the die