This is the recurring question. The final answer is of course, listing them all using the following code:
Code:
// - assumes socket is already Open
// - allocate from heap, because this is large structure
TSoInetInterfaceInfo *inf = new TSoInetInterfaceInfo;
if (inf == NULL)
return KErrNoMemory;
TPckg<TSoInetInterfaceInfo> opt(*inf);
TInt err = socket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl);
if (err == KErrNone)
{
while (socket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, inf) ==
KErrNone)
{
if (inf().iAddress.IsUnspecified())
{
// Interface inf().iName has no address
}
else
{
// interface inf().iName has address inf().iAddress
}
}
}
delete inf;
Now, the question is which one do you use? When no interfaces are up, you only see the loopback addresses ("::" and 127.0.0.1).
Other addresses are only defined when one or more interface is up.
An interface can have multiple addresses.
If your application is such that it actually connects to something, connecting does not need the knowledge of own address. After connect has been succesfull, you can find the correct address from the connected socket.
If you are doing server application, you don't need to know your address either. Just use unspecified address. Of course, your server will not see anything from outside until some interface is up.
Bookmarks