当前位置: 首页>>代码示例>>C++>>正文


C++ NetConnection::Connect方法代码示例

本文整理汇总了C++中NetConnection::Connect方法的典型用法代码示例。如果您正苦于以下问题:C++ NetConnection::Connect方法的具体用法?C++ NetConnection::Connect怎么用?C++ NetConnection::Connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NetConnection的用法示例。


在下文中一共展示了NetConnection::Connect方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: _tmain

int _tmain(int argc, _TCHAR* argv[])
{
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

	srand(NetGetTime());

	Peer* sender; int size;

	NetConnection connection;
	connection.Open(5008);

	NetConnection server;
	server.Open(5007);

	connection.connection.SetRTT(0);
	connection.connection.SetDrop(0);
	connection.connection.SetVariance(0);

	printf("Doing Connection Security Test\n");

	for (int i = 0; i < 50; i++)
	{
		char* buffer = new char[i*6];
		for (int in = 0; in < i*6; in++)
		{
			buffer[in] = rand();
		}
		connection.connection.Send(Address(127,0,0,1,5007), buffer, i*6);
		delete[] buffer;
	}

	NetSleep(1000);//wait to receive messages

	//get all messages out
	char* out;
	while (out = server.Receive(sender, size))
	{
		delete[] out;
	}
	if (server.peers.size() != 0)
		printf("Connection security test failed!!!\n\n");
	else
		printf("Connection security test passed.\n\n");

	auto t = std::thread([](NetConnection* server)
	{
		while (true)
		{
			Peer* sender; int size;
			server->Receive(sender, size);

			if (server->peers.size() > 5)
				return;
		}
	}, &server);

	int status = connection.Connect(Address(127,0,0,1,5007), "testing", 0);
	if (status < 0)
	{
		printf("Connection Test Failed!!!\n");
	}

	NetConnection cons[5];
	for (int i = 0; i < 5; i ++)
	{
		cons[i].Open(5010+i);
		int stat = cons[i].Connect(Address(127,0,0,1,5007), "yo", 0);
		if (stat < 0)
			printf("Connection test failed!\n");
	}

	if (t.joinable())
		t.join();

	printf("Connection Test Successful\n");

	for (int i = 0; i < 5; i++)
	{
		//cons[i].Disconnect();
		cons[i].Close();
	}

	while (server.peers.size() > 1)
	{
		Peer* s; int size;
		server.Receive(s, size);
		NetSleep(1);
	}

	printf("Connections closed successfully\n\n");

	printf("Doing Security Test\n");

	for (int i = 0; i < 50; i++)
	{
		char* buffer = new char[i*6];
		for (int in = 0; in < i*6; in++)
		{
			buffer[in] = rand();
		}
//.........这里部分代码省略.........
开发者ID:matt-attack,项目名称:netlibrary,代码行数:101,代码来源:Netlib+Unit+Test.cpp


注:本文中的NetConnection::Connect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。