Click here to Skip to main content
15,885,366 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionCTreeCtrl Problem Pin
Dhiraj kumar Saini20-Oct-08 3:35
Dhiraj kumar Saini20-Oct-08 3:35 
AnswerRe: CTreeCtrl Problem Pin
SandipG 20-Oct-08 3:44
SandipG 20-Oct-08 3:44 
QuestionAsynchronous echo server Pin
gamzi20-Oct-08 3:15
gamzi20-Oct-08 3:15 
AnswerRe: Asynchronous echo server Pin
SandipG 20-Oct-08 3:34
SandipG 20-Oct-08 3:34 
QuestionRe: Asynchronous echo server [modified] Pin
gamzi20-Oct-08 4:06
gamzi20-Oct-08 4:06 
NewsRe: Asynchronous echo server Pin
Rajesh R Subramanian20-Oct-08 4:55
professionalRajesh R Subramanian20-Oct-08 4:55 
AnswerRe: Asynchronous echo server Pin
Moak20-Oct-08 7:10
Moak20-Oct-08 7:10 
QuestionRe: Asynchronous echo server Pin
gamzi21-Oct-08 23:44
gamzi21-Oct-08 23:44 
Hi! I found this code under the boost c++ libraries. I also read that it is platform independent. Here's the code:

#include < cstdlib>
#include < iostream>
#include < boost/bind.hpp>
#include < boost/asio.hpp>

using boost::asio::ip::tcp;

class session
{
public:
session(boost::asio::io_service& io_service)
: socket_(io_service)
{
}

tcp::socket& socket()
{
return socket_;
}

void start()
{
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}

void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
if (!error)
{
boost::asio::async_write(socket_,
boost::asio::buffer(data_, bytes_transferred),
boost::bind(&session::handle_write, this,
boost::asio::placeholders::error));
}
else
{
delete this;
}
}

void handle_write(const boost::system::error_code& error)
{
if (!error)
{
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
delete this;
}
}

private:
tcp::socket socket_;
enum { max_length = 1024 };
char data_[max_length];
};

class server
{
public:
server(boost::asio::io_service& io_service, short port)
: io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
{
session* new_session = new session(io_service_);
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
}

void handle_accept(session* new_session,
const boost::system::error_code& error)
{
if (!error)
{
new_session->start();
new_session = new session(io_service_);
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
}
else
{
delete new_session;
}
}

private:
boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
};

int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: async_tcp_echo_server <port>\n";
return 1;
}

boost::asio::io_service io_service;

using namespace std; // For atoi.
server s(io_service, atoi(argv[1]));

io_service.run();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}

return 0;
}

I'm having a hard time understanding this code. Can someone familiar with this code help me out in understanding how the code works? Really sorry because I'm not really familiar with network programming. Thank you.
QuestionRe: Asynchronous echo server Pin
gamzi22-Oct-08 16:31
gamzi22-Oct-08 16:31 
QuestionMS Flex Grid 6.0 OCX [modified] Pin
dehseth20-Oct-08 2:42
dehseth20-Oct-08 2:42 
QuestionHi all! Pin
Dennis L20-Oct-08 2:37
Dennis L20-Oct-08 2:37 
AnswerRe: Hi all! Pin
SandipG 20-Oct-08 3:07
SandipG 20-Oct-08 3:07 
Questionwinhttp authentication issue Pin
George_George20-Oct-08 2:26
George_George20-Oct-08 2:26 
QuestionLoading Doc/View from Dialog Pin
Mark Gilson20-Oct-08 2:26
Mark Gilson20-Oct-08 2:26 
AnswerRe: Loading Doc/View from Dialog Pin
David Crow20-Oct-08 2:45
David Crow20-Oct-08 2:45 
GeneralRe: Loading Doc/View from Dialog Pin
Mark Gilson20-Oct-08 2:51
Mark Gilson20-Oct-08 2:51 
GeneralRe: Loading Doc/View from Dialog Pin
David Crow20-Oct-08 2:58
David Crow20-Oct-08 2:58 
GeneralRe: Loading Doc/View from Dialog Pin
Mark Gilson20-Oct-08 3:02
Mark Gilson20-Oct-08 3:02 
QuestionRe: Loading Doc/View from Dialog Pin
David Crow20-Oct-08 3:17
David Crow20-Oct-08 3:17 
AnswerRe: Loading Doc/View from Dialog Pin
Mark Gilson20-Oct-08 3:29
Mark Gilson20-Oct-08 3:29 
QuestionRe: Loading Doc/View from Dialog Pin
David Crow20-Oct-08 3:32
David Crow20-Oct-08 3:32 
AnswerRe: Loading Doc/View from Dialog Pin
Mark Gilson20-Oct-08 4:19
Mark Gilson20-Oct-08 4:19 
GeneralRe: Loading Doc/View from Dialog Pin
David Crow20-Oct-08 4:27
David Crow20-Oct-08 4:27 
GeneralRe: Loading Doc/View from Dialog Pin
Mark Gilson20-Oct-08 5:58
Mark Gilson20-Oct-08 5:58 
QuestionHow can increase a date by one day? Pin
Le@rner20-Oct-08 2:25
Le@rner20-Oct-08 2:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.