Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows API
  4. Winsock - Connect to Internet through specified adapter

Winsock - Connect to Internet through specified adapter

Scheduled Pinned Locked Moved Windows API
questioncomsysadminhelp
11 Posts 4 Posters 32 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M MrDooDoo

    Hi everyone, I try to develop a network program using WINSOCK. I am trying to connect to the internet from a laptop with some network adapters ( LAN, WIRELESS,A USB ADSL Modem). It sounds, when I call connect function, it tries to connect using LAN, so because LAN is not connected it fails. Here are my questions: 1)How can I force a socket to use a specified Network Adapter? 2)How can I find which adapter is connected to internet? Thanks for reading and answering :) Here is my code, the connect function fails (err code :10060 )

    //The function for handling HTTP
    BOOL HTTPSubmitForm(char* HostName, char* Action,char* ObjectName, char* Headers,int HeadersLen, char* Body,int BodyLen)
    {
    SOCKET conn;
    struct hostent* host=NULL;
    int iRet = SOCKET_ERROR;
    conn = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(conn==INVALID_SOCKET)
    {
    return FALSE;
    }
    for (int iTry=0;iTry<3 && host==NULL;iTry++)
    host= gethostbyname(HostName);
    if(host==NULL)
    {
    closesocket(conn);
    return FALSE;
    }
    sockaddr_in clientService;
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = *((unsigned long*)host->h_addr_list);
    clientService.sin_port = htons( 80 );
    ///*********************** This part doesn't work properly. I brought error code 10060
    for(int iTry=0;iTry<3 && iRet==SOCKET_ERROR;iTry++)
    iRet= connect( conn, (SOCKADDR*) &clientService, sizeof(clientService) ) ;
    int iError = WSAGetLastError(); //THIS returns 10060
    if ( iRet== SOCKET_ERROR)
    {
    closesocket(conn);
    return FALSE;
    }
    char RequestFirstLine[512];
    sprintf(RequestFirstLine,"%s %s HTTP/1.0\r\n",Action,ObjectName);
    iRet= SOCKET_ERROR;
    for(int iTry=0;iTry<3 && iRet==SOCKET_ERROR;iTry++)
    {
    send(conn,RequestFirstLine,strlen(RequestFirstLine),0);
    send(conn,Headers,HeadersLen,0);
    iRet=send(conn,Body,BodyLen,0);
    }
    closesocket(conn);
    return iRet!=SOCKET_ERROR;
    }

    //The Caller Function
    void CtestDlg::OnBnClickedBtnhttp()
    {
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
    wVersionRequested = MAKEWORD( 2, 0 );
    err=WSAStartup(wVersionRequested,&wsaData);
    HTTPSubmitForm("www.yahoo.com","GET","/","",0,"",0);
    WSACleanup();
    }

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    MrDooDoo wrote:

    I am trying to connect to the internet

    Exactly what do you mean by this? Are you trying to connect to a specific host and is that host accessible from your laptop?

    Veni, vidi, abiit domum

    M 1 Reply Last reply
    0
    • L Lost User

      MrDooDoo wrote:

      I am trying to connect to the internet

      Exactly what do you mean by this? Are you trying to connect to a specific host and is that host accessible from your laptop?

      Veni, vidi, abiit domum

      M Offline
      M Offline
      MrDooDoo
      wrote on last edited by
      #3

      I am talking about building a client software which connects to internet (for e.g to Google) by winsock. But when I try to call connect function a dial dialog appears which lists all of my lan connections. but I am connected to internet by my wireless or another network adapter. I think this is because the socket is built for my LAN and I must build a socket for specific adapter. I looked up MSDN,I found the GetBestInterface. I used it and it returns the index of my wireless adapter. then I tried to use the bind function to associate my socket to my wireless adapter but it doesn't work.

      L 1 Reply Last reply
      0
      • M MrDooDoo

        Hi everyone, I try to develop a network program using WINSOCK. I am trying to connect to the internet from a laptop with some network adapters ( LAN, WIRELESS,A USB ADSL Modem). It sounds, when I call connect function, it tries to connect using LAN, so because LAN is not connected it fails. Here are my questions: 1)How can I force a socket to use a specified Network Adapter? 2)How can I find which adapter is connected to internet? Thanks for reading and answering :) Here is my code, the connect function fails (err code :10060 )

        //The function for handling HTTP
        BOOL HTTPSubmitForm(char* HostName, char* Action,char* ObjectName, char* Headers,int HeadersLen, char* Body,int BodyLen)
        {
        SOCKET conn;
        struct hostent* host=NULL;
        int iRet = SOCKET_ERROR;
        conn = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        if(conn==INVALID_SOCKET)
        {
        return FALSE;
        }
        for (int iTry=0;iTry<3 && host==NULL;iTry++)
        host= gethostbyname(HostName);
        if(host==NULL)
        {
        closesocket(conn);
        return FALSE;
        }
        sockaddr_in clientService;
        clientService.sin_family = AF_INET;
        clientService.sin_addr.s_addr = *((unsigned long*)host->h_addr_list);
        clientService.sin_port = htons( 80 );
        ///*********************** This part doesn't work properly. I brought error code 10060
        for(int iTry=0;iTry<3 && iRet==SOCKET_ERROR;iTry++)
        iRet= connect( conn, (SOCKADDR*) &clientService, sizeof(clientService) ) ;
        int iError = WSAGetLastError(); //THIS returns 10060
        if ( iRet== SOCKET_ERROR)
        {
        closesocket(conn);
        return FALSE;
        }
        char RequestFirstLine[512];
        sprintf(RequestFirstLine,"%s %s HTTP/1.0\r\n",Action,ObjectName);
        iRet= SOCKET_ERROR;
        for(int iTry=0;iTry<3 && iRet==SOCKET_ERROR;iTry++)
        {
        send(conn,RequestFirstLine,strlen(RequestFirstLine),0);
        send(conn,Headers,HeadersLen,0);
        iRet=send(conn,Body,BodyLen,0);
        }
        closesocket(conn);
        return iRet!=SOCKET_ERROR;
        }

        //The Caller Function
        void CtestDlg::OnBnClickedBtnhttp()
        {
        WORD wVersionRequested;
        WSADATA wsaData;
        int err;
        wVersionRequested = MAKEWORD( 2, 0 );
        err=WSAStartup(wVersionRequested,&wsaData);
        HTTPSubmitForm("www.yahoo.com","GET","/","",0,"",0);
        WSACleanup();
        }

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #4

        To force a socket to use a specific network adapter pass the local IP address of the adapter to the bind() function. To check if a specific interface is connected to the Internet, try to establish a connection to some server. To get information about installed adapters, you can use the IP Helper API[^] functions.

        1 Reply Last reply
        0
        • M MrDooDoo

          I am talking about building a client software which connects to internet (for e.g to Google) by winsock. But when I try to call connect function a dial dialog appears which lists all of my lan connections. but I am connected to internet by my wireless or another network adapter. I think this is because the socket is built for my LAN and I must build a socket for specific adapter. I looked up MSDN,I found the GetBestInterface. I used it and it returns the index of my wireless adapter. then I tried to use the bind function to associate my socket to my wireless adapter but it doesn't work.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #5

          MrDooDoo wrote:

          I am talking about building a client software which connects to internet (for e.g to Google) by winsock.

          Well the first thing you need is to find the IP address of a Google server that you wish to connect to. You will then need to add some code to build HTTP requests that you send to it, and code to interpret the responses.

          MrDooDoo wrote:

          I tried to use the bind function to associate my socket to my wireless adapter but it doesn't work.

          "Doesn't work" is not a valid description of an error. You need to show the code that you are using and the exact error message or status that you receive. In general you do not need to be concerned about which interface to use. Just create a socket and try to connect to an IP address and you will get a response or status returned, depending on whether the target is reachable from your client system or not.

          Veni, vidi, abiit domum

          M 1 Reply Last reply
          0
          • M MrDooDoo

            Hi everyone, I try to develop a network program using WINSOCK. I am trying to connect to the internet from a laptop with some network adapters ( LAN, WIRELESS,A USB ADSL Modem). It sounds, when I call connect function, it tries to connect using LAN, so because LAN is not connected it fails. Here are my questions: 1)How can I force a socket to use a specified Network Adapter? 2)How can I find which adapter is connected to internet? Thanks for reading and answering :) Here is my code, the connect function fails (err code :10060 )

            //The function for handling HTTP
            BOOL HTTPSubmitForm(char* HostName, char* Action,char* ObjectName, char* Headers,int HeadersLen, char* Body,int BodyLen)
            {
            SOCKET conn;
            struct hostent* host=NULL;
            int iRet = SOCKET_ERROR;
            conn = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
            if(conn==INVALID_SOCKET)
            {
            return FALSE;
            }
            for (int iTry=0;iTry<3 && host==NULL;iTry++)
            host= gethostbyname(HostName);
            if(host==NULL)
            {
            closesocket(conn);
            return FALSE;
            }
            sockaddr_in clientService;
            clientService.sin_family = AF_INET;
            clientService.sin_addr.s_addr = *((unsigned long*)host->h_addr_list);
            clientService.sin_port = htons( 80 );
            ///*********************** This part doesn't work properly. I brought error code 10060
            for(int iTry=0;iTry<3 && iRet==SOCKET_ERROR;iTry++)
            iRet= connect( conn, (SOCKADDR*) &clientService, sizeof(clientService) ) ;
            int iError = WSAGetLastError(); //THIS returns 10060
            if ( iRet== SOCKET_ERROR)
            {
            closesocket(conn);
            return FALSE;
            }
            char RequestFirstLine[512];
            sprintf(RequestFirstLine,"%s %s HTTP/1.0\r\n",Action,ObjectName);
            iRet= SOCKET_ERROR;
            for(int iTry=0;iTry<3 && iRet==SOCKET_ERROR;iTry++)
            {
            send(conn,RequestFirstLine,strlen(RequestFirstLine),0);
            send(conn,Headers,HeadersLen,0);
            iRet=send(conn,Body,BodyLen,0);
            }
            closesocket(conn);
            return iRet!=SOCKET_ERROR;
            }

            //The Caller Function
            void CtestDlg::OnBnClickedBtnhttp()
            {
            WORD wVersionRequested;
            WSADATA wsaData;
            int err;
            wVersionRequested = MAKEWORD( 2, 0 );
            err=WSAStartup(wVersionRequested,&wsaData);
            HTTPSubmitForm("www.yahoo.com","GET","/","",0,"",0);
            WSACleanup();
            }

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #6

            You cannot specify an adapter to use. What does determine the adapter is the TCP/IP route table. The IP address you're connecting to is looked up in this route table and an appropriate adapter to get to the destination address is chosen based on that.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            M 1 Reply Last reply
            0
            • L Lost User

              MrDooDoo wrote:

              I am talking about building a client software which connects to internet (for e.g to Google) by winsock.

              Well the first thing you need is to find the IP address of a Google server that you wish to connect to. You will then need to add some code to build HTTP requests that you send to it, and code to interpret the responses.

              MrDooDoo wrote:

              I tried to use the bind function to associate my socket to my wireless adapter but it doesn't work.

              "Doesn't work" is not a valid description of an error. You need to show the code that you are using and the exact error message or status that you receive. In general you do not need to be concerned about which interface to use. Just create a socket and try to connect to an IP address and you will get a response or status returned, depending on whether the target is reachable from your client system or not.

              Veni, vidi, abiit domum

              M Offline
              M Offline
              MrDooDoo
              wrote on last edited by
              #7

              Thanks for your answer I brought my code here. the connect functions fails and WSAGetLastError returns 10060 (Connection timed out) The code:

              //The function for handling HTTP
              BOOL HTTPSubmitForm(char* HostName, char* Action,char* ObjectName, char* Headers,int HeadersLen, char* Body,int BodyLen)
              {
              SOCKET conn;
              struct hostent* host=NULL;
              int iRet = SOCKET_ERROR;
              conn = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
              if(conn==INVALID_SOCKET)
              {
              return FALSE;
              }
              for (int iTry=0;iTry<3 && host==NULL;iTry++)
              host= gethostbyname(HostName);
              if(host==NULL)
              {
              closesocket(conn);
              return FALSE;
              }
              sockaddr_in clientService;
              clientService.sin_family = AF_INET;
              clientService.sin_addr.s_addr = *((unsigned long*)host->h_addr_list);
              clientService.sin_port = htons( 80 );
              ///*********************** This part doesn't work properly. I brought error code 10060
              for(int iTry=0;iTry<3 && iRet==SOCKET_ERROR;iTry++)
              iRet= connect( conn, (SOCKADDR*) &clientService, sizeof(clientService) ) ;
              int iError = WSAGetLastError(); //THIS returns 10060
              if ( iRet== SOCKET_ERROR)
              {
              closesocket(conn);
              return FALSE;
              }
              char RequestFirstLine[512];
              sprintf(RequestFirstLine,"%s %s HTTP/1.0\r\n",Action,ObjectName);
              iRet= SOCKET_ERROR;
              for(int iTry=0;iTry<3 && iRet==SOCKET_ERROR;iTry++)
              {
              send(conn,RequestFirstLine,strlen(RequestFirstLine),0);
              send(conn,Headers,HeadersLen,0);
              iRet=send(conn,Body,BodyLen,0);
              }
              closesocket(conn);
              return iRet!=SOCKET_ERROR;
              }

              //The Caller Function
              void CtestDlg::OnBnClickedBtnhttp()
              {
              WORD wVersionRequested;
              WSADATA wsaData;
              int err;
              wVersionRequested = MAKEWORD( 2, 0 );
              err=WSAStartup(wVersionRequested,&wsaData);
              HTTPSubmitForm("www.yahoo.com","GET","/","",0,"",0);
              WSACleanup();
              }

              L 1 Reply Last reply
              0
              • D Dave Kreskowiak

                You cannot specify an adapter to use. What does determine the adapter is the TCP/IP route table. The IP address you're connecting to is looked up in this route table and an appropriate adapter to get to the destination address is chosen based on that.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                M Offline
                M Offline
                MrDooDoo
                wrote on last edited by
                #8

                thanks, but are you sure? suppose the condition which someone is building a server and in this situation he/she needs to specify the adapter. I did the same in C# before. but I couldn't do in C++. regards. :-O

                D 1 Reply Last reply
                0
                • M MrDooDoo

                  thanks, but are you sure? suppose the condition which someone is building a server and in this situation he/she needs to specify the adapter. I did the same in C# before. but I couldn't do in C++. regards. :-O

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #9

                  That's how the network stack works. By trying to pick an adapter you are essentially trying to remove the Transport layer from the network stack. I'm wondering how you did it in C#. Since you need TCP/IP to be bound to the adapter and have a valid route in order for it to work, I'm guessing that you really didn't do that, or that stuff was setup for you and you didn't know it.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  1 Reply Last reply
                  0
                  • M MrDooDoo

                    Thanks for your answer I brought my code here. the connect functions fails and WSAGetLastError returns 10060 (Connection timed out) The code:

                    //The function for handling HTTP
                    BOOL HTTPSubmitForm(char* HostName, char* Action,char* ObjectName, char* Headers,int HeadersLen, char* Body,int BodyLen)
                    {
                    SOCKET conn;
                    struct hostent* host=NULL;
                    int iRet = SOCKET_ERROR;
                    conn = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
                    if(conn==INVALID_SOCKET)
                    {
                    return FALSE;
                    }
                    for (int iTry=0;iTry<3 && host==NULL;iTry++)
                    host= gethostbyname(HostName);
                    if(host==NULL)
                    {
                    closesocket(conn);
                    return FALSE;
                    }
                    sockaddr_in clientService;
                    clientService.sin_family = AF_INET;
                    clientService.sin_addr.s_addr = *((unsigned long*)host->h_addr_list);
                    clientService.sin_port = htons( 80 );
                    ///*********************** This part doesn't work properly. I brought error code 10060
                    for(int iTry=0;iTry<3 && iRet==SOCKET_ERROR;iTry++)
                    iRet= connect( conn, (SOCKADDR*) &clientService, sizeof(clientService) ) ;
                    int iError = WSAGetLastError(); //THIS returns 10060
                    if ( iRet== SOCKET_ERROR)
                    {
                    closesocket(conn);
                    return FALSE;
                    }
                    char RequestFirstLine[512];
                    sprintf(RequestFirstLine,"%s %s HTTP/1.0\r\n",Action,ObjectName);
                    iRet= SOCKET_ERROR;
                    for(int iTry=0;iTry<3 && iRet==SOCKET_ERROR;iTry++)
                    {
                    send(conn,RequestFirstLine,strlen(RequestFirstLine),0);
                    send(conn,Headers,HeadersLen,0);
                    iRet=send(conn,Body,BodyLen,0);
                    }
                    closesocket(conn);
                    return iRet!=SOCKET_ERROR;
                    }

                    //The Caller Function
                    void CtestDlg::OnBnClickedBtnhttp()
                    {
                    WORD wVersionRequested;
                    WSADATA wsaData;
                    int err;
                    wVersionRequested = MAKEWORD( 2, 0 );
                    err=WSAStartup(wVersionRequested,&wsaData);
                    HTTPSubmitForm("www.yahoo.com","GET","/","",0,"",0);
                    WSACleanup();
                    }

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    Your code just needs to be modified at the point where you select the remote host ip address, thus:

                    	clientService.sin\_addr.s\_addr = \*((unsigned long\*)host->h\_addr\_list\[0\]);
                    

                    as h_addr_list is an array of addresses pointers to addresses, not a single pointer. In a live application, you may also need to try each address in the list in turn until one succeeds. [edit]Corrected as strikeout above.[edit]

                    Veni, vidi, abiit domum

                    M 1 Reply Last reply
                    0
                    • L Lost User

                      Your code just needs to be modified at the point where you select the remote host ip address, thus:

                      	clientService.sin\_addr.s\_addr = \*((unsigned long\*)host->h\_addr\_list\[0\]);
                      

                      as h_addr_list is an array of addresses pointers to addresses, not a single pointer. In a live application, you may also need to try each address in the list in turn until one succeeds. [edit]Corrected as strikeout above.[edit]

                      Veni, vidi, abiit domum

                      M Offline
                      M Offline
                      MrDooDoo
                      wrote on last edited by
                      #11

                      :laugh: thanks

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups