×
Namespaces

Variants
Actions

Socket code for establishing connection between two emulators on different machine

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: judith14 (29 Jun 2007)
Last edited: hamishwillee (31 Jul 2012)


The following code is establish connection between two emulators on different machine. This is done in a synchronous way. Unlike 2.x emulators there is no need for setting up IP for emulator in 3.0.

Server Side
 
SocketAcceptAO.h
 
#include <e32base.h>
#include <es_sock.h>
#include <in_sock.h>
 
_LIT(Krecv,"CLIENT DATA:");
class CSocketAcceptAO : public CActive
{
public:
CSocketAcceptAO();
virtual ~CSocketAcceptAO();
 
// New Function
void TestSocketAcceptAO();
void Receive();
void Senddata(TBuf8<128> aData);
// From CActive
void RunL();
void DoCancel() {}
void Close();
private:
RSocketServ iSockServ;
RSocket iSocket;
RSocket iBlankSocket;
TInetAddr iAddress;
TSockXfrLength iLen;
TBuf<64> iServerName;
TRequestStatus iRStatus;
TRequestStatus iSStatus;
TBuf8<128> iBuffer;
TBuf<128> iRecv;
TBuf<500> iRecv1;
TInt flag;
 
};
 
Server side
 
SocketAcceptAO.cpp
 
 
#include "SocketAcceptAO.h"
 
#include <es_sock.h>
#include <in_sock.h>
#include <aknnotewrappers.h>
#include <CommDbConnPref.h>
#include <eikenv.h>
 
 
CSocketAcceptAO::CSocketAcceptAO(): CActive( EPriorityStandard ), iServerName(NULL)
{
CActiveScheduler::Add( this );
}
 
CSocketAcceptAO::~CSocketAcceptAO()
{
 
}
 
void CSocketAcceptAO::Close()
{
 
iBlankSocket.Close();
iSocket.Close();
iSockServ.Close();
}
 
void CSocketAcceptAO::TestSocketAcceptAO()
{
TRequestStatus iSockStatus;
iSockServ.Connect();
 
 
RConnection conn;
TCommDbConnPref prefs;
 
prefs.SetDirection(ECommDbConnectionDirectionOutgoing);
prefs.SetBearerSet(KCommDbBearerLAN);
prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
prefs.SetIapId(3);
 
conn.Open(iSockServ);
conn.Start(prefs);
 
 
User::LeaveIfError( iBlankSocket.Open( iSockServ ) );
 
User::LeaveIfError( iSocket.Open( iSockServ,
KAfInet,
KSockStream,
KProtocolInetTcp,conn) );
 
iAddress.SetAddress( KInetAddrAny );
 
iAddress.SetPort( 3000 );
 
if ( iSocket.Bind( iAddress ) != KErrNone )
{
}
else
{
if ( iSocket.Listen( 5 ) != KErrNone )
{
}
else
{
iSocket.Accept( iBlankSocket, iStatus );
 
CAknInformationNote* infor= new (ELeave) CAknInformationNote;
 
infor->ExecuteLD(_L("Listening"));
 
this->flag=0;
SetActive();
}
}
}
 
void CSocketAcceptAO::RunL()
{
if(this->flag==0)
{
if(iStatus==KErrNone)
{
CAknInformationNote* infor= new (ELeave) CAknInformationNote;
infor->ExecuteLD(_L("Connection Accepted"));
}
else
{
CAknInformationNote* infor1= new (ELeave) CAknInformationNote;
infor1->ExecuteLD(_L("Connection Not Accepted"));
}
}
 
if(this->flag==1)
{
if(iRStatus==KErrNone)
{
iRecv.Copy(iBuffer);
iRecv1.Copy(Krecv);
iRecv1.Append(iRecv);
CAknInformationNote* infor2= new (ELeave)CAknInformationNote;
infor2->ExecuteLD(iRecv1);
this->flag=2;
}
else
{
CAknInformationNote* infor3= new (ELeave)CAknInformationNote;
infor3->ExecuteLD(_L("Data Not Received"));
this->flag=2;
}
}
}
 
void CSocketAcceptAO::Receive()
{
iBlankSocket.RecvOneOrMore(iBuffer,0,iRStatus,iLen);
CAknInformationNote* infor3= new (ELeave) CAknInformationNote;
infor3->ExecuteLD(_L("Waiting for Data"));
 
User::WaitForRequest(iRStatus);
if(iRStatus==KErrNone)
{
TBuf<256> iRecvBuffer;
iRecvBuffer.Copy(iBuffer);
CEikonEnv::InfoWinL(_L("CLIENT:"),iRecvBuffer);
iRecvBuffer.Zero();
}
 
else
{
CEikonEnv::InfoWinL(_L("Receiving data failed"),_L(""));
}
 
 
}
 
void CSocketAcceptAO::Senddata(TBuf8<128> aData)
{
iBlankSocket.Write(aData,iSStatus);
User::WaitForRequest(iSStatus);
 
if(iSStatus==KErrNone)
{
CAknInformationNote* infor2= new (ELeave)CAknInformationNote;
infor2->ExecuteLD(_L("Data Sent Successfully"));
}
else
{
CAknInformationNote* infor3= new (ELeave) CAknInformationNote;
infor3->ExecuteLD(_L("Sending Data Failed"));
}
}
 
 
Client Side:
 
Chatengine.h
 
#include <in_sock.h>
#include <es_sock.h>
#include "client.hrh"
 
_LIT(Krecv,"SERVER DATA:");
 
class Chatengine:public CActive
{
public:
static Chatengine* NewL();
static Chatengine* NewLC();
virtual ~Chatengine();
public:
void Senddata(TBuf8<1000> aData);
void Receive();
void ConnectL(const TDesC& aIpaddr);
void Wlaniaplist();
void Disconnect();
Chatengine();
protected:
void DoCancel();
void RunL();
private:
void ConstructL();
private:
void ConnectL( TUint32 aAddr );
RSocket iSocket;
RConnection conn;
RSocketServ iSocketServ;
TInetAddr iAddress;
TInt iPort;
TSockXfrLength iLen;
TRequestStatus iRStatus;
TRequestStatus iSStatus;
TBuf8<1000> iBuffer;
TBuf<1000> iRecv;
TBuf<1000> iRecv1;
TInt flag;
};
 
Client Side:
 
Chatengine.cpp
 
#include <eikgted.h>
#include <eikenv.h>
#include <stringloader.h>
#include <client.rsg>
#include <aknnotewrappers.h>
#include "Chatengine.h"
#include <CommDbConnPref.h>
#include <CDBCOLS.H>
#include <commdb.h>
 
Chatengine* Chatengine::NewL()
{
Chatengine* self = Chatengine::NewLC();
CleanupStack::Pop( self );
return self;
}
 
 
Chatengine* Chatengine::NewLC()
{
Chatengine* self = new ( ELeave ) Chatengine();
CleanupStack::PushL( self );
self->ConstructL();
return self;
}
 
Chatengine::Chatengine()
: CActive(EPriorityStandard)
{
 
}
 
void Chatengine::ConstructL()
{
 
CActiveScheduler::Add( this );
 
}
 
 
Chatengine::~Chatengine()
{
}
 
void Chatengine::Disconnect()
{
iSocket.Close();
conn.Close();
iSocketServ.Close();
}
 
 
void Chatengine::ConnectL(const TDesC& aIpaddr)
{
 
TInetAddr addr;
if ( addr.Input(aIpaddr) == KErrNone )
{
// server name is already a valid ip address
ConnectL( addr.Address() );
}
 
}
 
 
void Chatengine::ConnectL( TUint32 aAddr )
{
User::LeaveIfError( iSocketServ.Connect() );
TCommDbConnPref prefs;
prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
prefs.SetDirection(ECommDbConnectionDirectionOutgoing);
prefs.SetBearerSet(KCommDbBearerLAN);
conn.Open(iSocketServ);
conn.Start(prefs);
User::LeaveIfError( iSocket.Open( iSocketServ,
KAfInet,
KSockStream,
KProtocolInetTcp,conn) );
iAddress.SetPort( 3000 );
iAddress.SetAddress( aAddr );
iSocket.Connect( iAddress, iStatus );
CAknInformationNote* infor2= new (ELeave) CAknInformationNote;
this->flag=0;
infor2->ExecuteLD(_L("Connecting"));
SetActive();
}
 
void Chatengine::DoCancel()
{
}
 
void Chatengine::Senddata(TBuf8<1000> aData)
{
iSocket.Write(aData,iRStatus);
User::WaitForRequest(iRStatus);
 
if(iRStatus==KErrNone)
{
CAknInformationNote* infor2= new (ELeave) CAknInformationNote;
infor2->ExecuteLD(_L("Data Sent Successfully"));
aData.Zero();
}
else
{
CAknInformationNote* infor3= new (ELeave) CAknInformationNote;
infor3->ExecuteLD(_L("Sending Data Failed"));
}
}
 
void Chatengine::RunL()
{
if(this->flag==0)
{
if ( iStatus == KErrNone )
{
CAknInformationNote* infor= new (ELeave) CAknInformationNote;
infor->ExecuteLD(_L("Connection Established"));
}
else
{
iSocket.Close();
CAknInformationNote* infor1= new (ELeave) CAknInformationNote;
infor1->ExecuteLD(_L("Connection Not Established"));
}
}
 
if(this->flag==1)
{
if(iSStatus==KErrNone)
{
iRecv.Copy(iBuffer);
iBuffer.Zero();
iRecv1.Copy(Krecv);
iRecv1.Append(iRecv);
iRecv.Zero();
CAknInformationNote* infor5= new (ELeave) CAknInformationNote;
infor5->ExecuteLD(iRecv1);
iRecv1.Zero();
this->flag=2;
}
else
{
CAknInformationNote* infor6= new (ELeave) CAknInformationNote;
infor6->ExecuteLD(_L("Data Not Received"));
this->flag=2;
}
}
}
 
void Chatengine::Receive()
{
iSocket.RecvOneOrMore(iBuffer,0,iSStatus,iLen);
CAknInformationNote* infor4= new (ELeave) CAknInformationNote;
infor4->ExecuteLD(_L("Waiting for Data"));
 
User::WaitForRequest(iSStatus);
 
if(iSStatus == KErrNone)
{
TBuf<256> iRecvBuffer;
iRecvBuffer.Copy(iBuffer);
CEikonEnv::InfoWinL(_L("SERVER:"),iRecvBuffer);
iRecvBuffer.Zero();
 
}
else
{
CEikonEnv::InfoWinL(_L("Receiving Data failed"),_L(""));
}
}
This page was last modified on 31 July 2012, at 13:23.
46 page views in the last 30 days.
×