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


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

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


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

示例1: dabInputSlipThread

void* dabInputSlipThread(void* args)
{
    dabInputSlipData* data = (dabInputSlipData*)args;
    TcpSocket* client;

    while ((client = data->server->accept()) != NULL) {
        int size = 0;
        etiLog.log(info, "SLIP server got a new client.\n");

#ifdef _WIN32
        WaitForSingleObject(data->semWrite, INFINITE);
                WaitForSingleObject(data->semQueue, INFINITE);
#else
        sem_wait(&data->semWrite);
        sem_wait(&data->semQueue);
#endif
        UdpPacket* packet = data->packets[data->nbPackets];
#ifdef _WIN32
        ReleaseSemaphore(data->semQueue, 1, NULL);
#else
        sem_post(&data->semQueue);
#endif

        while ((size = client->read(packet->getData(), packet->getSize()))
                    > 0) {
            packet->setLength(size);
#ifdef _WIN32
            WaitForSingleObject(data->semQueue, INFINITE);
#else
            sem_wait(&data->semQueue);
#endif
            data->nbPackets++;
#ifdef _WIN32
            ReleaseSemaphore(data->semQueue, 1, NULL);
#else
            sem_post(&data->semQueue);
#endif
            
#ifdef _WIN32
            WaitForSingleObject(data->semWrite, INFINITE);
            WaitForSingleObject(data->semQueue, INFINITE);
#else
            sem_wait(&data->semWrite);
            sem_wait(&data->semQueue);
#endif
            packet = data->packets[data->nbPackets];
#ifdef _WIN32
            ReleaseSemaphore(data->semQueue, 1, NULL);
#else
            sem_post(&data->semQueue);
#endif
        }
        etiLog.log(info, "SLIP server client deconnected.\n");
        client->close();
    }
    etiLog.log(error, "SLIP thread can't accept new client (%s)\n",
            inetErrDesc, inetErrMsg);

    return NULL;
}
开发者ID:DarkDare,项目名称:ODR-DabMux,代码行数:60,代码来源:dabInputSlip.cpp

示例2: sendReceiveObjectTest

bool sendReceiveObjectTest(){
    cout << "sendReceiveObjectTest() ...\n";

    Object tosend('c', 34, true, "cinco");

    pid_t child;
    // client code
    if ((child=fork())==0){
        try {
            TcpSocket toServer;
            cout << "  Forked child waiting a bit\n";
            sleep(1);
            cout << "  Forked child connecting to server\n";
            toServer.connect("127.0.0.1",LOCALPORT);
            cout << "  Forked child connected: " << toServer << endl;
            cout << "  Forked child sending to server\n";
            toServer.write(reinterpret_cast<char *>(&tosend), sizeof(Object));
            cout << "  Forked child closing connection to server\n";
            toServer.close();
            cout << "  Forked child exiting\n";
            exit(0);
        } catch (std::exception& e) {
            cout << "  Forked child exception: " << e.what() << endl;
            exit(-1);
        }
        return false; // for clarity
    }
    // server code
    TcpSocket* connectedSocket;
    try {
        TcpSocket serverSocket;
        connectedSocket = bindListenAccept(LOCALPORT, serverSocket);
        std::string clientmessage;
        cout << "  Reading one object from client\n";

        // Read the object
        Object received;
        int len = connectedSocket->read(reinterpret_cast<char *>(&received), sizeof(Object));
        cout << "  Read: " << len << " bytes from " << *connectedSocket << endl;
        if (!(tosend.compare(received) == 0))
            throw std::runtime_error("objects dont match");
        cout << "  Server read " << received << endl;
        
        checkChild(child);
        serverSocket.close();
        connectedSocket->close();
        cout << "Done!\n";
        delete connectedSocket;
        return true;
    } catch (std::exception& e) {
        cout << "  Server exception: " << e.what() << endl;
        cout << "Failed!\n";
        checkChild(child, true);
        delete connectedSocket;
        return false;
    }
}
开发者ID:joaotavora,项目名称:minns,代码行数:57,代码来源:TcpSocketUnit.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::read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。