none
connect to my own ip address

    Question

  • Hi
          how can i connect to my own system using windows sockets.i dont want to connect via 127.0.0.1

    I have tried connecting using my ip address which i have found over the net

    but i am unable to connect.?????????
    Monday, January 25, 2010 12:46 PM

Answers

  • The IP you got from the internet might not be your machine IP, depending on the network you are on.  For example if you are at work or something, that will be the external IP address of your company.

    Anyway use this to get your local IP

     

    CString LSSocket::QueryLocalIPAddress()
    {
       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();
       } 
    
       return (IP);
    }
    

     

     


    Ali
    Monday, January 25, 2010 3:02 PM

All replies

  • Why you don't want to use 127.0.0.1?
    In all cases you need to provide IP in sockaddr struct for connect function.
    If you have static IP, you can use it.
    Monday, January 25, 2010 12:57 PM
  • I have an ip which changes daily..
    Monday, January 25, 2010 1:20 PM
  • The IP you got from the internet might not be your machine IP, depending on the network you are on.  For example if you are at work or something, that will be the external IP address of your company.

    Anyway use this to get your local IP

     

    CString LSSocket::QueryLocalIPAddress()
    {
       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();
       } 
    
       return (IP);
    }
    

     

     


    Ali
    Monday, January 25, 2010 3:02 PM
  • I agree with Nikita, that 127.0.0.1 (the loopback address) is the easiest way to go. 

    But I disagree with hard coding the static IP in a program.  Use the QueryLocalIP method instead.

    If you want to send your local IP to someone else then that's another story.
    Ali
    • Edited by Ali Rafiee Monday, January 25, 2010 5:01 PM Forgot about Static IP suggestion.
    Monday, January 25, 2010 3:25 PM
  • Are you trying to connect to your PC from another network? (That is a very relevant bit that you have left out of your post...why?)

    Your IP is only valid on your intranet; it is not possible for someone else to use your private IP directly without going through the gateway for your network.

    The path that you seek would need the cooperation of your router, and an address to "punch through" your router to your private network. (A port would suffice if you used port forwarding on your router.)

    If you are trying to connect to your PC from within your network, that should work fine.
    Monday, January 25, 2010 3:40 PM