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


C++ TcpSocket::create方法代码示例

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


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

示例1: main

int main(void) {
    TcpSocket servSocket;
    servSocket.create();
    servSocket.connect("127.0.0.1", 9999);
    Message msg;
    msg.add("type", "login");
    msg.add("user", "root");
    msg.add("pwd", "root");

    servSocket << msg;

    servSocket.close();
    return 0;
}
开发者ID:CyberLuc,项目名称:Simple_Instant_Messenger_Server,代码行数:14,代码来源:test_client.cpp

示例2: accept

Socket::Status TcpListener::accept(TcpSocket& socket)
{
    // Make sure that we're listening
    if (getHandle() == priv::SocketImpl::invalidSocket())
    {
        err() << "Failed to accept a new connection, the socket is not listening" << std::endl;
        return Error;
    }

    // Accept a new connection
    sockaddr_in address;
    priv::SocketImpl::AddrLength length = sizeof(address);
    SocketHandle remote = ::accept(getHandle(), reinterpret_cast<sockaddr*>(&address), &length);

    // Check for errors
    if (remote == priv::SocketImpl::invalidSocket())
        return priv::SocketImpl::getErrorStatus();

    // Initialize the new connected socket
    socket.close();
    socket.create(remote);

    return Done;
}
开发者ID:AdamFlores,项目名称:SFML,代码行数:24,代码来源:TcpListener.cpp

示例3: main

int main(int argc, char* argv[]) {

	startRobogen();

	interrupted = false;

	if (argc < 2) {
		std::cerr << "Please, provide server port." << std::endl;
		exitRobogen(EXIT_FAILURE);
	} 

	// Parameters: <PORT>
	int port = std::atoi(argv[1]);
	if (!port) {
		std::cerr << "The first argument must be a server port." << std::endl;
		exitRobogen(EXIT_FAILURE);
	}

	bool visualize = false;	
	bool startPaused = false;
	for (int currentArg=2; currentArg<argc; currentArg++) {
		if (std::string(argv[currentArg]).compare("--visualization") == 0) {
			visualize = true;
		} else if (std::string(argv[currentArg]).compare("--pause") == 0) {
			startPaused = true;
		}
	}

	if (startPaused && !visualize) {
		std::cerr << "Cannot start paused without visualization enabled." <<
				std::endl;
		exitRobogen(EXIT_FAILURE);
	}


	TcpSocket socket;
	bool rc = socket.create(port);
	if (!rc) {
		std::cerr << "Cannot listen for incoming connections on port " << port
				<< std::endl;
		exitRobogen(EXIT_FAILURE);
	}


	boost::random::mt19937 rng;
	rng.seed(port);

#ifdef QT5_ENABLED
	QCoreApplication a(argc, argv);
#endif


	while (!interrupted) {

		// Wait for client to connect
		std::cout << "Waiting for clients..." << std::endl;

		rc = socket.accept();

		if (rc) {

			std::cout << "Client connected..." << std::endl;

			while (true) {

				try {

					// ---------------------------------------
					// Decode solution
					// ---------------------------------------

					ProtobufPacket<robogenMessage::EvaluationRequest> packet;

					// 1) Read packet header
					std::vector<unsigned char> headerBuffer;
					socket.read(headerBuffer,
							ProtobufPacket<robogenMessage::EvaluationRequest>::HEADER_SIZE);
					unsigned int packetSize = packet.decodeHeader(headerBuffer);

					// 2) Read packet size
					std::vector<unsigned char> payloadBuffer;
					socket.read(payloadBuffer, packetSize);
					packet.decodePayload(payloadBuffer);

					// ---------------------------------------
					//  Decode configuration file
					// ---------------------------------------

					boost::shared_ptr<RobogenConfig> configuration =
							ConfigurationReader::parseRobogenMessage(
									packet.getMessage()->configuration());
					if (configuration == NULL) {
						std::cerr
								<< "Problems parsing the configuration file. Quit."
								<< std::endl;
						exitRobogen(EXIT_FAILURE);
					}

					// ---------------------------------------
					// Setup environment
//.........这里部分代码省略.........
开发者ID:aliceconcordel,项目名称:robogen,代码行数:101,代码来源:RobogenServer.cpp


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