Answer the question
In order to leave comments, you need to log in
Why can garbage get into a variable?
I have a small DLL in C++11, it clings to the WS server and waits for data and then closes the connection.
On first start everything works great. The API key comes as expected, but on the second iteration, the API key gets the contents of the error array, I can’t understand why this happens, can anyone tell me what could be the reason?
#include "stdafx.h"
#include "Project2.h"
#include "Header1.h"
//#include <eh.h>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
auto decorator_with_key(boost::beast::string_view& key) {
return [key](websocket::request_type& req) {
req.set("Api-Key", key);
};
}
PROJECT2_API bool readSignal(char* buf, char* key_input, char* error) {
try
{
boost::beast::string_view key = std::string(key_input);
std::string host = "example.com";
auto const port = "3001";
beast::flat_buffer buffer;
net::io_context ioc;
tcp::resolver resolver{ ioc };
websocket::stream<tcp::socket> ws{ ioc };
auto const results = resolver.resolve(host, port);
auto ep = net::connect(ws.next_layer(), results);
host += ':' + std::to_string(ep.port());
ws.set_option(websocket::stream_base::decorator(decorator_with_key(key)));
ws.handshake(host, "/");
ws.read(buffer);
ws.close(websocket::close_code::normal);
std::string read_buffer = beast::buffers_to_string(buffer.data());
strcpy_s(buf, 255, read_buffer.c_str());
return true;
}
catch (std::exception const& e)
{
strcpy_s(error, 255, e.what());
return false;
}
return false;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question