Added some echo example programs.
This commit is contained in:
parent
da09329596
commit
4edb35a23c
@ -14,6 +14,10 @@ noinst_PROGRAMS = \
|
||||
tests/timed_dgram_recv_test \
|
||||
tests/timer_test \
|
||||
tests/tpc_echo_server_test \
|
||||
examples/echo/async_tcp_echo_server \
|
||||
examples/echo/async_udp_echo_server \
|
||||
examples/echo/blocking_tcp_echo_client \
|
||||
examples/echo/blocking_udp_echo_client \
|
||||
examples/tutorial/timer1/timer \
|
||||
examples/tutorial/timer2/timer \
|
||||
examples/tutorial/timer3/timer \
|
||||
@ -32,6 +36,10 @@ tests_timed_connect_test_SOURCES = tests/timed_connect_test.cpp
|
||||
tests_timed_dgram_recv_test_SOURCES = tests/timed_dgram_recv_test.cpp
|
||||
tests_timer_test_SOURCES = tests/timer_test.cpp
|
||||
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_udp_echo_client_SOURCES = examples/echo/blocking_udp_echo_client.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
|
||||
|
@ -18,6 +18,10 @@ all: \
|
||||
tests\timed_dgram_recv_test.exe \
|
||||
tests\timer_test.exe \
|
||||
tests\tpc_echo_server_test.exe \
|
||||
examples\echo\async_tcp_echo_server.exe \
|
||||
examples\echo\async_udp_echo_server.exe \
|
||||
examples\echo\blocking_tcp_echo_client.exe \
|
||||
examples\echo\blocking_udp_echo_client.exe \
|
||||
examples\tutorial\timer1\timer.exe \
|
||||
examples\tutorial\timer2\timer.exe \
|
||||
examples\tutorial\timer3\timer.exe \
|
||||
|
@ -20,6 +20,10 @@ TEST_EXES = \
|
||||
tests/tpc_echo_server_test.exe
|
||||
|
||||
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_udp_echo_client.exe \
|
||||
examples\tutorial\timer1\timer.exe \
|
||||
examples\tutorial\timer2\timer.exe \
|
||||
examples\tutorial\timer3\timer.exe \
|
||||
@ -28,10 +32,10 @@ EXAMPLE_EXES = \
|
||||
all: $(TEST_EXES) $(EXAMPLE_EXES)
|
||||
|
||||
clean:
|
||||
-del /q /s tests\*.exe
|
||||
-del /q /s tests\*.o
|
||||
-del /q /s examples\*.exe
|
||||
-del /q /s examples\*.o
|
||||
-rm -f $(TEST_EXES)
|
||||
-rm -f $(TEST_EXES:.exe=.o)
|
||||
-rm -f $(EXAMPLE_EXES)
|
||||
-rm -f $(EXAMPLE_EXES:.exe=.o)
|
||||
|
||||
$(TEST_EXES) $(EXAMPLE_EXES): %.exe: %.o
|
||||
g++ -o$@ $(LDFLAGS) $< $(LIBS)
|
||||
|
@ -18,6 +18,10 @@ all: \
|
||||
tests\timed_dgram_recv_test.exe \
|
||||
tests\timer_test.exe \
|
||||
tests\tpc_echo_server_test.exe \
|
||||
examples\echo\async_tcp_echo_server.exe \
|
||||
examples\echo\async_udp_echo_server.exe \
|
||||
examples\echo\blocking_tcp_echo_client.exe \
|
||||
examples\echo\blocking_udp_echo_client.exe \
|
||||
examples\tutorial\timer1\timer.exe \
|
||||
examples\tutorial\timer2\timer.exe \
|
||||
examples\tutorial\timer3\timer.exe \
|
||||
@ -34,6 +38,10 @@ tests\timed_connect_test.exe: tests\timed_connect_test.obj
|
||||
tests\timed_dgram_recv_test.exe: tests\timed_dgram_recv_test.obj
|
||||
tests\timer_test.exe: tests\timer_test.obj
|
||||
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_udp_echo_client.exe: examples\echo\blocking_udp_echo_client.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
|
||||
|
8
asio/src/examples/echo/.cvsignore
Normal file
8
asio/src/examples/echo/.cvsignore
Normal file
@ -0,0 +1,8 @@
|
||||
.deps
|
||||
.dirstamp
|
||||
*.exe
|
||||
*_server
|
||||
*_client
|
||||
*.ilk
|
||||
*.pdb
|
||||
*.tds
|
117
asio/src/examples/echo/async_tcp_echo_server.cpp
Normal file
117
asio/src/examples/echo/async_tcp_echo_server.cpp
Normal file
@ -0,0 +1,117 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <boost/bind.hpp>
|
||||
#include "asio.hpp"
|
||||
|
||||
class session
|
||||
{
|
||||
public:
|
||||
session(asio::demuxer& d)
|
||||
: socket_(d)
|
||||
{
|
||||
}
|
||||
|
||||
asio::stream_socket& socket()
|
||||
{
|
||||
return socket_;
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
socket_.async_recv(data_, max_length,
|
||||
boost::bind(&session::handle_recv, this, _1, _2));
|
||||
}
|
||||
|
||||
void handle_recv(const asio::socket_error& error, size_t bytes_recvd)
|
||||
{
|
||||
if (!error && bytes_recvd > 0)
|
||||
{
|
||||
asio::async_send_n(socket_, data_, bytes_recvd,
|
||||
boost::bind(&session::handle_send, this, _1, _2, _3));
|
||||
}
|
||||
else
|
||||
{
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
void handle_send(const asio::socket_error& error, size_t total_bytes_sent,
|
||||
size_t last_bytes_sent)
|
||||
{
|
||||
if (!error && last_bytes_sent > 0)
|
||||
{
|
||||
socket_.async_recv(data_, max_length,
|
||||
boost::bind(&session::handle_recv, this, _1, _2));
|
||||
}
|
||||
else
|
||||
{
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
asio::stream_socket socket_;
|
||||
enum { max_length = 1024 };
|
||||
char data_[max_length];
|
||||
};
|
||||
|
||||
class server
|
||||
{
|
||||
public:
|
||||
server(asio::demuxer& d, short port)
|
||||
: demuxer_(d),
|
||||
acceptor_(d, asio::inet_address_v4(port))
|
||||
{
|
||||
session* new_session = new session(demuxer_);
|
||||
acceptor_.async_accept(new_session->socket(),
|
||||
boost::bind(&server::handle_accept, this, new_session, _1));
|
||||
}
|
||||
|
||||
void handle_accept(session* new_session, const asio::socket_error& error)
|
||||
{
|
||||
if (!error)
|
||||
{
|
||||
new_session->start();
|
||||
new_session = new session(demuxer_);
|
||||
acceptor_.async_accept(new_session->socket(),
|
||||
boost::bind(&server::handle_accept, this, new_session, _1));
|
||||
}
|
||||
else
|
||||
{
|
||||
delete new_session;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
asio::demuxer& demuxer_;
|
||||
asio::socket_acceptor acceptor_;
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
try
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
std::cerr << "Usage: async_tcp_echo_server <port>\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
asio::demuxer d;
|
||||
|
||||
using namespace std; // For atoi.
|
||||
server s(d, atoi(argv[1]));
|
||||
|
||||
d.run();
|
||||
}
|
||||
catch (asio::socket_error& e)
|
||||
{
|
||||
std::cerr << "Socket error: " << e.message() << "\n";
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
70
asio/src/examples/echo/async_udp_echo_server.cpp
Normal file
70
asio/src/examples/echo/async_udp_echo_server.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <boost/bind.hpp>
|
||||
#include "asio.hpp"
|
||||
|
||||
class server
|
||||
{
|
||||
public:
|
||||
server(asio::demuxer& d, short port)
|
||||
: demuxer_(d),
|
||||
socket_(d, asio::inet_address_v4(port))
|
||||
{
|
||||
socket_.async_recvfrom(data_, max_length, sender_address_,
|
||||
boost::bind(&server::handle_recvfrom, this, _1, _2));
|
||||
}
|
||||
|
||||
void handle_recvfrom(const asio::socket_error& error, size_t bytes_recvd)
|
||||
{
|
||||
if (!error && bytes_recvd > 0)
|
||||
{
|
||||
socket_.async_sendto(data_, bytes_recvd, sender_address_,
|
||||
boost::bind(&server::handle_sendto, this, _1, _2));
|
||||
}
|
||||
}
|
||||
|
||||
void handle_sendto(const asio::socket_error& error, size_t bytes_sent)
|
||||
{
|
||||
if (!error)
|
||||
{
|
||||
socket_.async_recvfrom(data_, max_length, sender_address_,
|
||||
boost::bind(&server::handle_recvfrom, this, _1, _2));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
asio::demuxer& demuxer_;
|
||||
asio::dgram_socket socket_;
|
||||
asio::inet_address_v4 sender_address_;
|
||||
enum { max_length = 1024 };
|
||||
char data_[max_length];
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
try
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
std::cerr << "Usage: async_udp_echo_server <port>\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
asio::demuxer d;
|
||||
|
||||
using namespace std; // For atoi.
|
||||
server s(d, atoi(argv[1]));
|
||||
|
||||
d.run();
|
||||
}
|
||||
catch (asio::socket_error& e)
|
||||
{
|
||||
std::cerr << "Socket error: " << e.message() << "\n";
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
47
asio/src/examples/echo/blocking_tcp_echo_client.cpp
Normal file
47
asio/src/examples/echo/blocking_tcp_echo_client.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include "asio.hpp"
|
||||
|
||||
const int max_length = 1024;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
try
|
||||
{
|
||||
if (argc != 3)
|
||||
{
|
||||
std::cerr << "Usage: blocking_tcp_echo_client <host> <port>\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
asio::demuxer d;
|
||||
|
||||
using namespace std; // For atoi and strlen.
|
||||
asio::stream_socket s(d);
|
||||
asio::socket_connector c(d);
|
||||
c.connect(s, asio::inet_address_v4(atoi(argv[2]), argv[1]));
|
||||
|
||||
std::cout << "Enter message: ";
|
||||
char request[max_length];
|
||||
std::cin.getline(request, max_length);
|
||||
size_t request_length = strlen(request);
|
||||
asio::send_n(s, request, request_length);
|
||||
|
||||
char reply[max_length];
|
||||
size_t reply_length = asio::recv_n(s, reply, request_length);
|
||||
std::cout << "Reply is: ";
|
||||
std::cout.write(reply, reply_length);
|
||||
std::cout << "\n";
|
||||
}
|
||||
catch (asio::socket_error& e)
|
||||
{
|
||||
std::cerr << "Socket error: " << e.message() << "\n";
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
47
asio/src/examples/echo/blocking_udp_echo_client.cpp
Normal file
47
asio/src/examples/echo/blocking_udp_echo_client.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include "asio.hpp"
|
||||
|
||||
const int max_length = 1024;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
try
|
||||
{
|
||||
if (argc != 3)
|
||||
{
|
||||
std::cerr << "Usage: blocking_udp_echo_client <host> <port>\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
asio::demuxer d;
|
||||
|
||||
asio::dgram_socket s(d, asio::inet_address_v4(0));
|
||||
|
||||
using namespace std; // For atoi and strlen.
|
||||
std::cout << "Enter message: ";
|
||||
char request[max_length];
|
||||
std::cin.getline(request, max_length);
|
||||
size_t request_length = strlen(request);
|
||||
asio::inet_address_v4 receiver_address(atoi(argv[2]), argv[1]);
|
||||
s.sendto(request, request_length, receiver_address);
|
||||
|
||||
char reply[max_length];
|
||||
asio::inet_address_v4 sender_address;
|
||||
size_t reply_length = s.recvfrom(reply, max_length, sender_address);
|
||||
std::cout << "Reply is: ";
|
||||
std::cout.write(reply, reply_length);
|
||||
std::cout << "\n";
|
||||
}
|
||||
catch (asio::socket_error& e)
|
||||
{
|
||||
std::cerr << "Socket error: " << e.message() << "\n";
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user