From e19c3deb7de48753c8e5fd977314664eda038350 Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 23 Apr 2004 07:43:17 +0000 Subject: [PATCH] Added UDP daytime tutorial programs. --- asio/src/Makefile.am | 8 +- asio/src/Makefile.bor | 5 +- asio/src/Makefile.mgw | 5 +- asio/src/Makefile.msc | 8 +- asio/src/doc/asio.dox.in | 2 +- .../src/examples/tutorial/daytime3/server.cpp | 13 ++- .../src/examples/tutorial/daytime4/.cvsignore | 6 ++ .../src/examples/tutorial/daytime4/client.cpp | 32 +++++++ .../src/examples/tutorial/daytime5/.cvsignore | 6 ++ .../src/examples/tutorial/daytime5/server.cpp | 36 +++++++ .../src/examples/tutorial/daytime6/.cvsignore | 6 ++ .../src/examples/tutorial/daytime6/server.cpp | 58 ++++++++++++ asio/src/examples/tutorial/daytime_dox.txt | 93 +++++++++++++++++++ asio/src/examples/tutorial/index_dox.txt | 27 +++++- 14 files changed, 289 insertions(+), 16 deletions(-) create mode 100644 asio/src/examples/tutorial/daytime4/.cvsignore create mode 100644 asio/src/examples/tutorial/daytime4/client.cpp create mode 100644 asio/src/examples/tutorial/daytime5/.cvsignore create mode 100644 asio/src/examples/tutorial/daytime5/server.cpp create mode 100644 asio/src/examples/tutorial/daytime6/.cvsignore create mode 100644 asio/src/examples/tutorial/daytime6/server.cpp create mode 100644 asio/src/examples/tutorial/daytime_dox.txt diff --git a/asio/src/Makefile.am b/asio/src/Makefile.am index bc980b0e..aa6a5e4a 100644 --- a/asio/src/Makefile.am +++ b/asio/src/Makefile.am @@ -29,7 +29,10 @@ noinst_PROGRAMS = \ examples/tutorial/timer4/timer \ examples/tutorial/daytime1/client \ examples/tutorial/daytime2/server \ - examples/tutorial/daytime3/server + examples/tutorial/daytime3/server \ + examples/tutorial/daytime4/client \ + examples/tutorial/daytime5/server \ + examples/tutorial/daytime6/server TESTS = \ tests/unit/demuxer_test \ @@ -73,6 +76,9 @@ examples_tutorial_timer4_timer_SOURCES = examples/tutorial/timer4/timer.cpp examples_tutorial_daytime1_client_SOURCES = examples/tutorial/daytime1/client.cpp examples_tutorial_daytime2_server_SOURCES = examples/tutorial/daytime2/server.cpp examples_tutorial_daytime3_server_SOURCES = examples/tutorial/daytime3/server.cpp +examples_tutorial_daytime4_client_SOURCES = examples/tutorial/daytime4/client.cpp +examples_tutorial_daytime5_server_SOURCES = examples/tutorial/daytime5/server.cpp +examples_tutorial_daytime6_server_SOURCES = examples/tutorial/daytime6/server.cpp EXTRA_DIST = \ Makefile.bor \ diff --git a/asio/src/Makefile.bor b/asio/src/Makefile.bor index 6f6cedb8..0b43280c 100644 --- a/asio/src/Makefile.bor +++ b/asio/src/Makefile.bor @@ -33,7 +33,10 @@ all: \ examples\tutorial\timer4\timer.exe \ examples\tutorial\daytime1\client.exe \ examples\tutorial\daytime2\server.exe \ - examples\tutorial\daytime3\server.exe + examples\tutorial\daytime3\server.exe \ + examples\tutorial\daytime4\client.exe \ + examples\tutorial\daytime5\server.exe \ + examples\tutorial\daytime6\server.exe check: all @tests\unit\demuxer_test.exe diff --git a/asio/src/Makefile.mgw b/asio/src/Makefile.mgw index 3f6328a8..713f0ddb 100644 --- a/asio/src/Makefile.mgw +++ b/asio/src/Makefile.mgw @@ -35,7 +35,10 @@ EXAMPLE_EXES = \ examples/tutorial/timer4/timer.exe \ examples/tutorial/daytime1/client.exe \ examples/tutorial/daytime2/server.exe \ - examples/tutorial/daytime3/server.exe + examples/tutorial/daytime3/server.exe \ + examples/tutorial/daytime4/client.exe \ + examples/tutorial/daytime5/server.exe + examples/tutorial/daytime6/server.exe all: $(TEST_EXES) $(EXAMPLE_EXES) diff --git a/asio/src/Makefile.msc b/asio/src/Makefile.msc index 326f3d27..91d5c4a9 100644 --- a/asio/src/Makefile.msc +++ b/asio/src/Makefile.msc @@ -33,7 +33,10 @@ all: \ examples\tutorial\timer4\timer.exe \ examples\tutorial\daytime1\client.exe \ examples\tutorial\daytime2\server.exe \ - examples\tutorial\daytime3\server.exe + examples\tutorial\daytime3\server.exe \ + examples\tutorial\daytime4\client.exe \ + examples\tutorial\daytime5\server.exe \ + examples\tutorial\daytime6\server.exe tests\performance\client.exe: tests\performance\client.obj tests\performance\server.exe: tests\performance\server.obj @@ -62,6 +65,9 @@ examples\tutorial\timer4\timer.exe: examples\tutorial\timer4\timer.obj examples\tutorial\daytime1\client.exe: examples\tutorial\daytime1\client.obj examples\tutorial\daytime2\server.exe: examples\tutorial\daytime2\server.obj examples\tutorial\daytime3\server.exe: examples\tutorial\daytime3\server.obj +examples\tutorial\daytime4\client.exe: examples\tutorial\daytime4\client.obj +examples\tutorial\daytime5\server.exe: examples\tutorial\daytime5\server.obj +examples\tutorial\daytime6\server.exe: examples\tutorial\daytime6\server.obj check: all @tests\unit\demuxer_test.exe && \ diff --git a/asio/src/doc/asio.dox.in b/asio/src/doc/asio.dox.in index ca2a532b..ecf4e944 100644 --- a/asio/src/doc/asio.dox.in +++ b/asio/src/doc/asio.dox.in @@ -357,7 +357,7 @@ WARN_LOGFILE = # directories like "/usr/src/myproject". Separate the files or directories # with spaces. -INPUT = ../../include ../../include/asio ../../include/asio/ipv4 asio_dox.txt ../examples/tutorial/index_dox.txt ../examples/tutorial/timer_dox.txt +INPUT = ../../include ../../include/asio ../../include/asio/ipv4 asio_dox.txt ../examples/tutorial/index_dox.txt ../examples/tutorial/timer_dox.txt ../examples/tutorial/daytime_dox.txt # If the value of the INPUT tag contains directories, you can use the # FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp diff --git a/asio/src/examples/tutorial/daytime3/server.cpp b/asio/src/examples/tutorial/daytime3/server.cpp index 50565e19..79430ae6 100644 --- a/asio/src/examples/tutorial/daytime3/server.cpp +++ b/asio/src/examples/tutorial/daytime3/server.cpp @@ -1,15 +1,14 @@ #include #include -#include #include "boost/bind.hpp" #include "asio.hpp" -void handle_send(asio::stream_socket* socket, char* data, +void handle_send(asio::stream_socket* socket, char* send_buf, const asio::socket_error& /*error*/, size_t /*last_bytes_sent*/, size_t /*total_bytes_sent*/) { using namespace std; // For free. - free(data); + free(send_buf); delete socket; } @@ -20,11 +19,11 @@ void handle_accept(asio::socket_acceptor* acceptor, { using namespace std; // For time_t, time, ctime, strdup and strlen. time_t now = time(0); - char* data = strdup(ctime(&now)); - size_t length = strlen(data); + char* send_buf = strdup(ctime(&now)); + size_t send_length = strlen(send_buf); - asio::async_send_n(*socket, data, length, - boost::bind(handle_send, socket, data, asio::arg::error, + asio::async_send_n(*socket, send_buf, send_length, + boost::bind(handle_send, socket, send_buf, asio::arg::error, asio::arg::last_bytes_sent, asio::arg::total_bytes_sent)); socket = new asio::stream_socket(acceptor->demuxer()); diff --git a/asio/src/examples/tutorial/daytime4/.cvsignore b/asio/src/examples/tutorial/daytime4/.cvsignore new file mode 100644 index 00000000..007ee2bb --- /dev/null +++ b/asio/src/examples/tutorial/daytime4/.cvsignore @@ -0,0 +1,6 @@ +client +.deps +.dirstamp +*.pdb +*.ilk +*.tds diff --git a/asio/src/examples/tutorial/daytime4/client.cpp b/asio/src/examples/tutorial/daytime4/client.cpp new file mode 100644 index 00000000..ccdcd353 --- /dev/null +++ b/asio/src/examples/tutorial/daytime4/client.cpp @@ -0,0 +1,32 @@ +#include +#include "asio.hpp" + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: client " << std::endl; + return 1; + } + + asio::demuxer demuxer; + + asio::dgram_socket socket(demuxer, asio::ipv4::address(0)); + + char send_buf[1] = { 0 }; + socket.sendto(send_buf, sizeof(send_buf), asio::ipv4::address(13, argv[1])); + + char recv_buf[128]; + asio::ipv4::address remote_address; + size_t len = socket.recvfrom(recv_buf, sizeof(recv_buf), remote_address); + std::cout.write(recv_buf, len); + } + catch (asio::socket_error& e) + { + std::cerr << e.what() << ": " << e.message() << std::endl; + } + + return 0; +} diff --git a/asio/src/examples/tutorial/daytime5/.cvsignore b/asio/src/examples/tutorial/daytime5/.cvsignore new file mode 100644 index 00000000..5b38c25d --- /dev/null +++ b/asio/src/examples/tutorial/daytime5/.cvsignore @@ -0,0 +1,6 @@ +server +.deps +.dirstamp +*.pdb +*.ilk +*.tds diff --git a/asio/src/examples/tutorial/daytime5/server.cpp b/asio/src/examples/tutorial/daytime5/server.cpp new file mode 100644 index 00000000..759d9e21 --- /dev/null +++ b/asio/src/examples/tutorial/daytime5/server.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include "asio.hpp" + +int main() +{ + try + { + asio::demuxer demuxer; + + asio::dgram_socket socket(demuxer, asio::ipv4::address(13)); + + for (;;) + { + char recv_buf[1]; + asio::ipv4::address remote_address; + socket.recvfrom(recv_buf, sizeof(recv_buf), remote_address, + asio::throw_error_if( + asio::error != asio::socket_error::message_size)); + + using namespace std; // For time_t, time and ctime. + time_t now = time(0); + std::string msg = ctime(&now); + + socket.sendto(msg.c_str(), msg.length(), remote_address, + asio::ignore_error()); + } + } + catch (asio::socket_error& e) + { + std::cerr << e.what() << ": " << e.message() << std::endl; + } + + return 0; +} diff --git a/asio/src/examples/tutorial/daytime6/.cvsignore b/asio/src/examples/tutorial/daytime6/.cvsignore new file mode 100644 index 00000000..5b38c25d --- /dev/null +++ b/asio/src/examples/tutorial/daytime6/.cvsignore @@ -0,0 +1,6 @@ +server +.deps +.dirstamp +*.pdb +*.ilk +*.tds diff --git a/asio/src/examples/tutorial/daytime6/server.cpp b/asio/src/examples/tutorial/daytime6/server.cpp new file mode 100644 index 00000000..358877ba --- /dev/null +++ b/asio/src/examples/tutorial/daytime6/server.cpp @@ -0,0 +1,58 @@ +#include +#include +#include "boost/bind.hpp" +#include "asio.hpp" + +void handle_sendto(char* send_buf, const asio::socket_error& /*error*/, + size_t /*bytes_sent*/) +{ + using namespace std; // For free. + free(send_buf); +} + +void handle_recvfrom(asio::dgram_socket* socket, char* recv_buf, + size_t recv_length, asio::ipv4::address* remote_address, + const asio::socket_error& error, size_t /*bytes_recvd*/) +{ + if (!error || error == asio::socket_error::message_size) + { + using namespace std; // For time_t, time, ctime, strdup and strlen. + time_t now = time(0); + char* send_buf = strdup(ctime(&now)); + size_t send_length = strlen(send_buf); + + socket->async_sendto(send_buf, send_length, *remote_address, + boost::bind(handle_sendto, send_buf, asio::arg::error, + asio::arg::bytes_sent)); + + socket->async_recvfrom(recv_buf, recv_length, *remote_address, + boost::bind(handle_recvfrom, socket, recv_buf, recv_length, + remote_address, asio::arg::error, asio::arg::bytes_recvd)); + } +} + +int main() +{ + try + { + asio::demuxer demuxer; + + asio::dgram_socket socket(demuxer, asio::ipv4::address(13)); + + char recv_buf[1]; + size_t recv_length = sizeof(recv_buf); + asio::ipv4::address remote_address; + + socket.async_recvfrom(recv_buf, recv_length, remote_address, + boost::bind(handle_recvfrom, &socket, recv_buf, recv_length, + &remote_address, asio::arg::error, asio::arg::bytes_recvd)); + + demuxer.run(); + } + catch (asio::socket_error& e) + { + std::cerr << e.what() << ": " << e.message() << std::endl; + } + + return 0; +} diff --git a/asio/src/examples/tutorial/daytime_dox.txt b/asio/src/examples/tutorial/daytime_dox.txt new file mode 100644 index 00000000..b3016582 --- /dev/null +++ b/asio/src/examples/tutorial/daytime_dox.txt @@ -0,0 +1,93 @@ +/** +\page tutdaytime1 Tutorial Daytime.1 - A synchronous TCP daytime client + +See the \ref tutdaytime1src \n +Return to the \ref tutindex \n +Go on to \ref tutdaytime2 + +*/ + +/** +\page tutdaytime1src Source listing for Tutorial Daytime.1 +\include daytime1/client.cpp +Return to \ref tutdaytime1 +*/ + +/** +\page tutdaytime2 Tutorial Daytime.2 - A synchronous TCP daytime server + +See the \ref tutdaytime2src \n +Return to the \ref tutindex \n +Go back to \ref tutdaytime1 \n +Go on to \ref tutdaytime3 + +*/ + +/** +\page tutdaytime2src Source listing for Tutorial Daytime.2 +\include daytime2/server.cpp +Return to \ref tutdaytime2 +*/ + +/** +\page tutdaytime3 Tutorial Daytime.3 - An asynchronous TCP daytime server + +See the \ref tutdaytime3src \n +Return to the \ref tutindex \n +Go back to \ref tutdaytime2 \n +Go on to \ref tutdaytime4 + +*/ + +/** +\page tutdaytime3src Source listing for Tutorial Daytime.3 +\include daytime3/server.cpp +Return to \ref tutdaytime3 +*/ + +/** +\page tutdaytime4 Tutorial Daytime.4 - A synchronous UDP daytime client + +See the \ref tutdaytime4src \n +Return to the \ref tutindex \n +Go back to \ref tutdaytime3 \n +Go on to \ref tutdaytime5 + +*/ + +/** +\page tutdaytime4src Source listing for Tutorial Daytime.4 +\include daytime4/client.cpp +Return to \ref tutdaytime4 +*/ + +/** +\page tutdaytime5 Tutorial Daytime.5 - A synchronous UDP daytime server + +See the \ref tutdaytime5src \n +Return to the \ref tutindex \n +Go back to \ref tutdaytime4 \n +Go on to \ref tutdaytime6 + +*/ + +/** +\page tutdaytime5src Source listing for Tutorial Daytime.5 +\include daytime5/server.cpp +Return to \ref tutdaytime5 +*/ + +/** +\page tutdaytime6 Tutorial Daytime.6 - An asynchronous UDP daytime server + +See the \ref tutdaytime6src \n +Return to the \ref tutindex \n +Go back to \ref tutdaytime5 + +*/ + +/** +\page tutdaytime6src Source listing for Tutorial Daytime.6 +\include daytime6/server.cpp +Return to \ref tutdaytime6 +*/ diff --git a/asio/src/examples/tutorial/index_dox.txt b/asio/src/examples/tutorial/index_dox.txt index 0b905d94..42b46ea2 100644 --- a/asio/src/examples/tutorial/index_dox.txt +++ b/asio/src/examples/tutorial/index_dox.txt @@ -3,14 +3,33 @@ \section tuttimer Basic Skills -The first four tutorials introduce the fundamental concepts required to use -the asio toolkit. Before plunging into the complex world of network -programming, these tutorial programs illustrate the basic skills using simple -asynchronous timers. +The tutorials in this first section introduce the fundamental concepts +required to use the asio toolkit. Before plunging into the complex world of +network programming, these tutorial programs illustrate the basic skills using +simple asynchronous timers. \li \ref tuttimer1 \li \ref tuttimer2 \li \ref tuttimer3 \li \ref tuttimer4 +\section tutdaytime Introduction to Sockets + +The tutorials in this section show how to use asio to develop simple client +and server programs. These tutorial programs are based around the daytime protocol, which supports +both TCP and UDP. + +The first three tutorials implement the daytime protocol using TCP. + +\li \ref tutdaytime1 +\li \ref tutdaytime2 +\li \ref tutdaytime3 + +The next three tutorials implement the daytime protocol using UDP. + +\li \ref tutdaytime4 +\li \ref tutdaytime5 +\li \ref tutdaytime6 + */