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

📄 tcp_clie.cpp

📁 一个symbian下的tcp连接的客户端/服务器源代码
💻 CPP
字号:
// tcp_clie.cpp
//
// Copyright (c) 1999 Symbian Ltd.  All rights reserved.
//
//////////////////////////////////////////////
//
// Engine code for tcp_cli. The interesting part 
// of the engine is in the function StartTestL().
// Everything else is peripheral.
//
// This program has to be run together with tcp_srv,
// a minimal "server" which accepts connections
// and sends some data to the client.
//
// To run: Launch tcp_srv and press the "Start"
// button. Then launch tcp_cli and press the "Start"
// button.
//
///////////////////////////////////////////////

#include <in_sock.h>

#include "tcp_clie.h"

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

CMinTcpEngine::CMinTcpEngine() :CActive(CActive::EPriorityStandard),
 iRecvLen(256)
	{
	}

void CMinTcpEngine::ConstructL()
	{
	CActiveScheduler::Add(this);
	}

CMinTcpEngine::~CMinTcpEngine()
	{
	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 CMinTcpEngine::SetObserver(MCommsTestObserver* aObserver)
	{
	iObserver=aObserver;
	}

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

	const TUint KHttpPort = 2080;

void CMinTcpEngine::StartTestL()
	{
	TInt err;
	// IP address specified here
	TInetAddr localLoop(KInetAddrLoop, 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 a socket
	err=iSocket.Open(ss, KAfInet, KSockStream, KUndefinedProtocol);
	User::LeaveIfError(err);

	// Make a connection
	iStatusText=_L("\nConnecting...");
	NotifyProgress();
	iChannelState=EChannelConnected;
	iSocket.Connect(localLoop, iStatus);
	SetActive();
	}

void CMinTcpEngine::RunL()
	{
	TRequestStatus iStatus2;
	switch(iChannelState)
		{
	case EChannelConnected:
		{
		if (iStatus==KErrNone)
			{
			// Connection was made successfully
			iStatusText=_L("\nConnection established");
			NotifyProgress();
						
			
			// the following lines send data to the server.
			// The data sent (GET) is a http command, which if the server
			// is a http server will result in an acknowledgement being returned

			// send some data to server
			TBuf8<256> writeBuffer(_L("GET /\n\n"));
			iSocket.Send(writeBuffer, 0, iStatus2);
			iChannelState=EChannelSending;
			SetActive();
			}
		else
			{
			// Display info in user interface
			iStatusText=_L("\nFailed to connect");
			NotifyProgress();
			}
		}
		break;

	case EChannelSending:
		{
		// Receive data from the "server"
			iSocket.RecvOneOrMore(iDataBuffer, 0, iStatus, iRecvLen);
			iChannelState=EChannelReceiving;
			SetActive();
			break;
		}

	case EChannelReceiving:
		{
		if (iStatus==KErrNone)
			{
			// Data was received successfully
			iStatusText=_L("\nReceived data");
			NotifyProgress();
			// Display received data in user interface
			iStatusText=iDataBuffer;
			NotifyProgress();
			}
		else
			{
			iStatusText=_L("\nError reading data");
			}

		iStatusText=_L("\nClose the connection...");
		NotifyProgress();

		iSocket.Close();
		iStatusText=_L("\nEnd of test.");
		NotifyProgress();
			}
		break;
	default:
		break;
		}
	}

void CMinTcpEngine::DoCancel()
	{
	switch (iChannelState)
		{
		case EChannelConnected:
		case EChannelReceiving:
			{
			iSocket.CancelAll();
			// Close all sockets
			iSocket.Close();
			iChannelState=EChannelIdle;
			iStatusText=_L("\nTest cancelled by user.");
			NotifyProgress();
			}
			break;
		default:
			break;
		}
	}

⌨️ 快捷键说明

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