本文整理汇总了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);
}
示例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);
}
示例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);
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
}
}
}
示例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;
}