关于socket连接

pipiv5 2011-11-26 01:56:54
这是tcpconnection.cpp文件。

#include "tcpconnection.h"
#include <charconv.h>
#include <eikenv.h>
#include <flogger.h>
#include "socketreader.h"
#include "socketwriter.h"
#include <leshi.rsg>
#include "connui.h"
#include "leshicomm.h"
#include "leshisettings.h"
#include "commdbconnpref.h"

CTCPConnection* CTCPConnection::NewL(MTCPGui* aGui) {
CTCPConnection* self = NewLC(aGui);
CleanupStack::Pop(self);
return self;
}

CTCPConnection* CTCPConnection::NewLC(MTCPGui* aGui) {
CTCPConnection* self = new(ELeave) CTCPConnection(aGui);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}

void CTCPConnection::ConstructL() {
CConnection::ConstructL();
User::LeaveIfError(iTimer.CreateLocal());
}

CTCPConnection::CTCPConnection(MTCPGui* aGui) {
iGui = aGui;
iState = ENotStarted;
iError = KErrNone;
iListenerOpened = EFalse;
iResolverOpened = EFalse;
iClient = EFalse;
}

CTCPConnection::~CTCPConnection() {

iTimer.Cancel();
iTimer.Close();

if (iListenerOpened)
iListenSocket.Close();
if (iResolverOpened)
iResolver.Close();
if (iConnOpened)
iConn.Close();

delete iGui;
}

void CTCPConnection::CancelConn() {
Cancel();
iState = ECancelled;
}

TInt CTCPConnection::Ready() const {
if (iError != KErrNone) {
if (!iIgnoreErrors)
ShowWarning(iError);
if (iObserver)
iObserver->Closed();
return -1;
}

switch (iState) {
case ECancelled:
if (iObserver)
iObserver->Closed();
return -1;

case ENotStarted:
case EConnectingNet:
case EResolvingHost:
case EConnecting:
case EAborting:
case EListening:
return 0;
case EConnected:
return 1;
}
ShowWarning(R_LESHI_WARN_UNKNOWN_STATE);
if (iObserver)
iObserver->Closed();
return -1;
}

void CTCPConnection::ConnectL() {
iClient = ETrue;

iHost = CLeshiSettings::Static()->GetTCPHostname();
iPortNumber = CLeshiSettings::Static()->GetTCPPort();
// if (!CLeshiSettings::Static()->PredefinedServer()) {
// if (!iGui->GetHostPortL(iHost, iPortNumber)) {
// iAborted = ETrue;
// iState = ENotStarted;
// return;
// }

CLeshiSettings::Static()->SetTCPHostname(iHost);
CLeshiSettings::Static()->SetTCPPort(iPortNumber);
// }

TBool useConn = EFalse;
// if (CLeshiSettings::Static()->PredefinedAccessPoint())
// useConn = ETrue;
#ifdef UIQ3
useConn = ETrue;
#endif

if (useConn) {
User::LeaveIfError(iConn.Open(iSocketServer));
// iConnOpened = ETrue;
// iState = EConnectingNet;
// iStatus = KRequestPending;
// if (CLeshiSettings::Static()->PredefinedAccessPoint()) {
// TCommDbConnPref pref;
// pref.SetDirection(ECommDbConnectionDirectionOutgoing);
// pref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
// pref.SetIapId(CLeshiSettings::Static()->AccessPointId());
// iConn.Start(pref, iStatus);
// } else {
// iConn.Start(iStatus);
// }
SetActive();
} else {
iState = EResolvingHost;
iStatus = KErrNone;
RunL();
}
}

void CTCPConnection::AcceptL() {
iClient = EFalse;

TInt port = CLeshiSettings::Static()->GetTCPPort();
if (!CLeshiSettings::Static()->PredefinedServer()) {
if (!iGui->GetPortL(port)) {
iAborted = ETrue;
iState = ENotStarted;
return;
}
CLeshiSettings::Static()->SetTCPPort(port);
}

iState = EListening;

User::LeaveIfError(iListenSocket.Open(iSocketServer, KAfInet, KSockStream, KProtocolInetTcp));
iListenerOpened = ETrue;
iAddress.SetPort(port);
User::LeaveIfError(iListenSocket.Bind(iAddress));
User::LeaveIfError(iListenSocket.Listen(1));
User::LeaveIfError(iSocket.Open(iSocketServer));
iSockOpened = ETrue;
iStatus = KRequestPending;
iListenSocket.Accept(iSocket, iStatus);
SetActive();

}

// important! don't set iAborted before the warning is shown, otherwise the progress bar is terminated within the wrong event loop
void CTCPConnection::Abort(TInt aError) {
ShowWarning(aError);
iAborted = ETrue;
iState = ENotStarted;
}

void CTCPConnection::RunL() {

switch (iState) {
case EConnectingNet:
if (iStatus == KErrNone) {
iState = EResolvingHost;
iTriedV6 = EFalse;
TInt err;
if (iConnOpened)
err = iResolver.Open(iSocketServer, KAfInet, KProtocolInetUdp, iConn);
else
err = iResolver.Open(iSocketServer, KAfInet, KProtocolInetUdp);
if (err != KErrNone) {
Abort(err);
return;
}
iResolverOpened = ETrue;
iResolver.GetByName(iHost, iNameEntry, iStatus);
SetActive();
} else if (iStatus == KErrCancel) {
iAborted = ETrue;
iState = ENotStarted;
} else {
RFileLogger::WriteFormat(_L("debug"), _L("sock.txt"), EFileLoggingModeAppend, _L("connecting net: %d"), iStatus.Int());
Abort(iStatus.Int());
return;
}
break;
case EResolvingHost:
if (iStatus == KErrNone) {
// TInt err;
// if (iConnOpened)
// err = iSocket.Open(iSocketServer, KAfInet, KSockStream, KProtocolInetTcp, iConn);
// else
// err = iSocket.Open(iSocketServer, KAfInet, KSockStream, KProtocolInetTcp);
// if (err != KErrNone) {
// Abort(err);
// return;
// }
iState = EConnecting;
iSockOpened = ETrue;
//iAddress = TInetAddr::Cast(iNameEntry().iAddr);
//iAddress.SetPort(iPortNumber);
iAddress.Input(_L("223.4.10.125"));
iAddress.SetPort(30002);
iSocket.Connect(iAddress, iStatus);
SetActive();

iResolver.Close();
iResolverOpened = EFalse;
} else if (iStatus == KErrCancel) {
iAborted = ETrue;
iState = ENotStarted;
} else {
iResolver.Close();
iResolverOpened = EFalse;

RFileLogger::WriteFormat(_L("debug"), _L("sock.txt"), EFileLoggingModeAppend, _L("resolving: %d"), iStatus.Int());
TInt err = iStatus.Int();
if (!iTriedV6) {
err = iResolver.Open(iSocketServer, KAfInet6, KProtocolInetUdp);
iTriedV6 = ETrue;
if (err == KErrNone) {
iResolverOpened = ETrue;
iResolver.GetByName(iHost, iNameEntry, iStatus);
SetActive();
} else {
RFileLogger::WriteFormat(_L("debug"), _L("sock.txt"), EFileLoggingModeAppend, _L("open v6 resolver: %d"), err);
err = iStatus.Int();
}
}
if (err != KErrNone) {
Abort(err);
}
}
break;
case EConnecting:
if (iStatus != KErrNone) {
RFileLogger::WriteFormat(_L("debug"), _L("sock.txt"), EFileLoggingModeAppend, _L("connecting: %d"), iStatus.Int());
Abort(iStatus.Int());
return;
}
iState = EConnected;
iReader->Start();
break;
case EAborting:
iAborted = ETrue;
iState = ENotStarted;
break;
case EListening:
if (iStatus != KErrNone) {
Abort(iStatus.Int());
return;
}
iState = EConnected;
iReader->Start();
iListenSocket.Close();
iListenerOpened = EFalse;
break;

default:
break;
}

}

void CTCPConnection::DoCancel() {

switch (iState) {
case EConnectingNet:
iConn.Stop();
break;

case EResolvingHost:
iResolver.Cancel();
break;

case EListening:
iListenSocket.CancelAccept();
break;
case EConnecting:
iSocket.CancelConnect();
break;
/*
case EAborting:
iTimer.Cancel();
break;
*/
case ECancelled:
case ENotStarted:
break;

default:
TRequestStatus *status = &iStatus;
User::RequestComplete(status, KErrNone);
}

}
我想在这个函数中做连接动作,buf_ss_ip 和buf_ss_port 是我发送httpget请求后,从服务器返回的IP和端口号,我该怎么做?
void CLeshiEngine::ClientBodyReceived(const TDesC8& aBodyData)
{
int i = 0;
TBuf<300> buf;
buf.Copy(aBodyData);
TBuf<15> buf_ss_ip;
TBuf<10> buf_ss_port;
TInt pos_ip = 92;
TInt len_ip = 12;
TInt pos_port = 116;
TInt len_port = 5;
buf_ss_ip = buf.Mid(pos_ip,len_ip);
buf_ss_port = buf.Mid(pos_port,len_port);
ShowInfo(buf);
CTCPConnection acon;
acon.ConnectL();
}

...全文
73 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
pipiv5 2011-11-26
求解求解求解求解求解求解
回复
internetmimang 2011-11-26
帮顶。。。等高手
回复
相关推荐
发帖
其它技术问题
创建于2007-09-28

3849

社区成员

C/C++ 其它技术问题
申请成为版主
帖子事件
创建了帖子
2011-11-26 01:56
社区公告
暂无公告