i have a problem with the Code below, i want a TCP-IP SSL Listener that connect to a Server (Same IP 127.0.0.1 and PORT 3333) The Server are a Python programm, my probleme are with the Python Listener or the QModbusmaster programm the connection with the Server are succesful but with my esp Programm the connection are not possible and the code close the socket.
At the second state of my code i want a SSL-Cryptographie with the IP and the Port but i have no idee how its possible, i know i need a x509 Certificate but the code structure of the Listener
- /*
- * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
- *
- * SPDX-License-Identifier: Unlicense OR CC0-1.0
- */
- #include "sdkconfig.h"
- #include <string.h>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <errno.h>
- #include <netdb.h> // struct addrinfo
- #include <arpa/inet.h>
- #include "esp_netif.h"
- #include "esp_log.h"
- #if defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN)
- #include "addr_from_stdin.h"
- #endif
- #if defined(CONFIG_EXAMPLE_IPV4)
- #define HOST_IP_ADDR CONFIG_EXAMPLE_IPV4_ADDR
- #elif defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN)
- #define HOST_IP_ADDR ""
- #endif
- #define PORT CONFIG_EXAMPLE_PORT
- static const char *TAG = "example";
- static const char *payload = "Message from ESP32 ";
- void tcp_client(void)
- {
- char rx_buffer[128];
- char host_ip[] = HOST_IP_ADDR;
- int addr_family = 0;
- int ip_protocol = 0;
- while (1) {
- #if defined(CONFIG_EXAMPLE_IPV4)
- struct sockaddr_in dest_addr;
- inet_pton(AF_INET, host_ip, &dest_addr.sin_addr);
- dest_addr.sin_family = AF_INET;
- dest_addr.sin_port = htons(PORT);
- addr_family = AF_INET;
- ip_protocol = IPPROTO_IP;
- #elif defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN)
- struct sockaddr_storage dest_addr = { 0 };
- ESP_ERROR_CHECK(get_addr_from_stdin(PORT, SOCK_STREAM, &ip_protocol, &addr_family, &dest_addr));
- #endif
- int sock = socket(addr_family, SOCK_STREAM, ip_protocol);
- if (sock < 0) {
- ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
- break;
- }
- ESP_LOGI(TAG, "Socket created, connecting to %s:%d", host_ip, PORT);
- int err = connect(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
- if (err != 0) {
- ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
- break;
- }
- ESP_LOGI(TAG, "Successfully connected");
- while (1) {
- int err = send(sock, payload, strlen(payload), 0);
- if (err < 0) {
- ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
- break;
- }
- int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
- // Error occurred during receiving
- if (len < 0) {
- ESP_LOGE(TAG, "recv failed: errno %d", errno);
- break;
- }
- // Data received
- else {
- rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
- ESP_LOGI(TAG, "Received %d bytes from %s:", len, host_ip);
- ESP_LOGI(TAG, "%s", rx_buffer);
- }
- }
- if (sock != -1) {
- ESP_LOGE(TAG, "Shutting down socket and restarting...");
- shutdown(sock, 0);
- close(sock);
- }
- }
- }
and now the python code
- import socket
- import ssl
- SERVER_IP = "127.0.0.1" # IP-Adresse des ESP32-Servers
- SERVER_PORT = 3333
- context = ssl.create_default_context()
- with socket.create_connection((SERVER_IP, SERVER_PORT)) as sock:
- with context.wrap_socket(sock, server_hostname=SERVER_IP) as ssock:
- # Daten senden
- message = "Hello from Python!"
- ssock.sendall(message.encode())
- # Antwort vom Server empfangen
- response = ssock.recv(1024)
- print("Received:", response.decode())