UDP is a datagram protocol. The protocol is stateless, and packets need to be received as a single packet.
See Beej's Guide to Networking for background information.
Your receive buffer, however, is 1 byte. You can not expect to read more than the first byte.
This looks like it may be copied from e.g. tutorial 6 listing. The difference is, that was a daytime service which doesn't care about the request. All that it wants to know is that "a packet was received" and it will just send the daytime response unconditionally.
So, a solution would seem to be to increase the buffer size. If the size of package cannot be predicted/is not bounded, consider switching to stream sockets (TCP/IP).
Here, as a bonus, using c++11 style:
#include <boost/asio.hpp>
using boost::asio::ip::udp;
class udp_server
{
public:
udp_server(boost::asio::io_service& io_service)
: socket_(io_service, udp::endpoint(udp::v4(), 7799))
{
start_receive();
}
private:
void start_receive()
{
socket_.async_receive_from(
boost::asio::buffer(recv_buffer_), remote_endpoint_,
[this](boost::system::error_code ec, std::size_t bytes_transferred)
{
if (!ec && (bytes_transferred > 0))
{
std::cout << "PACKET[" << std::string(&recv_buffer_[0], &recv_buffer_[0]+bytes_transferred) << "]\n";
} else
throw ec; // TODO
start_receive();
});
}
udp::socket socket_;
char recv_buffer_[1024];
udp::endpoint remote_endpoint_;
};
int main()
{
std::cout << std::unitbuf;
try
{
boost::asio::io_service io_service;
udp_server server(io_service);
io_service.run();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}