Jump to content


very simple question


5 replies to this topic

#1 starboarder2001

    Member

  • Members
  • PipPip
  • 35 posts

Posted 15 July 2003 - 03:25 AM

in c++ what function will return your IP address? I have searched for it but no luck. I figured you guys would know it off the top of your heads.
1000
0100
0010
0001

#2 Dia

    DevMaster Staff

  • Administrators
  • 1089 posts

Posted 15 July 2003 - 03:53 AM

If you're using windows, you can get your IP address using WinSock. You can't get the IP address in "C/C++". You'll have to use a library.

      #include <winsock2.h>

      WORD wVersionRequested;
      WSADATA wsaData;
      char name[255];
      CString ip;
      PHOSTENT hostinfo;
      wVersionRequested = MAKEWORD( 2, 0 );

      if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
      {

            if( gethostname ( name, sizeof(name)) == 0)
                  if((hostinfo = gethostbyname(name)) != NULL)
                        ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
           
            WSACleanup( );
      } 

don't forget to link with Wsock32.lib

#3 MA-Mestre

    New Member

  • Members
  • PipPip
  • 14 posts

Posted 15 July 2003 - 08:10 AM

if u use apex's code ( into VC6.0 ), and u need #include <windows.h> u must put #include <winsock2.h> before windows include. Otherwise the compiler found data types repeated. ;)

#4 starboarder2001

    Member

  • Members
  • PipPip
  • 35 posts

Posted 19 July 2003 - 06:12 AM

I have another question.... :)

I am using WinSock.

When i call: recv() it waits at that line until it is sent something. Is there a way i can set the function to time out if nothing is sent after a few secs? I am writing a simple chat program..and i need it to check if something was recieved..and if it didnt then just contenue with the program.

//take input
//send input
//recieve text //i dont need it to wait here until it is sent something :\
//display text
1000
0100
0010
0001

#5 Obunako

    New Member

  • Members
  • Pip
  • 8 posts

Posted 19 July 2003 - 09:47 AM

Using WSAAsyncSelect u can recive a message to a window procedure when u get some data, or other network event, about u socket. Try to use it !!! :nod:

#6 DrunkenCoder

    Member

  • Members
  • PipPip
  • 97 posts

Posted 21 July 2003 - 03:28 PM

or have a look at select() ...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users