Hi All,

I have created a two separate active objects to read and write from same socket. One AO reads from socket and another writes to socket.

I can successfully connect to server and send data to server but I fail to read data from server. Even server is sending, I am not receiving any data it seems it is just listening to stream and there is no data.... why??

my code is below

Reciever

/*
* ============================================================================
* Name : CTcpCocketReceiver from TcpCocketReceiver.cpp
* Part of : TcpCocket
* Created : 06.03.2006 by ToBeReplacedByAuthor
* Implementation notes:
*
* Version :
* Copyright: ToBeReplacedByCopyright
* ============================================================================
*/

// INCLUDE FILES
#include "TcpCocketReceiver.h"

// ================= MEMBER FUNCTIONS =======================

// constructor
CTcpCocketReceiver::CTcpCocketReceiver(MObserver& aObserver, RSocket& aSocket)
: CActive(CActive::EPriorityStandard), iObserver(aObserver), iSocket(aSocket),
iBuf(const_cast<TUint8*>(iBuffer.Ptr()), 4)
{
CActiveScheduler::Add(this);
}

// destructor
CTcpCocketReceiver::~CTcpCocketReceiver()
{
Cancel();
}

void CTcpCocketReceiver::Receive()
{
Cancel();
// Receive the length of the message
iBuf.Set(const_cast<TUint8*>(iBuffer.Ptr()), 0, 4);
iSocket.Read(iBuf, iStatus);
SetActive();
}

void CTcpCocketReceiver:oCancel()
{
iSocket.CancelRead();
}

void CTcpCocketReceiver::RunL()
{
TInt err = iStatus.Int();
if(err==KErrNone)
{
if(iBuf.Size()==4)
{
TInt size = 0;
TPckg<TInt> pkg(size);
pkg.Copy(iBuf);
// Receive the message
iBuf.Set(const_cast<TUint8*>(iBuffer.Ptr()), 0, size);
iSocket.Read(iBuf, iStatus);
SetActive();
}
else
{
// Notify observer
const TUint16* ptr = reinterpret_cast<const TUint16*>(iBuf.Ptr());
TPtrC msg(ptr, iBuf.Length()/2);
iObserver.OnReceiveL(msg, err);
}
}
else
{
iObserver.OnReceiveL(KNullDesC(), err);
}
}

// End of File



Socket Engine

/*
* ============================================================================
* Name : CTcpCocketEngine from TcpCocketEngine.cpp
* Part of : TcpCocket
* Created : 06.03.2006 by ToBeReplacedByAuthor
* Implementation notes:
*
* Version :
* Copyright: ToBeReplacedByCopyright
* ============================================================================
*/

// INCLUDE FILES
#include <TcpSocketPC.pan> // for Panic();

#include "TcpCocketEngine.h"

const TInt KTimeout = 30; // 30 seconds time-out

// ================= MEMBER FUNCTIONS =======================

// Two-phased constructor.
CTcpCocketEngine* CTcpCocketEngine::NewL(MTcpObserver& aObserver)
{
CTcpCocketEngine* self = new (ELeave) CTcpCocketEngine( aObserver );
CleanupStack::PushL( self );
self->ConstructL();
CleanupStack::Pop();
return self;
}

// destructor
CTcpCocketEngine::~CTcpCocketEngine()
{
delete iTimer;
delete iSender;
delete iReceiver;
Cancel();
iSocket.Close();
iListenSocket.Close();
iSocketServ.Close();
}

// New functions
CTcpCocketEngine::TState CTcpCocketEngine::State()
{
return iState;
}

void CTcpCocketEngine::ConnectL(const TDesC& aAddr, TUint aPort)
{
__ASSERT_DEBUG(iState==EStateIdle, Panic(ETcpCocketPanicFailToConnect));

User::LeaveIfError(iAddr.Input(aAddr));
iAddr.SetPort(aPort);

// Open a TCP socket
User::LeaveIfError(iSocket.Open(iSocketServ, KAfInet, KSockStream, KProtocolInetTcp));

// Initiate socket connection
iSocket.Connect(iAddr, iStatus);
SetActive();

// Start a timeout timer
iTimer->Start(KTimeout);

iState = EStateConnecting;
}

void CTcpCocketEngine:isconnect()
{
__ASSERT_DEBUG(iState==EStateConnected, Panic(ETcpCocketPanicFailToDisconnect));

// cancel all outstanding operations
// since we are connected, the only possibilities are read and write
iReceiver->Cancel();
iSender->Cancel();
iSocket.Close();

iState = EStateIdle;
}

void CTcpCocketEngine::ListenL()
{
__ASSERT_DEBUG(iState==EStateIdle, Panic(ETcpCocketPanicFailToListen));

TInetAddr addr(INET_ADDR(127,0,0,1), 8002);
User::LeaveIfError(iListenSocket.Open(iSocketServ, KAfInet, KSockStream, KProtocolInetTcp));
User::LeaveIfError(iSocket.Open( iSocketServ ) );

// Bind the socket to the required port.
iListenSocket.Bind(addr);

// Listen for incoming connections
iListenSocket.Listen(1);

// Accept incoming connections
iListenSocket.Accept(iSocket, iStatus);
SetActive();

iState = EStateListening;
}

void CTcpCocketEngine::SendL(const TDesC& aMsg)
{
__ASSERT_DEBUG(iState==EStateConnected, Panic(ETcpCocketPanicFailToSend));

iSender->Send(aMsg);
}

void CTcpCocketEngine::ReceiveL()
{
__ASSERT_DEBUG(iState==EStateConnected, Panic(ETcpCocketPanicFailToReceive));

iReceiver->Receive();
}

// Functions from CActive
void CTcpCocketEngine:oCancel()
{
switch(iState)
{
case EStateConnecting:
{
iTimer->Cancel();
iSocket.CancelConnect();
break;
}
case EStateListening:
{
iListenSocket.CancelAccept();
break;
}
default:
{
// unreachable
break;
}
}
iState = EStateIdle;
}

void CTcpCocketEngine::RunL()
{
TInt err = iStatus.Int();
switch(iState)
{
case EStateConnecting:
{
iTimer->Cancel();
if (err == KErrNone)
{
// Connection completed successfully
iState = EStateConnected;
ReceiveL();
}
else
{
iSocket.Close();
iState = EStateIdle;
}
iObserver.OnConnectL(err);
break;
}
case EStateListening:
{
if(err == KErrNone)
{
iState = EStateConnected;
ReceiveL();
}
iListenSocket.Close();
iObserver.OnConnectL(err);
break;
}
default:
{
// unreachable
break;
}
}
}

// Functions from CTcpCocketTimer::MTimerObserver
void CTcpCocketEngine::OnTimerL(TInt aError)
{
Cancel();
TInt err = (aError==KErrNone)?KErrTimedOut:aError;
iObserver.OnConnectL(err);
}

// Functions from CTcpCocketSender::MObserver
void CTcpCocketEngine::OnSendL(const TDesC& aMsg, TInt aError)
{
iObserver.OnSendL(aMsg, aError);
}

// Functions from CTcpCocketReceiver::MObserver
void CTcpCocketEngine::OnReceiveL(const TDesC& aData, TInt aError)
{
iObserver.OnReceiveL(aData, aError);
if(aError==KErrNone)
{
ReceiveL();
}
else
{
Disconnect();
}
}

// constructor
CTcpCocketEngine::CTcpCocketEngine(MTcpObserver& aObserver)
: CActive(CActive::EPriorityStandard), iObserver(aObserver)
{
CActiveScheduler::Add(this);
}

// EPOC default constructor can leave.
void CTcpCocketEngine::ConstructL()
{
// Open a session to Socket Server
User::LeaveIfError(iSocketServ.Connect());

iTimer = CTcpCocketTimer::NewL(*this);

iSender = CTcpCocketSender::NewL(*this, iSocket);
iReceiver = new(ELeave) CTcpCocketReceiver(*this, iSocket);
}




not getting what is error please some one tell how to resolve it