Skip to content

Instantly share code, notes, and snippets.

@j2doll
Last active August 12, 2017 07:32
Show Gist options
  • Save j2doll/9070625 to your computer and use it in GitHub Desktop.
Save j2doll/9070625 to your computer and use it in GitHub Desktop.
AsioTCPServer
// AsioTCPServer.cpp
//
// : Defines the entry point for the console application.
//
#if (_MSC_VER >= 1300) // VC++ 7.0(2003) or over
#include "stdafx.h"
#include <tchar.h>
#include <errno.h>
#else
#endif
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
// boost 1.55.0
#include <boost/date_time/date.hpp>
#include <boost/date_time/time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>
#include <boost/asio.hpp>
const int max_length = 1024;
typedef boost::shared_ptr<boost::asio::ip::tcp::socket> tcp_socket_ptr;
std::string current_time_string()
{
using namespace boost::posix_time;
using namespace boost::gregorian;
ptime now = second_clock::local_time(); //get the current time from the clock -- one second resolution
std::string ret = std::string("[") + to_simple_string(now) +std::string("]");
return ret;
}
void session(tcp_socket_ptr sock)
{
/*log*/std::cout << current_time_string() << " thread is created.\n";
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)
{
/*log*/ std::cout << current_time_string() << "...Connection closed cleanly by peer.\n" ;
break; // Connection closed cleanly by peer. (socket is gracefully closed.)
}
else if (error)
{
/*log*/ std::cout << current_time_string() << error.message() ;
throw boost::system::system_error(error); // some other error is occured.
}
// boost::asio::buffer buf(data, length);
// boost::asio::buffer abc;
// echo message
// boost::asio::write(*sock, boost::asio::buffer(data, length));
// sock->write_some( boost::asio::buffer(data, length) );
/*log*/ std::cout << current_time_string() << " " << length << " byte is received. \n" ;
}
}
catch (std::exception& e)
{
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
void server(boost::asio::io_service& io_service, unsigned short port)
{
using namespace boost::asio::ip;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), port));
for (;;)
{
tcp_socket_ptr sock(new tcp::socket(io_service)); // delete destructor is not needed.
/*log*/ std::cout << " sock native :" << sock->native() << "\n";
/*log*/ std::cout << current_time_string() << " acceptor socket is created.\n";
acceptor.accept(*sock);
/*log*/ std::cout << current_time_string() << " acceptor socket is accepted.\n";
boost::thread thread(boost::bind(session, sock));
}
}
#if (_MSC_VER >= 1300) // VC++ 7.0(2003) or over
int _tmain(int argc, _TCHAR* argv[])
#else
int main(int argc, char* argv[])
#endif
{
try
{
if (argc != 2)
{
std::cerr << "Usage: blocking_tcp_echo_server <port>\n";
return 1;
}
boost::asio::io_service io_service;
unsigned short tcpport;
#if (_MSC_VER >= 1300) // VC++ 7.0(2003) or over
tcpport = _tstoi(argv[1]);
#else
{
using namespace std; // For atoi.
tcpport = atoi(argv[1]);
}
#endif
server( io_service, tcpport );
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment