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


C++ TCPConnection::receive方法代码示例

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


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

示例1: read_lines

bool HTTPServer_Impl::read_lines(TCPConnection &connection, std::string &out_header_lines)
{
	out_header_lines.clear();
	while (out_header_lines.length() < 32*1024)
	{
		char buffer[1024];
		if (connection.get_read_event().wait(15000) == false)
			throw Exception("Read timed out");
		int bytes_read = connection.peek(buffer, 1024);
		if (bytes_read <= 0)
			break;
		std::string str(buffer, bytes_read);
		std::string::size_type start_pos = out_header_lines.length();
		out_header_lines += str;
		std::string::size_type pos = out_header_lines.find("\r\n\r\n");
		if (pos == std::string::npos)
		{
			connection.receive(buffer, bytes_read);
		}
		else
		{
			connection.receive(buffer, pos+4-start_pos);
			out_header_lines.resize(pos+4);
			return true;
		}
	}
	return false;
}
开发者ID:wbyang1985,项目名称:ClanLib,代码行数:28,代码来源:http_server_impl.cpp

示例2: run

		void run()
		{
			const char* testData = "Twenty-five or six to four";
			const size_t testDataLen = strlen(testData);
			char* buff = reinterpret_cast<char*>(calloc(testDataLen + 1, 1));

			const int port = 1337;

			try {
				// Create a listener and a client connection
				TCPListener server(port);
				TCPConnection client;

				// Start the server and connect to it
				server.start();
				client.connect(IPEndPoint(IP(127, 0, 0, 1), port));

				// Accept the connection
				auto serverConn = server.accept();

				// Test sending from the server to the client
				serverConn->send(testData, testDataLen);
				client.receive(buff, testDataLen);

				if (strcmp(testData, buff) != 0)
					throw TestFailedException("Data sent from server to client didn't go through properly");

				// Reset the buffer and try sending from
				// the client to the server
				memset(buff, 0, testDataLen);

				serverConn->shutDownSending();
				client.shutDownReceiving();

				client.send(testData, testDataLen);
				serverConn->receive(buff, testDataLen);

				if (strcmp(testData, buff) != 0)
					throw TestFailedException("Data sent from client to server didn't go through properly");

				// Shut down the connections
				client.disconnect();

				if (serverConn->receive(buff, testDataLen) != 0)
					throw TestFailedException("The server was not notified when the client disconnected");

				serverConn->disconnect();

				free(buff);
			}
			catch (...) {
				free(buff);
				throw;
			}
		}
开发者ID:hyassine,项目名称:MKb,代码行数:55,代码来源:TCPTest.hpp


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