QQ2004对IP地址显示的实现原理
<TABLE cellSpacing=0 cellPadding=0 width="100%" align=center border=0><TR>
<TD align=left width="83%" bgColor=#efefef>
文章标题:<B>QQ2004对IP地址显示的实现原理</B>
原 作 者:后街男孩-红
原 出 处:vczx.com
发 布 者:后街男孩-红
发布类型:原创
发布日期:2004-11-11
今日浏览:22
总 浏 览:776
</TD>
<TD vAlign=bottom align=left width="15%" bgColor=#efefef></TD></TR></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="96%" align=center border=0>
<TR>
<TD vAlign=top align=left bgColor=#ffffff>
<P><FONT size=2> 相信QQ2004大家玩过吧!其中对好友的IP和哪个地方的显示是不是很爽的啊。哪就跟我来让我看来看究竟。以下为实现原理。</FONT></P>
<P><FONT size=2> 首先载入Winsock动态库,代码如下:</FONT></P>
<P><FONT size=2> int CIPAddressDlg::StartUp()</FONT></P>
<P><FONT size=2> {</FONT></P>
<P><FONT size=2> WORD wVersionRequested;</FONT></P>
<P><FONT size=2> WSADATA wsaData;</FONT></P>
<P><FONT size=2> int err;</FONT></P>
<P><FONT size=2> wVersionReuqested=MAKEWORD(2,0);</FONT></P>
<P><FONT size=2> err=WSAStartup(wVersionReuqested, &wsaData);</FONT></P>
<P><FONT size=2> if(err!=0)</FONT></P>
<P><FONT size=2> {</FONT></P>
<P><FONT size=2> return err;</FONT></P>
<P><FONT size=2> }</FONT></P>
<P><FONT size=2> if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=0)</FONT></P>
<P><FONT size=2> {</FONT></P>
<P><FONT size=2> WSACleanup();</FONT></P>
<P><FONT size=2> return WSAVERNOTSUPPORTED;</FONT></P>
<P><FONT size=2> }</FONT></P>
<P><FONT size=2> return 0;</FONT></P>
<P><FONT size=2> }</FONT></P>
<P><FONT size=2>以下的GetLocalHostName()函数为现实获取计算机名称,</FONT></P>
<P><FONT size=2> int CIPAddressDlg::GetLocalHostName(CString&sHostName)</FONT></P>
<P><FONT size=2> {</FONT></P>
<P><FONT size=2> char szHostName;</FONT></P>
<P><FONT size=2> int nRectCode;</FONT></P>
<P><FONT size=2> nRectCode=gethostname(szHostName,sizeofa(szHostName));</FONT></P>
<P><FONT size=2> if(nRectCode!=0)</FONT></P>
<P><FONT size=2> {</FONT></P>
<P><FONT size=2> sHostName=_T("Not available");</FONT></P>
<P><FONT size=2> return WSAGetLastError();</FONT></P>
<P><FONT size=2> }</FONT></P>
<P><FONT size=2> sHostName=szHostName;</FONT></P>
<P><FONT size=2> return 0;</FONT></P>
<P><FONT size=2> }</FONT></P>
<P><FONT size=2>然后调用GetIPAddress来获取IP地址</FONT></P>
<P><FONT size=2> int CIPAddressDlg::GetIPAddress(const CString& sHostName, CString&sIPAddress)</FONT></P>
<P><FONT size=2> {</FONT></P>
<P><FONT size=2> struct hostent FAR *lpHostEnt=gethostbyname(sHostName);</FONT></P>
<P><FONT size=2> if(lpHostEnt==NELL)</FONT></P>
<P><FONT size=2> {</FONT></P>
<P><FONT size=2> sIPAddress=_T("");</FONT></P>
<P><FONT size=2> return WSAGetLastError();</FONT></P>
<P><FONT size=2> }</FONT></P>
<P><FONT size=2> LPSTR lpAddr=lpHostEnt->h_adr_list;</FONT></P>
<P><FONT size=2> if(lpAddr)</FONT></P>
<P><FONT size=2> {</FONT></P>
<P><FONT size=2> struct in_addr inAddr;</FONT></P>
<P><FONT size=2> memmove (&inAddr,lpAddr, 4);</FONT></P>
<P><FONT size=2> sIPAddress=inet_ntoa (inAddr);</FONT></P>
<P><FONT size=2> if(sIPAddress.IsEmpty())</FONT></P>
<P><FONT size=2> sIPAddress=_T("Not available");</FONT></P>
<P><FONT size=2> }</FONT></P>
<P><FONT size=2> return 0;</FONT></P>
<P><FONT size=2> }</FONT></P>
<P><FONT size=2>哈哈,一切就绪了,是不是很简单啊!</FONT></P>
<P><FONT size=2>本程序是VC++6.0下运行通过。</FONT></P></TD></TR></TABLE> <P>先顶一把,我还没涉及网络开发.</P> <P>不错</P><P>好东西</P> <P>见过,似乎是转贴把</P> <P>不管是不是转帖,希望好东西大家分享。</P> 看不懂。。。。。。。。。。。。。
页:
[1]