Added UDP daytime tutorial programs.
This commit is contained in:
parent
227579fb2c
commit
e19c3deb7d
@ -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 \
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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 && \
|
||||
|
@ -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
|
||||
|
@ -1,15 +1,14 @@
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#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());
|
||||
|
6
asio/src/examples/tutorial/daytime4/.cvsignore
Normal file
6
asio/src/examples/tutorial/daytime4/.cvsignore
Normal file
@ -0,0 +1,6 @@
|
||||
client
|
||||
.deps
|
||||
.dirstamp
|
||||
*.pdb
|
||||
*.ilk
|
||||
*.tds
|
32
asio/src/examples/tutorial/daytime4/client.cpp
Normal file
32
asio/src/examples/tutorial/daytime4/client.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include <iostream>
|
||||
#include "asio.hpp"
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
try
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
std::cerr << "Usage: client <host>" << 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;
|
||||
}
|
6
asio/src/examples/tutorial/daytime5/.cvsignore
Normal file
6
asio/src/examples/tutorial/daytime5/.cvsignore
Normal file
@ -0,0 +1,6 @@
|
||||
server
|
||||
.deps
|
||||
.dirstamp
|
||||
*.pdb
|
||||
*.ilk
|
||||
*.tds
|
36
asio/src/examples/tutorial/daytime5/server.cpp
Normal file
36
asio/src/examples/tutorial/daytime5/server.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#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;
|
||||
}
|
6
asio/src/examples/tutorial/daytime6/.cvsignore
Normal file
6
asio/src/examples/tutorial/daytime6/.cvsignore
Normal file
@ -0,0 +1,6 @@
|
||||
server
|
||||
.deps
|
||||
.dirstamp
|
||||
*.pdb
|
||||
*.ilk
|
||||
*.tds
|
58
asio/src/examples/tutorial/daytime6/server.cpp
Normal file
58
asio/src/examples/tutorial/daytime6/server.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#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;
|
||||
}
|
93
asio/src/examples/tutorial/daytime_dox.txt
Normal file
93
asio/src/examples/tutorial/daytime_dox.txt
Normal file
@ -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
|
||||
*/
|
@ -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 <a
|
||||
href="http://www.ietf.org/rfc/rfc867.txt">daytime</a> 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
|
||||
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user