Symbian
Symbian OS Library

FAQ-1282 Why can't I connect to a local (within the device) TCP/UDP socket using the name 'localhost'?

[Index][spacer] [Previous] [Next]



 

Classification: C++ Category: Networking
Created: 05/12/2005 Modified: 05/20/2005
Number: FAQ-1282
Platform: Symbian OS v6.1, Symbian OS v7.0, Symbian OS v7.0s, Symbian OS v8.0, Symbian OS v8.0a, Symbian OS v8.0b, Symbian OS v8.1a, Symbian OS v8.1b

Question:
I am connecting to a local TCP/IP socket - using 'localhost' as the address and resolving via RHostResolver. Why do I get errors when connecting?
I am connecting to a local HTTP server using a standard web browser or the HTTP API on the device. When connecting to the HTTP server the device prompts me to select an IAP from a list. Why?
Do I get billed if I select an IAP when connecting to a local HTTP server using a browser?
What method can I use for connecting to a local TCP/IP server?


Answer:
Why can't I connect to a local (within the device) TCP/UDP socket using the name 'localhost'?
I am connecting to a local TCP/IP socket - using 'localhost' as the address and resolving via RHostResolver. Why do I get errors when connecting?

RHostResolver::GetByName()should return a 'localhost' IP address of 127.0.0.1, for which (by default) there is always a pre-configured route and an interface. Unfortunately, the function call currently returns a resolved IP address that does not have a valid route to connect the socket.

Depending on the SDK you are using, the call to Connect()will result in KErrBadName, KErrCouldNotConnect or other system errors

An example of such code is shown below...

_LIT(KLocalHost, "LocalHost");
RHostResolver resolver;
User::LeaveIfError(resolver.Open(socketServer,KAfInet,KProtocolInetTcp));
TNameEntry sendaddressentry;
User::LeaveIfError(resolver.GetByName(KLocalHost, sendaddressentry));
TInetAddr sendaddr(sendaddressentry().iAddr);
sendaddr.SetPort(1234);

TRequestStatus sendingstatus;
User::LeaveIfError(sendingocket.Open(socketServer, KAfInet, KSockStream, KProtocolInetTcp);
sendingocket.Connect(sendaddr,sendingstatus);
User::WaitForRequest(sendingstatus);
User::LeaveIfError(sendingstatus.Int());

I am connecting to a local HTTP server using a standard web browser or the HTTP API on the device. When connecting to the HTTP server the device prompts me to select an IAP from a list. Why?

Connecting to a local HTTP server is compounded by 2 issues:
      1. The 'localhost' name does not get resolved properly; hence, the connection fails if the 'http://localhost:5050' address is used.
      2. By the use of the HTTP API. If the connection to the local HTTP server is made using the Symbian HTTP API (directly or indirectly via a standard HTTP browser), due to the way the API currently works, the user will always be prompted to select an IAP. The HTTP API makes an unconditional bearer connection regardless of whether the HTTP connection is to a local or remote host. Failure to select an IAP (or for that matter an invalid IAP is selected) will result in a HTTP connection failure.

      Do I get billed if I select an IAP when connecting to a local HTTP server using a browser?

      No. Generally, the local HTTP connection will only complete after the IAP is selected, and a successful bearer connection is established. Although a bearer connection is made, the local HTTP traffic (between the borwser and the HTTP server) does not 'leak' outside the device. You will only be billed if the local HTTP server (or the browser, in response to a GET request) makes a remote connection to the internet as part of the local connection.

      What method can I use for connecting to a local TCP/IP or HTTP server?

      As far as connecting locally to a HTTP server using a browser, there is currently no work-around solution to suppress the prompting of the IAP dialog. The user will always be presented with the IAP selection dialog, and to complete the local HTTP connection, a valid IAP must be selected. It is not recommended to change the CommDB connection preference settings to 'silence' the IAP dialog - even if a default IAP has been configured. Note that the IAP prompting issue only surfaces if a conenction attempt is made via the HTTP API. A direct RSocket HTTP local connection will not trigger an IAP dialog.

      With regards to the 'localhost' resolving issue, it is envisaged that 'localhost' will resolve correctly in future Symbian OS versions (or check with the SDK manufacturer for any firmware updates). The following work-around solutions can be applied in the interim:

      1. Harcode local IP address
        Hardcode the local IP address to '127.0.0.1' when connecting to a local TCP/IP socket. The above issues are only seen when using a RHostResolver to resolve the IP address.

        Hard-coding should be used in preference to the following intrusive work-around. Its is captured here for completeness; there is no guarantee that the solution will work on a given SDK.
        2. Configuration file changes
          Copy and paste the file z:\system\data\tcpip.ini into c:\system\data\tcpip.ini if it does not exist. Edit the file under c: and insert the text below. The hostname can be any string except 'localhost'.
           [host]
           hostname= myphone


          Edit or create the c:\system\data\hosts file (note no extension) and insert the following:

          127.0.0.1 localhost

          Restart the phone in order to enable the new configuration.
          Note:
          • Other than the above work-arounds, do not change the IP routing table to circumvent this issue. Changing the routing table can result in undesired routing behaviour.