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


C++ TCPStream::send方法代码示例

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


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

示例1: main

int main(int argc, char** argv)
{
    if (argc != 3) {
        printf("usage: %s <port> <ip>\n", argv[0]);
        exit(1);
    }

    int len;
    string message;
    char line[256];
    TCPConnector* connector = new TCPConnector();
    TCPStream* stream = connector->connect(argv[2], atoi(argv[1]));
    if (stream) {
        message = "Is there life on Mars?";
        stream->send(message.c_str(), message.size());
        printf("sent - %s\n", message.c_str());
        len = stream->receive(line, sizeof(line));
        line[len] = NULL;
        printf("received - %s\n", line);
        delete stream;
    }

    stream = connector->connect(argv[2], atoi(argv[1]));
    if (stream) {
        message = "Why is there air?";
        stream->send(message.c_str(), message.size());
        printf("sent - %s\n", message.c_str());
        len = stream->receive(line, sizeof(line));
        line[len] = NULL;
        printf("received - %s\n", line);
        delete stream;
    }
    exit(0);
}
开发者ID:vichargrave,项目名称:mtserver,代码行数:34,代码来源:client.cpp

示例2: main

int main(int argc, char** argv) {
    if (argc < 2 || argc > 4) {
        printf("usage: server <port> [<ip>]\n");
        exit(1);
    }
    TCPStream* stream = NULL;
    TCPAcceptor* acceptor = NULL;

    if (argc == 3) {
        acceptor = new TCPAcceptor(atoi(argv[1]), argv[2]);
    } else {
        acceptor = new TCPAcceptor(atoi(argv[1]));
    }
    printf("server started successfully\n");
    if (acceptor->start() == 0) {
        while(1) {
            stream = acceptor->accept();
            if (stream != NULL) {
                size_t len;
                char buffer[256];
                while((len = stream->receive(buffer, sizeof(buffer))) > 0) {
                    buffer[len] = '\0';
                    printf("received: %s\n", buffer);
                    stream->send(buffer, len);
                }
                delete stream;
            }
        }
    }
    perror("Could not start the server");
    exit(-1);
}
开发者ID:anhmv,项目名称:network-pattern,代码行数:32,代码来源:echo_server.cpp

示例3: main

int main(int argc, char** argv)
{
    if (argc < 2 || argc > 4) {
        printf("usage: server <port> [<ip>]\n");
        exit(1);
    }

    TCPStream* stream = NULL;
    TCPAcceptor* acceptor = NULL;
    if (argc == 3) {
        acceptor = new TCPAcceptor(atoi(argv[1]), argv[2]);
    }
    else {
        acceptor = new TCPAcceptor(atoi(argv[1]));
    }
    if (acceptor->start() == 0) {
        while (1) {
            stream = acceptor->accept();
            if (stream != NULL) {
                ssize_t len;
                char line[256];
                while ((len = stream->receive(line, sizeof(line))) > 0) {
                    line[len] = 0;
                    printf("received - %s\n", line);
                    stream->send(line, len);
                }
                delete stream;
            }
        }
    }
    exit(0);
}
开发者ID:OpenChaiSpark,项目名称:OCspark,代码行数:32,代码来源:server.cpp

示例4: main

int main(int argc, char** argv)
{
    if (argc != 5) {
        printf("usage: %s <pause port> <time out port> <ip> <time out>\n", argv[0]);
        exit(1);
    }

    int result, timeout = atoi(argv[4]);
    string message;
    char line[256];

    printf("Connecting to the paused server...\n");
    TCPConnector* connector = new TCPConnector();
    TCPStream* stream = connector->connect(argv[3], atoi(argv[1]), timeout);
    if (stream == NULL) {
        printf("Timed out connecting to the server\n");
    }

    printf("Connecting to the time out server...\n");
    stream = connector->connect(argv[3], atoi(argv[2]), timeout);
    if (stream) {
        message = "Is there life on Mars?";
        stream->send(message.c_str(), message.size());
        printf("sent - %s\n", message.c_str());
        result = stream->receive(line, sizeof(line), timeout);
        if (result == TCPStream::connectionTimedOut) {
            printf("Timed out waiting for a server response\n");
        }
        delete stream;
    }

    exit(0);
}
开发者ID:HarmMunk,项目名称:tcpsockets,代码行数:33,代码来源:client_timeout.cpp

示例5: send

ssize_t Package::send(TCPStream& stream) const {
	char buff[getPackageSize()];
	memset(buff, 0, sizeof(buff));

	writeData(buff);

	ssize_t sent = stream.send(buff, getPackageSize());

	return sent;
}
开发者ID:alvin-nt,项目名称:Simple-TCP-Chat,代码行数:10,代码来源:Package.cpp

示例6: main

int main(int argc, char *argv[]) {
	// TCP server
	TCPStream *stream = NULL;
	TCPAcceptor *acceptor = NULL;

	if (argc != 1) {
		usage(argv);
		return -1;
	}

	initCommandMap();

	acceptor = new TCPAcceptor(TCP_PORT);
	if (acceptor->start() != 0) {
		cerr << LOG_PREFIX "Could not start TCPAcceptor" << endl;
		return -1;
	}

	cout << LOG_PREFIX "Initialized TLM Server" << endl;
	while(1) {
		cout << LOG_PREFIX "Waiting for connection." << endl;
		stream = acceptor->accept();
		cout << LOG_PREFIX "Connection Accepted, sending telemetry. " << endl;
			if (stream != NULL) {
				ssize_t len;
				char line[256];
				string retval;
				const char *txstr;
				while ((len = stream->receive(line, sizeof(line))) > 0) {
					line[len] = 0;
					retval = parse(line);
					txstr = retval.c_str();
					stream->send(txstr, retval.length());
				}
			delete stream;
			cout << LOG_PREFIX "Connection closed." << endl;
		}
	}
	delete acceptor;

	return 0;
}
开发者ID:thasti,项目名称:arca-linux,代码行数:42,代码来源:tlm_srv.cpp

示例7: dowrite

ssize_t mbed_socket::dowrite(const void* buf, size_t count) {
  TCPStream* stream = get_stream();
  socket_error_t err;

  if ((sock_flag_ & MBED_SOCKET_FLAG_CONNECTED) == 0) {
    TDLOG(".. dowrite, socket not connected");
    set_errno(ENOTCONN);
    return -1;
  }

  sock_flag_ &= ~MBED_SOCKET_FLAG_WRITABLE;

  err = stream->send(buf, count);
  if (stream->error_check(err)) {
    TDLOG(".. socket send error: %s (%d)", socket_strerror(err), err);
    set_errno(EINVAL);
    // TODO: need to check this... how?
    sock_flag_ |= MBED_SOCKET_FLAG_WRITABLE;
    return -1;
  }

  return count;
}
开发者ID:esevan,项目名称:libtuv,代码行数:23,代码来源:tuv_mbed_port_socket.cpp

示例8: run

    void* run()
    {
        // Remove 1 item at a time and process it. Blocks if no items are
        // available to process.
        for (int i = 0;; i++)
        {
            //qDebug("thread %lu, loop %d - waiting for item...", (long unsigned int)self(), i);
            WorkItem* item = m_queue.remove();
            //qDebug("thread %lu, loop %d - got one item", (long unsigned int)self(), i);
            TCPStream* stream = item->getStream();

            // Echo messages back the client until the connection is
            // closed
            char input[256];
            for (int i = 0; i < 255; i++)
            {
                input[i] = '\0';
            }

            string output;
            int len;

            while ((len = stream->receive(input, sizeof(input)-1)) > 0 )
            {
                output = "OK";
                stream->send(output.c_str(), (sizeof(output.c_str())-1));
                //qDebug("thread %lu, echoed '%s' back to the client", (long unsigned int)self(), input);
                std::string cmd(input);
                executeCommand(cmd);
            }
            delete item;

        }

        // Should never get here
        return NULL;
    }
开发者ID:wercool,项目名称:valter,代码行数:37,代码来源:platformlocationp1.tcphandler.cpp

示例9: run

//this function received the command from ios, and push them to the queue.
int SocketServerManager::run(){
    TCPStream* stream = NULL;
    TCPAcceptor* acceptor = NULL;
    CommandQueue cmdQueue;
    Command rcvCmd;
    acceptor = new TCPAcceptor(8081);
    if (acceptor->start() == 0) {
        while (1) {
            stream = acceptor->accept();
            if (stream != NULL) {
                ssize_t len;
                char line[256];
                while ((len = stream->receive(line, sizeof(line))) > 0) {
                    line[len] = 0;
                    string cmd = string(line);
                    std::cout << "running Command: " << cmd << std::endl;
                    if( cmd == "forward"){
                        rcvCmd.type = pi2::FORWARD;
                        rcvCmd.m1speed = 15;
                        rcvCmd.m2speed = 15;
                        cmdQueue.putCommand(rcvCmd);
                    }
                    else if( cmd == "back"){
                        rcvCmd.type = pi2::BACK;
                        rcvCmd.m1speed = -15;
                        rcvCmd.m2speed = -15;
                        cmdQueue.putCommand(rcvCmd);
                    }
                    else if( cmd == "left"){
                        rcvCmd.type = pi2::LEFT;
                        rcvCmd.m1speed = 0;
                        rcvCmd.m2speed = 8;
                        cmdQueue.putCommand(rcvCmd);
                    }
                    else if( cmd == "right"){
                        rcvCmd.type = pi2::RIGHT;
                        rcvCmd.m1speed = 8;
                        rcvCmd.m2speed = 0;
                        cmdQueue.putCommand(rcvCmd);
                    }
                    else if( cmd == "stop"){
                        rcvCmd.type = pi2::STOP;
                        rcvCmd.m1speed = 0;
                        rcvCmd.m2speed = 0;
                        cmdQueue.putCommand(rcvCmd);
                    }
                    else if( cmd == "conndone"){
                        rcvCmd.type = pi2::CONNDONE;
                        rcvCmd.m1speed = 0;
                        rcvCmd.m2speed = 0;
                        cmdQueue.putCommand(rcvCmd);
                    }
                    else{
                        rcvCmd.type = pi2::NOCOMMAND;
                        rcvCmd.m1speed = 0;
                        rcvCmd.m2speed = 0;
                        cmdQueue.putCommand(rcvCmd);
                    }
                    stream->send(line, len);
                }
                delete stream;
            }

        }
    }
}
开发者ID:karagara,项目名称:pi-squared,代码行数:67,代码来源:SocketServerManager.cpp

示例10: run

    void* run()
    {
        // Remove 1 item at a time and process it. Blocks if no items are
        // available to process.
        for (int i = 0;; i++)
        {
            //qDebug("thread %lu, loop %d - waiting for item...", (long unsigned int)self(), i);
            WorkItem* item = m_queue.remove();
            //qDebug("thread %lu, loop %d - got one item", (long unsigned int)self(), i);
            TCPStream* stream = item->getStream();

            // Echo messages back the client until the connection is
            // closed
            char input[10256];
            for (int i = 0; i < 10255; i++)
            {
                input[i] = '\0';
            }

            string output;
            int len;

            while ((len = stream->receive(input, sizeof(input)-1)) > 0 )
            {
                output = "OK";
                stream->send(output.c_str(), (sizeof(output.c_str())-1));
                //qDebug("thread %lu, echoed '%s' back to the client", (long unsigned int)self(), input);

                //taskPrefix - Remote Task Manager Message, HOP task
                char taskPrefix[5];
                strncpy(taskPrefix, input, 4);
                taskPrefix[4] = '\0';

                if (strcmp(taskPrefix, "RTMM") == 0)
                {
                    std::string rtmm(input);
                    qDebug("%s", rtmm.c_str());
                    TaskManager::getInstance()->addUpdateRTMM(rtmm);
                    continue;
                }
                else if (strcmp(taskPrefix, "HOP_") == 0)
                {
                    std::string hoptaskmsg(input);
                    qDebug("%s", hoptaskmsg.c_str());

                    TaskManager::getInstance()->sendScript(hoptaskmsg.substr(4));

                    continue;
                }
                else
                {
                    std::string script(input);

                    if (script.length() > 0)
                    {
                        executeScript(script);
                    }
                }
            }

            delete item;
        }

        // Should never get here
        return NULL;
    }
开发者ID:wercool,项目名称:valter,代码行数:66,代码来源:taskmanager.tcphandler.cpp


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