SSL_CTX *create_ssl_context() {
SSL_CTX *ctx = SSL_CTX_new(TLS_client_method());
if (!ctx) {
fprintf(stderr, "Ошибка создания TLS-контекста: %s\n", ERR_error_string(ERR_get_error(), NULL));
exit(EXIT_FAILURE);
}
return ctx;
}
void configure_context(SSL_CTX *ctx) {
// Укажите алгоритм доступа к сертификатам CA (при необходимости)
if (SSL_CTX_load_verify_locations(ctx, "private_key.pem", NULL) != 1) {
ERR_print_errors_fp(stderr);
abort();
}
}
std::string getCurrentTimeFormatted() {
std::time_t now = std::time(0);
std::tm *gmtm = gmtime(&now);
if (gmtm == nullptr) {
// Обработка ошибки, например, вывод сообщения об ошибке
std::cerr << "Ошибка получения времени" << std::endl;
return "";
}
std::stringstream ss;
ss << std::put_time(gmtm, "%Y%m%d-%H:%M:%S");
// Получаем миллисекунды
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count() % 1000;
ss << "." << std::setfill('0') << std::setw(3) << ms;
return ss.str();
}
int main() {
SSL_library_init();
SSL_load_error_strings();
ERR_load_crypto_strings();
OpenSSL_add_all_ciphers();
OpenSSL_add_all_digests();
OpenSSL_add_ssl_algorithms();
const char *hostname = "fix-oe.binance.com";
const int port = 9000;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Ошибка создания сокета");
exit(EXIT_FAILURE);
}
struct hostent *server = gethostbyname(hostname);
if (!server) {
fprintf(stderr, "Ошибка получения адреса хоста: %s\n", hostname);
exit(EXIT_FAILURE);
}
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
serv_addr.sin_port = htons(port);
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("Ошибка подключения");
exit(EXIT_FAILURE);
}
SSL_CTX *ctx = create_ssl_context();
SSL *ssl = SSL_new(ctx);
//configure_context(ctx);
if (!ssl) {
fprintf(stderr, "Ошибка создания SSL-объекта: %s\n", ERR_error_string(ERR_get_error(), NULL));
exit(EXIT_FAILURE);
}
SSL_set_fd(ssl, sockfd);
if (SSL_connect(ssl) <= 0) {
fprintf(stderr, "Ошибка установления TLS-соединения: %s\n", ERR_error_string(ERR_get_error(), NULL));
exit(EXIT_FAILURE);
}
``` - in this code i set a tsl connection but server dont renponse after i sent logon<A> message
in this code i set a tsl connection but server dont renponse after i sent logon message - help please
Your OpenSSL code looks correct but it’s notoriously hard to get it right.
Does it work for non-FIX APIs? If you use this code connect to api.binance.com:443
instead of fix-oe.binance.com
and, for example, send a raw HTTP request for server time:
GET /api/v3/time HTTP/1.1
Host: api.binance.com
Accept: application/json
Does the connection get established successfully and do you get a response back?
Thank i have solve this problem myself, now i have another question, do you now: SSL_write function works 10 microseconds but i wait SSL_read 3 milliseconds after write , what is reazon for this waiting?
@vadinabronin This is not an OpenSSL support forum. I can’t provide extensive practical advice on how to develop trading bots, sorry
Typically, SSL_write() only accepts the outgoing data, encrypts it, puts the bytes in the buffer, while the actual network send might happen later in background.
SSL_read() on the other hand will wait until the server response is received from the network, gets decrypted, and returned to your application.