gethostname The gethostname function retrieves the standard host name for the local computer.int gethostname( char* name, int namelen );Parameters name [out] Pointer to a buffer that receives the local host name. namelen [in] Length of the buffer, in bytes. Return Values If no error occurs, gethostname returns zero. Otherwise, it returns SOCKET_ERROR and a specific error code can be retrieved by calling WSAGetLastError.
The gethostname function retrieves the standard host name for the local computer.int gethostname(
char* name,
int namelen
);Parameters
name
[out] Pointer to a buffer that receives the local host name.
namelen
[in] Length of the buffer, in bytes.
Return Values
If no error occurs, gethostname returns zero. Otherwise, it returns SOCKET_ERROR and a specific error code can be retrieved by calling WSAGetLastError.
getpeername 函数的使用方法可以查看MSDN。
l 连接ws2_32.lib和 mpr.lib库
l #include winsock2.h
CString strTemp;
struct hostent *host;struct in_addr *ptr; // 检索IP地址 DWORD dwScope = RESOURCE_CONTEXT;
NETRESOURCE *NetResource = NULL;
HANDLE hEnum;
WNetOpenEnum( dwScope, NULL, NULL,
NULL, &hEnum );WSADATA wsaData;
WSAStartup(MAKEWORD(1,1),&wsaData);if ( hEnum )
{
DWORD Count = 0xFFFFFFFF;
DWORD BufferSize = 2048;
LPVOID Buffer = new char[2048];
WNetEnumResource( hEnum, &Count,
Buffer, &BufferSize );
NetResource = (NETRESOURCE*)Buffer; char szHostName[200];
unsigned int i; for ( i = 0;
i < BufferSize/sizeof(NETRESOURCE);
i++, NetResource++ )
{
if ( NetResource->dwUsage ==
RESOURCEUSAGE_CONTAINER &&
NetResource->dwType ==
RESOURCETYPE_ANY )
{
if ( NetResource->lpRemoteName )
{
CString strFullName =
NetResource->lpRemoteName;
if ( 0 ==
strFullName.Left(2).Compare("\\\\") )
strFullName =
strFullName.Right(
strFullName.GetLength()-2); gethostname( szHostName,
strlen( szHostName ) );
host = gethostbyname(strFullName); if(host == NULL) continue;
ptr = (struct in_addr *)
host->h_addr_list[0]; // =. 分隔开IP:211.40.35.76.
int a = ptr->S_un.S_un_b.s_b1; // 211
int b = ptr->S_un.S_un_b.s_b2; // 40
int c = ptr->S_un.S_un_b.s_b3; // 35
int d = ptr->S_un.S_un_b.s_b4; // 76 strTemp.Format("%s --> %d.%d.%d.%d",
strFullName,a,b,c,d);
AfxMessageBox(strTemp);
}
}
} delete Buffer;
WNetCloseEnum( hEnum );
}WSACleanup();
hostent *pHost;
in_addr addr;
addr.S_un.S_addr = inet_addr("192.168.0.1");
pHost = gethostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
AfxMessageBox(pHost->h_name);
{
struct sockaddr_in sockaddr;
int nlen = sizeof(sockaddr); memset(&sockaddr, 0, sizeof(sockaddr)); getpeername(m_socket, (struct sockaddr*)&sockaddr, &nlen); m_straddr = inet_ntoa(sockaddr.sin_addr); return m_straddr.data();
}
getpeername()函数
这个函数太简单了。
它太简单了,以至我都不想单列一章。但是我还是这样做了。 函数 getpeername()
告诉你在连接的流式套接字上谁在另外一边。函 数是这样的:
#include <sys/socket.h>
int getpeername(int sockfd, struct sockaddr *addr,
int *addrlen);
sockfd 是连接的流式套接字的描述符。addr 是一个指向结构 struct sockaddr
(或者是 struct sockaddr_in) 的指针,它保存着连接的另一边的
信息。addrlen 是一个 int 型的指针,它初始化为 sizeof(struct
sockaddr)。 函数在错误的时候返回 -1,设置相应的 errno。
一旦你获得它们的地址,你可以使用 inet_ntoa() 或者 gethostbyaddr()
来打印或者获得更多的信息。但是你不能得到它的帐号。(如果它运行着愚
蠢的守护进程,这是可能的,但是它的讨论已经超出了本文的范围,请参 考 RFC-1413
以获得更多的信息。)