📄 sockets1.cpp
字号:
TInt CSocketEngine::GetPort() const
// Report the currently defined port
{
return(iPort);
}
void CSocketEngine::Connect()
//
// Attempt to connect to the server defined by iServerName
//
{
TInetAddr addr;
if (addr.Input(iServerName)==KErrNone)
Connect(addr.Address()); // server name already a valid IP addr
else
{ // need to look up IP addr using DNS
TInt err=iHostResolver.Open(iSocketServer,KAfInet,KProtocolInetUdp);
if (err==KErrNone)
{
iHostResolver.GetByName(iServerName,iNameEntry,iStatus);
_LIT(KGetByNameActive,"GetByName active");
iObserver->LogInfo(KGetByNameActive,iServerName);
}
else
{ // deal with all errors in the RunL()
TRequestStatus* q=(&iStatus);
User::RequestComplete(q,err);
_LIT(KConnectErr,"ConnectErr");
iObserver->LogInfo(KConnectErr,iServerName);
}
iState=ESeLookingUp; // now started request
SetActive();
}
}
//////////////////////////////////////////////////////////////////////////////////
class CAppSpecificListView : public CQikViewBase, public MSocketEngineObserver
{
protected:
// from CQikViewBase
TVwsViewId ViewId() const;
void HandleCommandL(CQikCommand& aCommand);
void ViewConstructL();
void ViewDeactivated();
void ViewActivatedL(const TVwsViewId& aPrevViewId,const TUid aCustomMessageId,const TDesC8& aCustomMessage);
// from MSocketEngineObserver
void LogInfo(const TDesC& aBuf,const TDesC& aBuf2);
// new methods
void RebuildListbox();
public:
// new methods
~CAppSpecificListView();
CAppSpecificListView(CAppSpecificUi& aAppUi);
protected:
RSocketServ iSocketServ;
CSocketEngine* iSocketEngine;
CLogText* iLogText;
};
CAppSpecificListView::~CAppSpecificListView()
{
delete(iSocketEngine);
delete(iLogText);
iSocketServ.Close();
}
CAppSpecificListView::CAppSpecificListView(CAppSpecificUi& aAppUi) :
CQikViewBase(aAppUi,KNullViewId)
{}
TVwsViewId CAppSpecificListView::ViewId() const
//
// All views are uniquely identified within the entire system. A TVwsViewId consists of
// the application uid (uid3) and app specific view uid
//
{
return(KViewIdListView);
}
void CAppSpecificListView::HandleCommandL(CQikCommand& aCommand)
//
// Handle the commands coming in from the controls that can deliver cmds..
//
{
switch (aCommand.Id())
{
case EAppCmdConnect:
{
// any previous one - cancels connections etc
delete(iSocketEngine);
iSocketEngine=NULL;
// reset log and display to blank
if (!iLogText)
{
iLogText=new(ELeave)CLogText;
iLogText->ConstructL();
}
iLogText->ResetText();
RebuildListbox();
// a new socket
iSocketEngine=new(ELeave)CSocketEngine(iSocketServ,this);
iSocketEngine->ConstructL();
_LIT(KServerName,"www.uiq.com");
iSocketEngine->SetServerName(KServerName);
iSocketEngine->SetPort(80);
// asynchronously attempt to connect to a server
iSocketEngine->Connect();
break;
}
default: // e.g. the back button...
CQikViewBase::HandleCommandL(aCommand);
break;
}
}
void CAppSpecificListView::RebuildListbox()
//
// Rebuild the listbox from scratch
//
{
TRAPD(err,
CQikListBox* listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId);
listbox->RemoveAllItemsL();
MQikListBoxModel& model(listbox->Model());
model.ModelBeginUpdateLC();
TInt count=iLogText->Count();
for (TInt i=0;i<count;i+=2)
{
MQikListBoxData* lbData=model.NewDataL(MQikListBoxModel::EDataNormal);
CleanupClosePushL(*lbData);
lbData->AddTextL(iLogText->At(i),EQikListBoxSlotText1);
lbData->AddTextL(iLogText->At(i+1),EQikListBoxSlotText2);
CleanupStack::PopAndDestroy(lbData);
}
model.ModelEndUpdateL();
);
}
void CAppSpecificListView::LogInfo(const TDesC& aBuf,const TDesC& aBuf2)
// Called back by the engine to add some log text to output log so operations are visible
{
// This is an example app. Logging content to the list box in this fashion cannot be
// recommended for commerical quality applications
iLogText->LogText(aBuf2);
iLogText->LogText(aBuf);
RebuildListbox();
}
void CAppSpecificListView::ViewConstructL()
{
// Loads information about the UI configurations this view supports
// together with definition of each view.
ViewConstructFromResourceL(R_APP_VIEW_CONFIGURATIONS);
// obtain IPC connected to the Symbian socket server process that does lots on our behalf
User::LeaveIfError(iSocketServ.Connect());
// seed our main list box with all the protocols supported
CQikListBox* listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId);
MQikListBoxModel& model(listbox->Model());
model.ModelBeginUpdateLC();
TBuf<128>bb;
TUint count;
User::LeaveIfError(iSocketServ.NumProtocols(count));
TProtocolDesc info;
for (TUint i=0;i<count;i++)
{
// just to be perverse the GetProtocolInfo() does not base its index from 0 - it indexes from 1 !!
User::LeaveIfError(iSocketServ.GetProtocolInfo(i+1,info));
// add two lines per supported protocol - primary name + some characteristics
MQikListBoxData* lbData=model.NewDataL(MQikListBoxModel::EDataNormal);
CleanupClosePushL(*lbData);
lbData->AddTextL(info.iName,EQikListBoxSlotText1);
_LIT(KTypeProtAddr,"Type:%u, Prot:%u, Addr:%u");
bb.Format(KTypeProtAddr,info.iSockType,info.iProtocol,info.iAddrFamily);
lbData->AddTextL(bb,EQikListBoxSlotText2);
CleanupStack::PopAndDestroy(lbData);
}
model.ModelEndUpdateL();
// add a view title
iEikonEnv->ReadResourceL(bb,R_STR_APP_TITLE);
ViewContext()->AddTextL(1,bb);
}
void CAppSpecificListView::ViewDeactivated()
{
}
void CAppSpecificListView::ViewActivatedL(
const TVwsViewId& aPrevViewId,
const TUid aCustomMessageId,
const TDesC8& aCustomMessage)
{
}
//////////////////////////////////////////////////////////////////////////////
CAppSpecificUi::~CAppSpecificUi()
{
}
void CAppSpecificUi::ConstructL()
//
// Normal primary entry point to a Symbian App
//
{
CQikAppUi::ConstructL();
// We create and add a list view.
CAppSpecificListView* list=new(ELeave)CAppSpecificListView(*this);
CleanupStack::PushL(list);
list->ConstructL();
AddViewL(*list); // takes ownership
CleanupStack::Pop(list);
SetDefaultViewL(*list);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Standard Symbian application framework code when creating an application
class CAppSpecificDocument : public CQikDocument
{
protected:
CQikAppUi* CreateAppUiL();
public:
CAppSpecificDocument(CQikApplication& aApp);
static CAppSpecificDocument* NewL(CQikApplication& aApp);
protected:
};
CAppSpecificDocument::CAppSpecificDocument(CQikApplication& aApp) :
CQikDocument(aApp)
{
__DECLARE_NAME(_S("CAppSpecificDocument"));
}
CAppSpecificDocument* CAppSpecificDocument::NewL(CQikApplication& aApp)
{
return(new(ELeave)CAppSpecificDocument(aApp));
}
CQikAppUi* CAppSpecificDocument::CreateAppUiL()
{
return(new(ELeave)CAppSpecificUi);
}
//////////////////////////////////////////////////////////////////////////////////
// Standard Symbian application framework code when creating an application
class CAppSpecificApplication : public CQikApplication
{
protected:
TUid AppDllUid() const;
CApaDocument* CreateDocumentL();
};
TUid CAppSpecificApplication::AppDllUid() const
{
return(KAppSpecificUid);
}
CApaDocument* CAppSpecificApplication::CreateDocumentL()
{
return(CAppSpecificDocument::NewL(*this));
}
//////////////////////////////////////////////////////////////////////////////////
// Standard Symbian application start up code
LOCAL_C CApaApplication* NewApplication()
{
return(new CAppSpecificApplication);
}
GLDEF_C TInt E32Main()
{
return(EikStart::RunApplication(NewApplication));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -