CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Winsock <-> IP

  1. #1
    Join Date
    May 1999
    Posts
    19

    Winsock <-> IP

    I'm programming using VC++ 5.0.

    The program I'm writing is for both win95 and 98.
    The part of the program should show IP number of the computer.

    What is the best way to get IP adress, without external dlls'? So far, I have used:

    char szHostName[128];
    struct hostent * pHost;
    CString str = "";
    int nCheck = 0;
    WSADATA wsaData;
    /*
    WSAStartup (MAKEWORD( 1, 0 ), &wsaData);
    nCheck = gethostname (szHostName, 128);

    if( gethostname (szHostName, 128) == 0 ) {
    // Get host adresses
    int i;

    pHost = gethostbyname(szHostName);

    for( i = 0; pHost!= NULL && pHost-&gt;h_addr_list[i]!= NULL; i++
    int j;

    for( j = 0; j &lt; pHost-&gt;h_length; j++ ) {
    CString addr;
    if( j &gt; 0 ) str += ".";

    addr.Format("%u", (unsigned int)((unsigned
    char*)pHost-&gt;h_addr_list[i])[j]);
    str += addr;
    }



  2. #2
    Join Date
    Jul 1999
    Posts
    7

    Re: Winsock <-> IP

    hi,
    here is one method:


    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    char tmp[100];
    PHOSTENT hostinfo;
    CString m_MachineAddr , m_MachineIPAddr;

    gethostname(tmp,100);
    strcpy(m_MachineAddr.GetBuffer(1),tmp);

    hostinfo = gethostbyname(tmp);
    m_MachineIPAddr = inet_ntoa (*(struct in_addr *)*hostinfo-&gt;h_addr_list);



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured