请教gethostbyaddr的参数

dotnba 2002-11-27 10:33:50
struct hostent FAR * gethostbyaddr (
const char FAR * addr,
int len,
int type
);
Parameters
addr
[in] A pointer to an address in network byte order.
len
[in] The length of the address.
type
[in] The type of the address.
addr指的是IP地址吗?如果不是那是什么?
len是不是 strlen(addr)?如果不是那是什么?
若addr是IP地址,那么type是不是 AF_INET?如果不是那是什么?

我对C和winsock都不熟悉。请详细指点。
...全文
83 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bind 2002-11-28
  • 打赏
  • 举报
回复
SORRY,更正一下
int addr = inet_addr("127.0.0.1");
Bind 2002-11-28
  • 打赏
  • 举报
回复
INT addr = htonl("127.0.0.1");
hostent* pHost = gethostbyaddr(&addr, sizeof(addr), AF_INET);

kingzai 2002-11-28
  • 打赏
  • 举报
回复
Some applications use the gethostbyaddr( ) function to resolve an IP address to a host name. The gethostbyaddr( ) call uses the following (default) sequence:

Check the hosts file for a matching address entry.
If a DNS server is configured, query it.
Send a NetBIOS Adapter Status Request to the IP address being queried, and if it responds with a list of NetBIOS names registered for the adapter, parse it for the computer name.
kingzai 2002-11-28
  • 打赏
  • 举报
回复
//you may initialize socket,such as
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return 0;
}
dotnba 2002-11-28
  • 打赏
  • 举报
回复
谢谢:问题解决
copy_paste 2002-11-27
  • 打赏
  • 举报
回复
char* gethost(int s)
{
int size;
char host[30];
struct sockaddr_in addr;
struct hostent *pHostent;
::ZeroMemory(host, sizeof(host));
if (SOCKET_ERROR != getpeername(s, &addr, sizeof(addr)))
{
pHostent = gethostbyaddr(&addr.sin_addr.s_addr, sizeof(int), PF_INET);
if (pHostent)
strcpy(host, pHostent->h_name);
}
return host;
}
dotnba 2002-11-27
  • 打赏
  • 举报
回复
另外,NetBEUI协议的话,type应该是什么?
type
[in] The type of the address
这个type是不是跟addr有关啊?
如果type == AF_ISO,那么相应的addr是不是应该是另外一种形式,是什么?
dotnba 2002-11-27
  • 打赏
  • 举报
回复
您批评的是.
struct hostent * pHostent
pHostent = gethostbyaddr("192.168.192.1",strlen("192.168.192.1"),AF_INET)
if (pHostent == NULL) return "Error";
//Codes Goes Here
//....
为什么我的返回值是NULL呢?
zhao_as 2002-11-27
  • 打赏
  • 举报
回复
addr指的是IP地址,但不光ip地址,
len是 strlen(addr)
type在用IP的所有协议中是 AF_INET
但这个函数 不光是tcp/ip可以用 用它的多了
既然都不懂 那干吗不从头来看呢?

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧