snapcast/blocking_tcp_echo_server.cpp
(no author) 342bc79770 x
git-svn-id: svn://elaine/murooma/trunk@143 d8a302eb-03bc-478d-80e4-98257eca68ef
2014-08-06 06:21:09 +00:00

112 lines
2.8 KiB
C++

//
// blocking_tcp_echo_server.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2013 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 <iostream>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <chrono>
#include <ctime> // localtime
#include <sstream> // stringstream
#include <iomanip>
using boost::asio::ip::tcp;
const int max_length = 1024;
typedef boost::shared_ptr<tcp::socket> socket_ptr;
using namespace std;
using namespace std::chrono;
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();
}
void session(socket_ptr sock)
{
try
{
for (;;)
{
/* char data[max_length];
boost::system::error_code error;
size_t length = sock->read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.
*/
string response(return_current_time_and_date() + "\tHallo\n");
boost::system::error_code error;
cout << response << "\n";
boost::asio::write(*sock, boost::asio::buffer(response), boost::asio::transfer_all(), error);
if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.
sleep(1);
//std::cout << return_current_time_and_date() << "\n";
}
}
catch (std::exception& e)
{
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
void server(boost::asio::io_service& io_service, unsigned short port)
{
tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
for (;;)
{
socket_ptr sock(new tcp::socket(io_service));
a.accept(*sock);
cout << "New connection: " << sock->remote_endpoint().address().to_string() << "\n";
boost::thread t(boost::bind(session, sock));
}
}
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: blocking_tcp_echo_server <port>\n";
return 1;
}
boost::asio::io_service io_service;
using namespace std; // For atoi.
server(io_service, atoi(argv[1]));
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}