get ip address

作者在 2008-05-01 16:06:59 发布以下内容
Class CSocket derives from CAsyncSocket and inherits its encapsulation of the Windows Sockets API,因此这个软件使用的是异步(非阻塞)方式实现文件的传输

CWindow::GetDlgItem

This method retrieves the specified child window.

The GetDlgItem method only works for immediate child controls of a dialog box—it does not search through nested dialog boxes.用这个获取指定子窗口,以便对其设置状

Use the GetLocalName method to retrieve the local name of the file.

The gethostname function returns the standard host name for the local machine.

GetLocalHostAddressesRetrieves the local addresses being used to host the session.

The gethostbyname function retrieves host information corresponding to a host name from a host database.
首先载入Winsock动态库,代码如下:

int CIPAddressDlg::StartUp()
{
 WORD wVersionRequested;
 WSADATA wsaData;
 int err;

 wVersionReuqested=MAKEWORD(2,0);
 err=WSAStartup(wVersionReuqested, &wsaData);
 if(err!=0)
 {
  return err;
 }
 if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=0)
 {
  WSACleanup();
  return WSAVERNOTSUPPORTED;
 }
 return 0;
}
 

  以下的GetLocalHostName()函数为现实获取计算机名称,

int CIPAddressDlg::GetLocalHostName(CString&sHostName)
{
 char szHostName[256];
 int nRectCode;
 nRectCode=gethostname(szHostName,sizeofa(szHostName));
 if(nRectCode!=0)
 {
  sHostName=_T("Not available");
  return WSAGetLastError();
 }
 sHostName=szHostName;
 return 0;
}

  然后调用GetIPAddress来获取IP地址

int CIPAddressDlg::GetIPAddress(const CString& sHostName, CString&sIPAddress)
{
 struct hostent FAR *lpHostEnt=gethostbyname(sHostName);
 if(lpHostEnt==NELL)
 {
  sIPAddress=_T("");
  return WSAGetLastError();
 }
 LPSTR lpAddr=lpHostEnt->h_adr_list[0];
 if(lpAddr)
 {
  struct in_addr inAddr;
  memmove (&inAddr,lpAddr, 4);
  sIPAddress=inet_ntoa (inAddr);
  if(sIPAddress.IsEmpty())
   sIPAddress=_T("Not available");
 }
 return 0;
}




技术 | 阅读 2800 次
文章评论,共2条
vfdff(作者)
2008-05-01 21:15
1
TCP与UDP的区别

       1. 基于连接与无连接
       2. 对系统资源的要求(TCP较多,UDP少)
       3. UDP程序结构较简单
       4. 流模式与数据报模式

            TCP保证数据正确性,UDP可能丢包
            TCP保证数据顺序,UDP不保证

 

具体编程时的区别

       1. socket()的参数不同
       2. UDP Server不需要调用listen和accept
       3. UDP收发数据用sendto/recvfrom函数
       4. TCP:地址信息在connect/accept时确定
          UDP:在sendto/recvfrom函数中每次均 需指定地址信息
       5. UDP:shutdown函数无效
vfdff(作者)
2008-05-01 21:32
2
shutdown
The shutdown function disables sends or receives on a socket.
notes: The  shutdown  function does not close the socket. Any resources attached to the socket will not be freed until closesocket is invoked.
游客请输入验证码
浏览1941091次