Hi all,
I have been struggling with having this working for a long time now I have failed to connect a socket client app to a socket server app bith running on different phones (and using different SIM cards: T-Mobile and O2). The thing is i managed to get the IP address allocated for the server. And everytime i try to connect the client via that IP the connection fails (ie does not initiate)
I have the server implemented as follows:
void CSocketEngine::ConstructL()
{
CActiveScheduler::Add(this);
// If in Idle state
if(iState == EIdle)
{
// Connect the socket
TInt err = iSocketServ.Connect();
if( err != KErrNone && err != KErrAlreadyExists)
User::Leave(err);
// get the number of protocols supported (bluetooth, irda, tcp...)
TUint protNb;
err = iSocketServ.NumProtocols(protNb);
User::LeaveIfError(err);
// to view the IAP dialog and select one from it
User::LeaveIfError(iConnection.Open(iSocketServ));
iConnection.Start(iStatus);
iState = EStartConnecting;
SetActive();
}
}
void CSocketEngine::RunL()
{
if( iState == EStartConnecting && iStatus == KErrNone)
{
TInt err;
// now open the socket on the socker server session created with the IAP interface connection
err = iSocket.Open( iSocketServ,
KAfInet,
KSockStream,
KProtocolInetTcp,
iConnection );
if(err != KErrNone)
User::LeaveIfError(err);
TInetAddr addr;
// get teh IP address allocated after RConnection::Start()
// this address will be displayed on screen and dictated to the client
GetIp(addr);
// Bind the socket to the address and a port
iInetAddr.SetPort(0x02);
iInetAddr.SetAddress(addr.Address());
iSocket.Bind(iInetAddr);
// make the socket listening (parameter specifies the size of the quoue)
iSocket.Listen(1);
// open another socket to service any connection request as the listening
// socket must be free always to listen for incomming connection requests.
err = iServiceSocket.Open(iSocketServ);
if(err != KErrNone)
User::LeaveIfError(err);
// Accept the first connection. This is an asynchronous call.
iSocket.Accept(iServiceSocket, iStatus);
iState = EAccepting;
SetActive();
}
else if(iState == EAccepting && iStatus == KErrNone)
{
/***************************************/
// THIS PART HERE IS NOT REACHED EVER
/***************************************/
CAknInformationNote *note = new (ELeave) CAknInformationNote();
CleanupStack::PushL(note);
note->ExecuteLD(_L("Recieved"));
CleanupStack::Pop();
}
}
TInt CSocketEngine::GetIp(TInetAddr &aAddr)
{
RSocket socket;
User::LeaveIfError(socket.Open( iSocketServ,
KAfInet,
KSockStream,
KProtocolInetTcp) );
TUint32 iapId = 0;
User::LeaveIfError(iConnection.GetIntSetting(_L("IAP\\Id"), iapId));
TSoInetInterfaceInfo ifinfo;
TPckg<TSoInetInterfaceInfo> ifinfopkg(ifinfo);
TSoInetIfQuery ifquery;
TPckg<TSoInetIfQuery> ifquerypkg(ifquery);
User::LeaveIfError(socket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
while(socket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, ifinfopkg) == KErrNone)
{
ifquery.iName = ifinfo.iName;
TInt err = socket.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery, ifquerypkg);
if( err == KErrNone && ifquery.iZone[1] == iapId)
{
if(ifinfo.iAddress.Address() >0)
{
aAddr = ifinfo.iAddress;
socket.Close();
return err;
}
}
else if(err != KErrNone)
{
socket.Close();
return err;
}
} // while
socket.Close();
return KErrNotFound;
}
The client is implemented as follows:
void CSocketEngine_C::ConnectClient(TDes& aIp) // The IP address of the server
{
// If in Idle state
if(iState == EIdle)
{
// Connect the socket
TInt err = iSocketServ.Connect();
if( err != KErrNone && err != KErrAlreadyExists)
User::Leave(err);
// get the number of protocols supported (bluetooth, irda, tcp...)
TUint protNb;
err = iSocketServ.NumProtocols(protNb);
User::LeaveIfError(err);
// Set the address of the server app to connect to
TInetAddr temp;
temp.Input(aIp);
iInetAddr.SetAddress(temp.Address());
iInetAddr.SetPort(0x02);
// now open the socket on the socker server session created
err = iSocket.Open( iSocketServ,
KAfInet,
KSockStream,
KProtocolInetTcp);
if(err != KErrNone)
User::LeaveIfError(err);
iSocket.Connect(iInetAddr, iStatus);
iState = EConnecting;
SetActive();
}
}
void CSocketEngine_C::RunL()
{
if(iState == EConnecting)
{
/***********************************/
// This part is never reached
/***********************************/
CAknInformationNote *note = new (ELeave) CAknInformationNote();
CleanupStack::PushL(note);
note->ExecuteLD(_L("Connected"));
CleanupStack::Pop();
}
}




