Symbian
Symbian OS Library

FAQ-0793 How do I set a route for a socket connection?

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



 

Classification: C++ Category: Comms
Created: 05/15/2002 Modified: 06/11/2002
Number: FAQ-0793
Platform: Symbian OS v6.0, Symbian OS v6.1

Question:
How do I set a route for a socket connection?

Answer:
You can use the following approach:

#define IPADDR(a,b,c,d) (TUint32)(((a)<<24)|((b)<<16)|((c)<<8)|(d))

TInt SetRoute(RSocket& aSocket, TInt aOp, TUint32 aDest, TUint32 aNetMask, TUint32 aGateway, TUint32 aInterface, TInt aMetric)
    {
    TPckgBuf info;
    info().iDstAddr.SetAddress(aDest);
    info().iNetMask.SetAddress(aNetMask);
    info().iGateway.SetAddress(aGateway);
    info().iIfAddr.SetAddress(aInterface);
    info().iMetric = aMetric;
    return aSocket.SetOpt(aOp, KSolInetRtCtrl, info);
    //const TInt KSoInetAddRoute = 0x223;
    //const TInt KSoInetDeleteRoute = 0x224;
    //const TInt KSoInetChangeRoute = 0x225;
    }
    TInt StartSocketWithRoute(TUint32 addr)
      {
      RSocketServ socketServ;
      RSocket sock;
      User::LeaveIfError(sock.Open(socketServ, KAfInet, KSockStream, KProtocolInetTcp));
      TInt ret = SetRoute(sock, KSoInetAddRoute, addr, IPADDR(255,255,255,0),
      IPADDR(127,0,0,1), IPADDR(0,0,0,0), 0);
      // do something useful with the socket
      return ret;
      }