示例#1
0
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);
}
示例#2
0
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);
}
示例#3
0
int main(int argc, char** argv)
{
	int conn_timeout = 10, rw_timeout = 0;
	
	// As of now this is to choose the server to connect to
	if(argc != 3){
		printf("Usage: server <port> [<ip>]\n");
		exit(1);
	}

	TCPStream* stream = NULL;
    TCPAcceptor* acceptor = NULL;
       
    acceptor = new TCPAcceptor(atoi(argv[1]), argv[2]);

    // This is hard coded to the server thats running the redis server
	acl::string addr("135.44.219.148:6380");
	
	acl::acl_cpp_init();
	acl::log::stdout_open(true);
	acl::redis_client client(addr.c_str(), conn_timeout, rw_timeout);
	acl::redis_pubsub redis(&client);
	// Hard coded channel 
	acl::string channel = "channel", msg;

	redis.clear();

	// Have this program run continuously, and if an ip address is picked up, publish it to the redis server
    if (acceptor->start() == 0) {
        while (1) {   	
            stream = acceptor->accept();
            if (stream != NULL) {
		    	printf("IP %s\n", stream->getPeerIP().c_str());	
		    	msg.format("%s", stream->getPeerIP().c_str());
		    	redis.publish(channel, msg, msg.length());
			}
		}
	}


#ifdef WIN32
	printf("enter any key to exit\r\n");
	getchar();
#endif
	return 0;
}
示例#4
0
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;
}
示例#5
0
void peerListener::listen(){
    int timer = 0;
    messageReader* mr = new messageReader(peer, "");
    TCPAcceptor* acceptor = new TCPAcceptor(peer->port, peer->address.c_str(), false, false, true);
    if (acceptor->start() == 0) {
        while (timer < mainTimer) {
            TCPStream* stream = acceptor->accept();
            if (stream != NULL) {
                mr->readMsg(stream);
                PeerHandler* ph = new PeerHandler(peer, stream, peer->tmpPeerName);
                ph->start();
                peer->peerHandlers[peer->tmpPeerName]= ph;
            }
            sleep(1);
            timer += 1;
        }
    }
    delete mr;
    delete acceptor;
}
//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;
            }

        }
    }
}