Added blocking server examples.

This commit is contained in:
chris 2003-11-05 05:33:18 +00:00
parent 6bd3aa2a34
commit 1a60813234
6 changed files with 124 additions and 0 deletions

View File

@ -17,7 +17,9 @@ noinst_PROGRAMS = \
examples/echo/async_tcp_echo_server \
examples/echo/async_udp_echo_server \
examples/echo/blocking_tcp_echo_client \
examples/echo/blocking_tcp_echo_server \
examples/echo/blocking_udp_echo_client \
examples/echo/blocking_udp_echo_server \
examples/tutorial/timer1/timer \
examples/tutorial/timer2/timer \
examples/tutorial/timer3/timer \
@ -39,7 +41,9 @@ tests_tpc_echo_server_test_SOURCES = tests/tpc_echo_server_test.cpp
examples_echo_async_tcp_echo_server_SOURCES = examples/echo/async_tcp_echo_server.cpp
examples_echo_async_udp_echo_server_SOURCES = examples/echo/async_udp_echo_server.cpp
examples_echo_blocking_tcp_echo_client_SOURCES = examples/echo/blocking_tcp_echo_client.cpp
examples_echo_blocking_tcp_echo_server_SOURCES = examples/echo/blocking_tcp_echo_server.cpp
examples_echo_blocking_udp_echo_client_SOURCES = examples/echo/blocking_udp_echo_client.cpp
examples_echo_blocking_udp_echo_server_SOURCES = examples/echo/blocking_udp_echo_server.cpp
examples_tutorial_timer1_timer_SOURCES = examples/tutorial/timer1/timer.cpp
examples_tutorial_timer2_timer_SOURCES = examples/tutorial/timer2/timer.cpp
examples_tutorial_timer3_timer_SOURCES = examples/tutorial/timer3/timer.cpp

View File

@ -21,7 +21,9 @@ all: \
examples\echo\async_tcp_echo_server.exe \
examples\echo\async_udp_echo_server.exe \
examples\echo\blocking_tcp_echo_client.exe \
examples\echo\blocking_tcp_echo_server.exe \
examples\echo\blocking_udp_echo_client.exe \
examples\echo\blocking_udp_echo_server.exe \
examples\tutorial\timer1\timer.exe \
examples\tutorial\timer2\timer.exe \
examples\tutorial\timer3\timer.exe \

View File

@ -23,7 +23,9 @@ EXAMPLE_EXES = \
examples\echo\async_tcp_echo_server.exe \
examples\echo\async_udp_echo_server.exe \
examples\echo\blocking_tcp_echo_client.exe \
examples\echo\blocking_tcp_echo_server.exe \
examples\echo\blocking_udp_echo_client.exe \
examples\echo\blocking_udp_echo_server.exe \
examples\tutorial\timer1\timer.exe \
examples\tutorial\timer2\timer.exe \
examples\tutorial\timer3\timer.exe \

View File

@ -21,7 +21,9 @@ all: \
examples\echo\async_tcp_echo_server.exe \
examples\echo\async_udp_echo_server.exe \
examples\echo\blocking_tcp_echo_client.exe \
examples\echo\blocking_tcp_echo_server.exe \
examples\echo\blocking_udp_echo_client.exe \
examples\echo\blocking_udp_echo_server.exe \
examples\tutorial\timer1\timer.exe \
examples\tutorial\timer2\timer.exe \
examples\tutorial\timer3\timer.exe \
@ -41,7 +43,9 @@ tests\tpc_echo_server_test.exe: tests\tpc_echo_server_test.obj
examples\echo\async_tcp_echo_server.exe: examples\echo\async_tcp_echo_server.obj
examples\echo\async_udp_echo_server.exe: examples\echo\async_udp_echo_server.obj
examples\echo\blocking_tcp_echo_client.exe: examples\echo\blocking_tcp_echo_client.obj
examples\echo\blocking_tcp_echo_server.exe: examples\echo\blocking_tcp_echo_server.obj
examples\echo\blocking_udp_echo_client.exe: examples\echo\blocking_udp_echo_client.obj
examples\echo\blocking_udp_echo_server.exe: examples\echo\blocking_udp_echo_server.obj
examples\tutorial\timer1\timer.exe: examples\tutorial\timer1\timer.obj
examples\tutorial\timer2\timer.exe: examples\tutorial\timer2\timer.obj
examples\tutorial\timer3\timer.exe: examples\tutorial\timer3\timer.obj

View File

@ -0,0 +1,68 @@
#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include "asio.hpp"
const int max_length = 1024;
typedef boost::shared_ptr<asio::stream_socket> stream_socket_ptr;
void session(stream_socket_ptr sock)
{
try
{
char data[max_length];
int length;
while ((length = sock->recv(data, max_length)) > 0)
if (asio::send_n(*sock, data, length) <= 0)
break;
}
catch (asio::socket_error& e)
{
std::cerr << "Socket error in thread: " << e.message() << "\n";
}
catch (std::exception& e)
{
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
void server(asio::demuxer& d, short port)
{
asio::socket_acceptor a(d, asio::inet_address_v4(port));
for (;;)
{
stream_socket_ptr sock(new asio::stream_socket(d));
a.accept(*sock);
asio::detail::thread t(boost::bind(session, sock));
}
}
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: blocking_tcp_echo_server <port>\n";
return 1;
}
asio::demuxer d;
using namespace std; // For atoi.
server(d, atoi(argv[1]));
}
catch (asio::socket_error& e)
{
std::cerr << "Socket error: " << e.message() << "\n";
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}

View File

@ -0,0 +1,44 @@
#include <cstdlib>
#include <iostream>
#include "asio.hpp"
const int max_length = 1024;
void server(asio::demuxer& d, short port)
{
asio::dgram_socket sock(d, asio::inet_address_v4(port));
for (;;)
{
char data[max_length];
asio::inet_address_v4 sender_address;
size_t length = sock.recvfrom(data, max_length, sender_address);
sock.sendto(data, length, sender_address);
}
}
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: blocking_udp_echo_server <port>\n";
return 1;
}
asio::demuxer d;
using namespace std; // For atoi.
server(d, atoi(argv[1]));
}
catch (asio::socket_error& e)
{
std::cerr << "Socket error: " << e.message() << "\n";
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}