i have implemented echoengine tcp/ip example socket
1)one active object for socket connection
2)second for socket writing
3)third for socket reading
CEchoEngine::CEchoEngine() :
CActive(EPriorityStandard)
{
}
CEchoEngine* CEchoEngine::NewL()
{
CEchoEngine* self = NewLC();
CleanupStack::Pop();
return self;
}
CEchoEngine* CEchoEngine::NewLC()
{
CEchoEngine* self = new (ELeave) CEchoEngine;
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
void CEchoEngine::ConstructL()
// Construct object, and open a socket
{
CActiveScheduler::Add(this);
// iPort=5014;
// open server socket
User::LeaveIfError(iSocketServ.Connect());
// _LIT( KIPAddress, "192.168.1.100");//server
TInetAddr addr;
TUint32 aAddr;
if ( addr.Input( KIPAddress ) == KErrNone )
{
// server name is already a valid ip address
aAddr = addr.Address();
iAddress.SetPort( iPort );
iAddress.SetAddress( aAddr );
iAddress.SetFamily( KAfInet );
iEchoSocket.Close();
//open tcp socket
User::LeaveIfError(iEchoSocket.Open(iSocketServ, KAfInet, KSockStream, KProtocolInetTcp));
}
iEngineStatus = EConnecting;
iEchoSocket.Connect(iAddress,iStatus);
SetActive();
iEchoRead = CEchoRead::NewL(&iEchoSocket);
iEchoWrite = CEchoWrite::NewL(&iEchoSocket);
}
void CEchoEngine::RunL()
// Active object request complete handler.
// iEngineStatus flags what request was made, so its
// completion can be handled appropriately
{
// iTimer->Cancel(); // Cancel TimeOut timer before completion
_LIT(KConnecting,"\n<CEchoEngine> Connecting\n");
_LIT(KConnectionFailed,"\n<CEchoEngine> Connection failed");
_LIT(KDNSFailed,"\n<CEchoEngine> DNS lookup failed");
_LIT(KTimedOut,"\n<CEchoEngine> Timed out\n");
_LIT(KDomainName,"\nDomain name = ");
_LIT(KIPAddress,"\nIP address = ");
TBuf<15> ipAddr;
switch(iEngineStatus)
{
case EConnecting:
{
// IP connection request
if (iStatus == KErrNone)
// Connection completed sucessfully
{
iEngineStatus = EConnected;
Write(origdata);
Read();
}
else
{
iEngineStatus = EConnectFailed;
}
}
break;
case ETimedOut:
{
// Timeout request
//iConsole->ErrorNotify(KTimedOut, KErrTimedOut);
}
break;
case ELookingUp:
{
iResolver.Close();
if (iStatus == KErrNone)
// DNS look up successful
{
iNameRecord = iNameEntry();
// Extract domain name and IP address from name record
// iConsole->PrintNotify(KDomainName);
// iConsole->PrintNotify(iNameRecord.iName);
TInetAddr::Cast(iNameRecord.iAddr).Output(ipAddr);
// iConsole->PrintNotify(KIPAddress);
// iConsole->PrintNotify(ipAddr);
// And connect to the IP address
//ConnectL(TInetAddr::Cast(iNameRecord.iAddr).Address());
}
else
{
iStatus = ELookUpFailed;
//iConsole->ErrorNotify(KDNSFailed, iStatus.Int());
}
}
break;
case EComplete:
{
}
break;
case ELookUpFailed:
{
}
break;
default:
break;
};
}
void CEchoEngine::Write(const TDes8 &aChar)
// Exported function wrapping call to CEchoWrite: writes character to socket
{
if ((iEngineStatus == EConnected) && !iEchoWrite->IsActive())
{
iEchoWrite->IssueWrite(aChar);
}
}
void CEchoEngine::Read()
// Exported function wrapping call to CEchoRead: reads character from socket
{
iEchoRead->DoCancel();
if ((iEngineStatus == EConnected)&&(!iEchoRead->IsActive()))
{
iEchoRead->IssueRead();
}
}
//for reading operation
CEchoRead::CEchoRead() :
CActive(EPriorityStandard)
{
}
CEchoRead* CEchoRead::NewL(RSocket* aSocket)
{
CEchoRead* self = NewLC(aSocket);
CleanupStack::Pop();
return self;
}
CEchoRead* CEchoRead::NewLC(RSocket* aSocket)
{
CEchoRead* self = new (ELeave) CEchoRead;
CleanupStack::PushL(self);
self->ConstructL(aSocket);
return self;
}
void CEchoRead::ConstructL(RSocket* aSocket)
{
iEchoSocket = aSocket;
//iConsole = aConsole;
CActiveScheduler::Add(this);
flag = 1;
}
void CEchoRead:oCancel()
// Cancel asychronous read request
{
iEchoSocket->CancelRead();
}
void CEchoRead::RunL()
// Active object request complete handler
{
if (iStatus == KErrNone)
// Character has been read from socket
{
_LIT(KDot,".");
// iConsole->PrintNotify(KDot);
TBuf16<1000> Buffer;
// iConsole->PrintNotify(Buffer);
IssueRead();
}
else
{
// Error: pass it up to user interface
_LIT(KCEchoReadError,"\nCEchoRead error");
//iConsole->ErrorNotify(KCEchoReadError, iStatus.Int());
}
}
void CEchoRead::IssueRead()
// Read data from a stream socket
{
TInt fileLen;
if (!IsActive())
{
iEchoSocket->RecvOneOrMore(len,0,iStatus,iLength);
SetActive();
}
// if(iBuffer1->Des().Length() > 0)
{
}
if(iBuffer1)
{
delete iBuffer1;
iBuffer1 = NULL;
}
}
//for writing
// Construction functions
CEchoWrite::CEchoWrite() :
CActive(EPriorityStandard)
{
}
CEchoWrite* CEchoWrite::NewL(RSocket* aSocket)
{
CEchoWrite* self = NewLC(aSocket);
CleanupStack::Pop();
return self;
}
void CEchoWrite::RunL()
// Active object request complete handler
{
if (iStatus == KErrNone)
{
_LIT(KWriteOperationTimedOut,"\nWrite operation timed out");
switch (iWriteStatus)
{
// Character has been written to socket
case ESending:
{
// iTimer->Cancel(); // Cancel TimeOut timer
iWriteStatus = EWaiting;
}
break;
// Request timed out
case ETimedOut:
// iConsole->ErrorNotify(KWriteOperationTimedOut, KErrTimedOut);
break;
default:
break;
};
}
else
{
// Error: pass it up to user interface
_LIT(KCEchoWriteError,"\nCEchoWrite error");
// iConsole->ErrorNotify(KCEchoWriteError, iStatus.Int());
}
}
CEchoWrite* CEchoWrite::NewLC(RSocket* aSocket)
{
CEchoWrite* self = new (ELeave) CEchoWrite;
CleanupStack::PushL(self);
self->ConstructL(aSocket);
return self;
}
;
void CEchoWrite::ConstructL(RSocket* aSocket)
{
iEchoSocket = aSocket;
// iConsole = aConsole;
CActiveScheduler::Add(this);
// iTimeOut = KTimeOut;
// iTimer = CTimeOutTimer::NewL(10, *this);
iWriteStatus = EWaiting;
}
;
CEchoWrite::~CEchoWrite()
{
// delete iTimer;
}
void CEchoWrite:oCancel()
// Cancel asychronous write request
{
iEchoSocket->CancelWrite();
}
void CEchoWrite::IssueWrite(const TDes8 &aChar)
// Write a data to a stream socket
{
iBuffer2 = HBufC8::NewL( 2000 );
TPtr8 bufferPtr2( iBuffer2->Des() );
bufferPtr2.Copy(aChar);
iEchoSocket->Write(bufferPtr2, iStatus);
if(iBuffer2)
{
delete iBuffer2;
iBuffer2=NULL;
}
// Request timeout
// iTimer->After(iTimeOut);
SetActive();
};
i want to perform read and write operation continuously
but once i perform read and write operation in RunL() method i want to perform same operation again and again
for that i have to call connect and setactive in constructl() or any other issuerequest method
but some where it is ruuning and it's not complete
where i get wrong i dnot understand

oCancel()
Reply With Quote

