mirror of
https://github.com/badaix/snapcast.git
synced 2025-07-22 19:07:38 +02:00
x
git-svn-id: svn://elaine/murooma/trunk@144 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
parent
342bc79770
commit
fd654ad5b2
1 changed files with 88 additions and 0 deletions
88
blocking_tcp_echo_client.cpp
Normal file
88
blocking_tcp_echo_client.cpp
Normal file
|
@ -0,0 +1,88 @@
|
|||
//
|
||||
// blocking_tcp_echo_client.cpp
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <boost/asio.hpp>
|
||||
#include <chrono>
|
||||
#include <ctime> // localtime
|
||||
#include <sstream> // stringstream
|
||||
#include <iomanip>
|
||||
|
||||
|
||||
using boost::asio::ip::tcp;
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
|
||||
|
||||
enum { max_length = 1024 };
|
||||
|
||||
|
||||
std::string return_current_time_and_date()
|
||||
{
|
||||
auto now = system_clock::now();
|
||||
auto in_time_t = system_clock::to_time_t(now);
|
||||
system_clock::duration ms = now.time_since_epoch();
|
||||
char buff[20];
|
||||
strftime(buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&in_time_t));
|
||||
stringstream ss;
|
||||
ss << buff << "." << std::setw(3) << std::setfill('0') << ((ms / milliseconds(1)) % 1000);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (argc != 3)
|
||||
{
|
||||
std::cerr << "Usage: blocking_tcp_echo_client <host> <port>\n";
|
||||
return 1;
|
||||
}
|
||||
try
|
||||
{
|
||||
boost::asio::io_service io_service;
|
||||
tcp::resolver resolver(io_service);
|
||||
tcp::resolver::query query(tcp::v4(), argv[1], argv[2]);
|
||||
tcp::resolver::iterator iterator = resolver.resolve(query);
|
||||
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
tcp::socket s(io_service);
|
||||
s.connect(*iterator);
|
||||
boost::array<char, 128> buf;
|
||||
boost::system::error_code error;
|
||||
|
||||
while (true)
|
||||
{
|
||||
size_t len = s.read_some(boost::asio::buffer(buf), error);
|
||||
if (error == boost::asio::error::eof)
|
||||
break;
|
||||
|
||||
std::cout.write(buf.data(), len);
|
||||
}
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cerr << "Exception: " << e.what() << "\n";
|
||||
usleep(100*1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue