diff --git a/blocking_tcp_echo_server.cpp b/blocking_tcp_echo_server.cpp index 8ff59215..111794c3 100644 --- a/blocking_tcp_echo_server.cpp +++ b/blocking_tcp_echo_server.cpp @@ -14,12 +14,32 @@ #include #include #include +#include +#include // localtime +#include // stringstream +#include using boost::asio::ip::tcp; const int max_length = 1024; typedef boost::shared_ptr 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) { @@ -27,7 +47,7 @@ void session(socket_ptr sock) { for (;;) { - char data[max_length]; +/* char data[max_length]; boost::system::error_code error; size_t length = sock->read_some(boost::asio::buffer(data), error); @@ -35,8 +55,17 @@ void session(socket_ptr sock) break; // Connection closed cleanly by peer. else if (error) throw boost::system::system_error(error); // Some other error. - - boost::asio::write(*sock, boost::asio::buffer(data, length)); +*/ + 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) @@ -52,6 +81,7 @@ void server(boost::asio::io_service& io_service, unsigned short port) { 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)); } } diff --git a/client.cpp b/client.cpp index ec0c00d0..69fe7e3b 100644 --- a/client.cpp +++ b/client.cpp @@ -133,7 +133,7 @@ int initAudio() outputParameters.sampleFormat = paInt16; /* 32 bit floating point output */ outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency; outputParameters.hostApiSpecificStreamInfo = NULL; - +std::cerr << "HighLatency: " << outputParameters.suggestedLatency << "\t LowLatency: " << Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency << "\n"; err = Pa_OpenStream( &paStream, NULL, /* no input */