Using g++ and boost::asio, I'm trying to format network message frames containing the size of the data to be transmited:
+----------------+------------------------+
| Length | Message string |
| 4 bytes | n bytes |
+----------------+------------------------+
Using the examples of asio blocking tcp echo client/server under a 32 byte Linux, I'm stucked with transmitting the correct size in the frame. The client is supposed to be a Windows machine.
CLIENT:
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/asio.hpp>
#include <stdint.h>
#include <arpa/inet.h>
using boost::asio::ip::tcp;
enum { 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;
}
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), argv[1], argv[2]);
tcp::resolver::iterator iterator = resolver.resolve(query);
tcp::socket s(io_service);
s.connect(*iterator);
std::cout << "sizeof uint32_t=" << sizeof(uint32_t) << std::endl;
uint32_t len = 1234;
std::cout << "len=" << len << std::endl;
// uint => char 4
char char_len[4];
char_len[0] = (len >> 0);
char_len[1] = (len >> 8);
char_len[2] = (len >> 16);
char_len[3] = (len >> 24);
std::cout << "char[4] len=["
<< char_len[0] << ',' << char_len[1] << ','
<< char_len[2] << ',' << char_len[3] << ']'
<< std::endl;
// char 4 => uint
uint32_t uint_len = *(reinterpret_cast<uint32_t *>( char_len ));
std::cout << "uint len=" << uint_len << std::endl;
// network bytes order
uint32_t net_len = htonl( len );
std::cout << "net_len=" << net_len << std::endl;
// uint => char 4
char net_char_len[4];
net_char_len[0] = (net_len >> 0);
net_char_len[1] = (net_len >> 8);
net_char_len[2] = (net_len >> 16);
net_char_len[3] = (net_len >> 24);
std::cout << "net char[4] len=["
<< net_char_len[0] << ',' << net_char_len[1] << ','
<< net_char_len[2] << ',' << net_char_len[3] << ']'
<< std::endl;
boost::asio::write(s, boost::asio::buffer(char_len, 4));
char reply[max_length];
size_t reply_length = boost::asio::read(s,
boost::asio::buffer(reply, 1 ));
std::cout << "Reply is: ";
std::cout.write(reply, reply_length);
std::cout << "\n";
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
SERVER:
#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
using boost::asio::ip::tcp;
const int max_length = 1024;
typedef boost::shared_ptr<tcp::socket> socket_ptr;
void session(socket_ptr sock)
{
try
{
for (;;)
{
char data[max_length];
boost::system::error_code error;
size_t length = sock->read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.
uint32_t net_len;
size_t len_len = sock->read_some( boost::asio::buffer( reinterpret_cast<char*>(&net_len), 4), error );
std::clog << "net len=" << net_len << std::endl;
uint32_t len = ntohl( net_len );
std::clog << "uint len=" << len << std::endl;
char char_len = (char)len;
std::clog << "char len=" << len << std::endl;
boost::asio::write(*sock, boost::asio::buffer( &char_len, size_t(1)));
}
}
catch (std::exception& e)
{
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
void server(boost::asio::io_service& io_service, short port)
{
tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
for (;;)
{
socket_ptr sock(new tcp::socket(io_service));
a.accept(*sock);
boost::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;
}
boost::asio::io_service io_service;
std::cout << "Started" << std::endl;
std::cout << "sizeof uint32_t=" << sizeof(uint32_t) << std::endl;
using namespace std; // For atoi.
server(io_service, atoi(argv[1]));
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
Should compile with a simple script:
g++ -o client -I/usr/include/boost -L/usr/lib -lboost_system -lboost_thread-mt client.cpp
g++ -o server -I/usr/include/boost -L/usr/lib -lboost_system -lboost_thread-mt server.cpp
The client is unable to decode the length and the server does not seems to receive the right irt. What am I missing?