Thread: systray app to get IP Address

  1. #16
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Originally posted by anonytmouse
    char port='7';
    rc = getaddrinfo(hostname,&port,NULL,&res);

    servname
    [in] Pointer to a null-terminated string containing either a service name or port number.

    You are not passing a proper port string to getaddrinfo.
    I made this change, but I still get the same result.

    I didn't use gethostbyname becase the manual pages say that it is depricated by getaddrinfo.

    I know I'm closer than I have been before. I will try and get this working today. Thanks for your help.
    Best Regards,

    Bonkey

  2. #17
    Registered User
    Join Date
    Aug 2002
    Posts
    170

    Thumbs up

    It works!

    What I ended up having to do was cast between a winsock 1 and winsock 2 structure to get access to the in_addr structure that the inet_ntoa function needed.

    Here is my working code:

    Code:
    	WORD wVersionRequested;
    	WSADATA wsaData;
    	int err;
     
    	wVersionRequested = MAKEWORD( 2, 2 );
     
    	err = WSAStartup( wVersionRequested, &wsaData );
    	if ( err != 0 ) {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        return;
    	}
    
    	char host[255];
    	char *hostname=&host[0];
    	memset(hostname,NULL,255);
    	gethostname(hostname,255);
    
    	struct addrinfo *res = NULL;
        int             rc;
    
    	char *port=new char[25];
    	memset(port,NULL,25);
    	strcpy(port,"7");
    
    	rc = getaddrinfo(hostname,port,NULL,&res);
        if (rc != 0)
           fprintf(stderr, "Invalid address %s, getaddrinfo failed: %d\n", hostname, rc);
    	struct sockaddr_in *myip;
    	myip=(sockaddr_in *)res->ai_addr;
    	char *ipaddr=new char[16];
    	memset(ipaddr,NULL,16);
    	sprintf(ipaddr,"%s",inet_ntoa( myip->sin_addr ));
    	delete [] ipaddr;
    	delete [] port;
    Best Regards,

    Bonkey

  3. #18
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Originally posted by _Elixia_
    Maybe then that you have an old version of ssh. When I ssh into my shell account here, this is what I get:

    bash-2.05$ who -m
    tribal.metalab.unc.edu!jamesp pts/13 Sep 18 18:30 (du-018-0066.claranet.co.uk)
    bash-2.05$
    It does work on Linux. But It doesn't work on SUN, IBM, or HP UNIX.
    Best Regards,

    Bonkey

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  2. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  3. how get ip address on 2 network cards
    By akesayyo in forum C Programming
    Replies: 8
    Last Post: 07-04-2005, 10:15 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. MSN Vital Information
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-22-2001, 08:55 PM