Exemplo n.º 1
0
//for solaris compile:
////g++ localmain.cpp localTCPConnect.cpp -o local.out -lsocket -lnsl -lresolv
int main(int argc, char** argv)
{
    localTCPConnect local;
    std::string message;
    std::string serverString(SERVER);
    if(argc == 1){
	std::cout<<"\n Enter the message you would like to send: ";
	std::getline(std::cin,message);
    }else{
        for(int i = 1; i < argc; i++){
	    message.append(argv[i]);
	    if(i > 3){
		message.append(" ");
	    }
        }
    }
    local.sendMessage(serverString,message);
    return 0;
}
Exemplo n.º 2
0
int main(int argc, char** argv)
{
    LBCHECK(co::init(argc, argv));

    co::ConnectionDescriptionPtr description = new co::ConnectionDescription;
    description->type = co::CONNECTIONTYPE_TCPIP;
    description->port = 4242;

    bool isClient = true;
    bool useThreads = false;
    size_t packetSize = 1048576;
    size_t nPackets = 0xffffffffu;
    uint32_t waitTime = 0;

    try // command line parsing
    {
        po::options_description options(
            "netperf - Collage network benchmark tool " +
            co::Version::getString());

        std::string clientString("");
        std::string serverString("");
        bool showHelp(false);

        options.add_options()("help,h",
                              po::bool_switch(&showHelp)->default_value(false),
                              "show help message")(
            "client,c", po::value<std::string>(&clientString),
            "run as client, format IP[:port][:protocol]")(
            "server,s", po::value<std::string>(&serverString),
            "run as server, format IP[:port][:protocol]")(
            "threaded,t", po::bool_switch(&useThreads)->default_value(false),
            "Run each receive in a separate thread (server only)")(
            "packetSize,p", po::value<std::size_t>(&packetSize),
            "packet size")("numPackets,n", po::value<std::size_t>(&nPackets),
                           "number of packets to send, unsigned int")(
            "wait,w", po::value<uint32_t>(&waitTime),
            "wait time (ms) between sends (client only)")(
            "delay,d", po::value<uint32_t>(&_delay),
            "wait time (ms) between receives (server only");

        // parse program options
        po::variables_map variableMap;
        po::store(po::command_line_parser(argc, argv)
                      .options(options)
                      .allow_unregistered()
                      .run(),
                  variableMap);
        po::notify(variableMap);

        // evaluate parsed arguments
        if (showHelp)
        {
            std::cout << options << std::endl;
            co::exit();
            return EXIT_SUCCESS;
        }

        if (variableMap.count("client") == 1)
            description->fromString(clientString);
        else if (variableMap.count("server") == 1)
        {
            isClient = false;
            description->fromString(serverString);
        }
    }
    catch (std::exception& exception)
    {
        std::cerr << "Command line parse error: " << exception.what()
                  << std::endl;
        co::exit();
        return EXIT_FAILURE;
    }

    // run
    co::ConnectionPtr connection = co::Connection::create(description);
    if (!connection)
    {
        LBWARN << "Unsupported connection: " << description << std::endl;
        co::exit();
        return EXIT_FAILURE;
    }

    Selector* selector = 0;
    if (isClient)
    {
        if (description->type == co::CONNECTIONTYPE_RSP)
        {
            selector = new Selector(connection, packetSize, useThreads);
            selector->start();
        }
        else if (!connection->connect())
            ::exit(EXIT_FAILURE);

        lunchbox::Buffer<uint8_t> buffer;
        buffer.resize(packetSize);
        for (size_t i = 0; i < packetSize; ++i)
            buffer[i] = static_cast<uint8_t>(i);

        const float mBytesSec = buffer.getSize() / 1024.0f / 1024.0f * 1000.0f;
        lunchbox::Clock clock;
        size_t lastOutput = nPackets;

        clock.reset();
        while (nPackets--)
        {
            buffer[SEQUENCE] = uint8_t(nPackets);
            LBCHECK(connection->send(buffer.getData(), buffer.getSize()));
            const float time = clock.getTimef();
            if (time > 1000.f)
            {
                const lunchbox::ScopedWrite mutex(_mutexPrint);
                const size_t nSamples = lastOutput - nPackets;
                std::cerr << "Send perf: " << mBytesSec / time * nSamples
                          << "MB/s (" << nSamples / time * 1000.f << "pps)"
                          << std::endl;

                lastOutput = nPackets;
                clock.reset();
            }
            if (waitTime > 0)
                lunchbox::sleep(waitTime);
        }
        const float time = clock.getTimef();
        const size_t nSamples = lastOutput - nPackets;
        if (nSamples != 0)
        {
            const lunchbox::ScopedWrite mutex(_mutexPrint);
            std::cerr << "Send perf: " << mBytesSec / time * nSamples
                      << "MB/s (" << nSamples / time * 1000.f << "pps)"
                      << std::endl;
        }
        if (selector)
        {
            connection->close();
            selector->join();
        }
    }
    else
    {
        selector = new Selector(connection, packetSize, useThreads);
        selector->start();

        LBASSERTINFO(connection->getRefCount() >= 1, connection->getRefCount());

        if (selector)
            selector->join();
    }

    delete selector;
    LBASSERTINFO(connection->getRefCount() == 1, connection->getRefCount());
    connection = 0;
    LBCHECK(co::exit());
    return EXIT_SUCCESS;
}