Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ sockets, how to send hexadecimal with asio?

I need help, I’m trying to send this data in hexadeciamal, but always the packet_byte.size() and date() says: the express needs to have type of calsse, but has the type "char".
I dont now what i need do, if anyone can help me.and if you can indicate what to study to understand this error Thank you.

#include <iostream>
#define ASIO_STANDALONE
#ifdef _WIN32
#define _WIN32_WINNT 0x0A00
#endif 
#include <asio.hpp>
#include <asio/ts/buffer.hpp>
#include <asio/ts/internet.hpp>





int main() {
    asio::error_code ec; //pega erros especificos

    //Cria o "context" 
    asio::io_context context;

    //Pega o endereço no qual estamos conectando1
    asio::ip::tcp::endpoint endpoint(asio::ip::make_address("ip", ec), 15559);
    
    //Cria o socket, o context ira entregar a implementaçao
    asio::ip::tcp::socket socket(context);

    //Diz ao socket para tentar se conectar
    socket.connect(endpoint, ec);

    if (!ec) {
        std::cout << "Conectado!" << std::endl; 
    }

    else {
        std::cout << "Falha de conecxao com o endereço: \n" << ec.message()/*pega mensagem associada ao erro */ << std::endl;
    }

    //Verifia se esta conectado
    if (socket.is_open()) { 

        const unsigned char packet_bytes [] = {
                "0x1e, 0x00, 0xe0, 0x55, 0xc0, 0x52, 0x09, 0x00,"
                "0x41, 0x00, 0x73, 0x00, 0x70, 0x00, 0x70, 0x00,"
                "0x69, 0x00, 0x65, 0x00, 0x72, 0x00, 0x72, 0x00,"
                "0x00, 0x00, 0x08, 0x9b, 0x95, 0x01"
         };
        


        //Se conexçao for True, escreve:
        //asio::buffer e um container, contendo bytes da string e sizer.
        socket.write_some(asio::buffer(packet_bytes.data(), packet_bytes.size()), ec);
        
        //Estava dando erro pelo tempo de resposta, programa indo mais rapido que a resposta.       
        socket.wait(socket.wait_read);
        
        size_t bytes = socket.available();
        std::cout << "Bytes Available: " << bytes << std::endl;

        if (bytes > 0) {
            //adiciona dados recebidos em um vector
            std::vector<char> vBuffer(bytes);
            socket.read_some(asio::buffer(vBuffer.data(), vBuffer.size()), ec);
            // 
            for (auto c : vBuffer) {
                std::cout << c;
            }
        }

        
    }
    system("pause");
    return 0;

}```

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your packet_bytes is a plain character array, it doesn’t have data and size member function. Hence it is not possible to call packet_bytes.data() and packet_byte.size() on character array.

To create a asio::buffer from an array of characters, you need to pass the character array and its length to asio::buffer.

Please update your code as follows, then it should compile fine.

socket.write_some(asio::buffer(packet_bytes, sizeof(packet_bytes)), ec);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading