⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcp_srve.cpp

📁 一个symbian下的tcp连接的客户端/服务器源代码
💻 CPP
字号:
// tcp_srve.cpp
//
// Copyright (c) 1999 Symbian Ltd.  All rights reserved.
//
//////////////////////////////////////////////
//
// Engine code for tcp_srv. The interesting part 
// of the engine is in the function StartTestL().
// Everything else is peripheral.
//
// StartTestL() runs a simple "server" that waits
// for incoming connections. When a connection is
// made, it sends some test data to the client.
//
///////////////////////////////////////////////

#include <in_sock.h>

#include "tcp_srve.h"

///////////////////////////////////////////////
//
// Construct the engine
//
///////////////////////////////////////////////
CTcpSrvEngine* CTcpSrvEngine::NewL()
	{
	CTcpSrvEngine* self = new (ELeave) CTcpSrvEngine;
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(); // self
	return self;
	}

CTcpSrvEngine::CTcpSrvEngine() :CActive(CActive::EPriorityStandard)
	{
	}

void CTcpSrvEngine::ConstructL()
	{
	iStatusText = _L("Press Start to begin comms test");
	CActiveScheduler::Add(this);
	}

CTcpSrvEngine::~CTcpSrvEngine()
	{
	Cancel();
	}

///////////////////////////////////////////////
//
// Functions for using the engine's observer.
// This allows the engine to send messages to
// the user interface to notify it about the
// status of the TCP test, without knowing
// anything about how the user interface is
// implemented. (Many EPOC application engines
// also use a mechanism similar to this.)
///////////////////////////////////////////////
void CTcpSrvEngine::SetObserver(MCommsTestObserver* aObserver)
	{
	iObserver=aObserver;
	}

void CTcpSrvEngine::NotifyProgress()
	{
	iObserver->HandleProgressEvent();
	}


///////////////////////////////////////////////
//
// Ensure that the Comms environment has been
// set up - this is normally the responsibility
// of the system Shell, but this kind of test 
// program doesn't have such a thing.
//
///////////////////////////////////////////////

const TUint KHttpPort = 2080;

void CTcpSrvEngine::StartTestL()
	{
	StartConnection();
	SetActive();
	}

void CTcpSrvEngine::RunL()
	{
	if (iStatus==KErrNone)
		{
		switch (iChannelState)
			{
		case EChannelListening:
			{
			// Connection was made successfully
			// Send some data to client
			TBuf8<256> writeBuffer(_L("\nSome data"));
			iChannelState=EChannelSending;
			iStatusText=_L("\nSending data");
			NotifyProgress();
			iConnection.Send(writeBuffer, 0, iStatus);
			SetActive();
			}
			break;
		case EChannelSending:
			{
			// Data was sent successfully
			iStatusText=_L("\nClose the connection...");
			NotifyProgress();
			iChannelState=EChannelShutingDown;
			iConnection.Close();
			iStatusText=_L("\nEnd of test.");
			NotifyProgress();

			iSocket.Close();

			StartConnection();
			SetActive();	// start again
			}
			break;
		default:
			break;
			}
		}
	}


void CTcpSrvEngine::DoCancel()
	{
	switch (iChannelState)
		{
		case EChannelSending:
		case EChannelListening:
			{
			iSocket.CancelAll();
			iSocket.Close();
			iConnection.CancelAll();
			iConnection.Close();
			iChannelState=EChannelIdle;
			iStatusText=_L("\nTest cancelled by user.");
			NotifyProgress();
			}
			break;
		default:
			break;
		}
	}

void CTcpSrvEngine::StartConnection()
{
	TInt err;	
	TInetAddr httpPort(KInetAddrAny, KHttpPort);

	// Make a connection to the socket server
	RSocketServ ss;
	err=ss.Connect();
	User::LeaveIfError(err);

	iStatusText=_L("\nCreate a TCP socket...");
	NotifyProgress(); // Display info in user interface

	// Open two sockets - one to listen on, one for making a connection and sending data
	err=iSocket.Open(ss, KAfInet, KSockStream, KUndefinedProtocol);
	User::LeaveIfError(err);
	err=iConnection.Open(ss);
	User::LeaveIfError(err);

	// Bind the socket to the required port.
	iSocket.Bind(httpPort);
	iStatusText=_L("\nListen at port 80...");
	NotifyProgress();

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

	// Accept incoming connections
	iSocket.Accept(iConnection, iStatus);
	iChannelState=EChannelListening;
	iStatusText=_L("\nWait for incoming connection...");
	NotifyProgress();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -