ارسال سوکت از طریق IPV4 و شماره پورت

سلام دوستان. درباره کانکت سوکت در میکرو های esp32s3 سوال داشتم. من یک برد دارم، esp32s3 devkit که میخوام با استفاده از وایفای داده انتقال بدم به سیستم. IDE ام هم Espressif idf هست
برنامه شو نوشتم و یکبار هم وصل شد و به پورت سیستم ام دیتا میفرستاد ولی بعد از دو روز دیگه وصل نشد و کد تو قسمت توابع api گیر میکنه و ارور برمیگردونه. کسی هست بتونه کمکم کنه؟

فرمت کد میریزه بهم اگر اینجا کپی پیست کنم.
فایر وال خاموشه و تغییری توی کد ندادم و همچنین آیپی سیستم های دیگه رو هم چک کردم و پورت رو هم عوض کردم ولی وصل نمیشه!

سلام کدتون رو اینجا قرار بدید تا بررسی بشه.

image
از طریق این منو میتونید کد داخل سوالتون قرار بدید که فرمتش بهم نریزه
اگر هم نشد قرار بدید من خودم ویرایشش میکنم

سلام. ببینید مشکل تا حدودی حل شده ولی نه کاملا. برنامه من یه لوپپ داره که آخرین Irritation اش رو همون ارور رو میده.

/* Scan Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/

/*
    This example shows how to scan for available set of APs.
*/
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "freertos/task.h"
#include "esp_mac.h"
#include "esp_event_loop.h"
#include "esp_dpp.h"
#include "esp_flash.h"
#include "UART_COMP.h"
#include "lwip/tcpip.h"
#include "unistd.h"
#include "lwip/sys.h"
#include "lwip/sockets.h"
#include "mdns.h"
#include "netdb.h" //getnameinfo
#include "unistd.h" //tp close socket
#include "protocol_examples_common.h"
#include "lwip/inet.h"
#include "sdkconfig.h"

#define DEFAULT_SCAN_LIST_SIZE CONFIG_EXAMPLE_SCAN_LIST_SIZE + 20
#define WIFI_SSID ""
#define WIFI_PASS  ""

wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
uint16_t ap_count = 0;
uint16_t retry_num = 0;
esp_netif_t *sta_netif;
wifi_config_t wifi_configuration = {.sta = {.ssid = WIFI_SSID , .password = WIFI_PASS}};
bool wifi_conncted = false;
char host_name[] = "192.168.231.246";
typedef struct sockaddr SA;
char* Payload = "Hi there, ESP32S3 speaking!";

//tcpip_adapter_dns_info_t dns_info = {0};
//dhcps_offer_t dhcps_dns_value = OFFER_DNS;
//dns_info.ip.type = IPADDR_TYPE_V4;

static void wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id,void *event_data)
{
	if(event_id == WIFI_EVENT_STA_START)
	{
		printf("WIFI CONNECTING....\n");
		retry_num = 0;
	}
	else if (event_id == WIFI_EVENT_STA_CONNECTED)
	{
		printf("WiFi CONNECTED\n");
		retry_num = 0;
	}
	else if (event_id == WIFI_EVENT_STA_DISCONNECTED)
	{
		printf("WiFi lost connection\n");
		if(retry_num < 10)
		{
			esp_wifi_connect();
			retry_num++;
			printf("Retrying to Connect...\n");
		}
	}
	else if (event_id == IP_EVENT_STA_GOT_IP)
	{
		printf("WiFi got IP...\n\n");
		wifi_conncted = true;
		retry_num = 0;
	}
}

int tcp_client()
{
	int port = 8032;
	vTaskDelay(5000/portTICK_PERIOD_MS);
    int ip_protocol = 0;
    for (;;)
    {
    	if (wifi_conncted)
		{

			printf("\n-------1 ok--------\n");
			int socket_file_descriptor = socket(AF_INET, SOCK_STREAM, ip_protocol);
			if (socket_file_descriptor < 0)
			{
				ESP_LOGE("tcp_client", "Unable to create socket: errno %d", errno);
				printf("\n-------2 error--------\n");
				break;
			}
			printf("-------2 ok--------\n");

			struct hostent *host = gethostbyname(host_name); //   192.168.1.102    "www.google.com"
			if (host == NULL)
			{
				//herror("gethostbyname");
				printf("NO host!\n");
				printf("\n-------3 error--------\n");
				return -1;
			}
			printf("-------3 ok--------\n");

			struct sockaddr_in address;

			address.sin_family = AF_INET; //IPV4
			address.sin_port = htons(port); //Server port number internet endianess is big endian  8032
			//address.sin_addr.s_addr = (in_addr_t)host->h_addr_list[0]; //server IP
			//address.sin_addr.s_addr = inet_addr(host->h_addr);

			memcpy(&address.sin_addr, host_name, sizeof(address.sin_addr));
			memset(&(address.sin_zero), 0, 8);

			printf("-------4 ok--------\n");

			printf("All addresses: ");
			struct in_addr **addr_list = (struct in_addr **)host->h_addr_list;
			for(int i = 0; addr_list[i] != NULL; i++)
			{
			    printf("%s \n", inet_ntoa(*addr_list[i]));
			}

			printf("-------5 ok--------\n");
			//int err = connect(socket_file_descriptor, (SA *)&address, address.sin_len);

			int err;
			if ((err = connect(socket_file_descriptor, (struct sockaddr *)&address, sizeof(address))) != 0)
			{
				/*address.sin_port = htons(port++);
				vTaskDelay(500);
				printf("-------port number = %d--------\n", port);
				goto retry;*/
				printf("-------6 error--------\n");
				printf("Socket unable to connect, errno: %d\n", err);
				return -1;
			}

			printf("-------6 ok--------\n");
			printf("Socket connected!\n");
			//char* get_request = "Hello dear. Espressif talking..."; //GET / HTTPS/1.1\r\nHost:  www.google.com\r\n\r\n
			for(int i = 0; i <= 100; i++)
			{
			send(socket_file_descriptor, Payload, strlen(Payload), 0);

			vTaskDelay(1000/portTICK_PERIOD_MS);

			printf("data sent = ");
			printf(Payload);
			printf(" for the %u th time.\n", i);


			}

			//char buffer[1024];
			//recv(socket_file_descriptor, buffer, 1024, 0);
			//printf("received message: %s \r\n", buffer);
			vTaskDelay(5000/portTICK_PERIOD_MS);

			if (socket_file_descriptor != -1)
			{
				printf("closing socket...");
				shutdown(socket_file_descriptor, 0);
				close(socket_file_descriptor);
			}
		}
    }
	return 0;
}

void wifi_connection()
{
	esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, NULL);//creating event handler register for wifi
	esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, NULL);//creating event handler register for ip event

	strcpy((char*)wifi_configuration.sta.ssid,WIFI_SSID);
	strcpy((char*)wifi_configuration.sta.password,WIFI_PASS);
	esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_configuration);

	esp_wifi_start();
	esp_wifi_set_mode(WIFI_MODE_STA);

	printf("Connection to %s %s. \n",(char*)wifi_configuration.sta.ssid, (char*)wifi_configuration.sta.password);

	if (esp_wifi_connect() == ESP_OK)
		printf("Connected to %s. ",WIFI_SSID);
	else
		printf("Could not connect to %s. \n",WIFI_SSID);

	printf( "wifi_init_softap finished. SSID:%s  password:%s \n",WIFI_SSID,WIFI_PASS);

}
/*
static void WifiStillAlive(int32_t event_id)
{
	for (;;)
	{
		if (event_id == WIFI_EVENT_STA_CONNECTED || event_id == IP_EVENT_STA_GOT_IP)
		{
			printf("connected.");
			vTaskDelay(10000/portTICK_PERIOD_MS);
			wifi_conncted = true;
			retry_num = 0;
		}
		else if (event_id == WIFI_EVENT_STA_DISCONNECTED)
		{
			if(retry_num<10)
				{
					esp_wifi_connect();
					retry_num++;
					printf("Retrying to Connect...\n");
					wifi_conncted = false;
				}
		}
	}
}*/

static void ScanNetwork(void)
{

    sta_netif = esp_netif_create_default_wifi_sta();
    assert(sta_netif);

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

    uint16_t number = DEFAULT_SCAN_LIST_SIZE;
    //wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
    memset(ap_info, 0, sizeof(ap_info));

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_start());
    esp_wifi_scan_start(NULL, true);
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
    printf("  Total APs scanned = %u \n\n", ap_count);
    printf(" **************SSID************** |  Channel  |  RSSI\n");

    for (int i = 0; i < ap_count; i++)
    {
    	printf(" %31s  |  %7d  |  %4d  \n",
    			ap_info[i].ssid, ap_info[i].primary, ap_info[i].rssi);
    }

}

void app_main(void)
{
    // Initialize NVS
	ESP_ERROR_CHECK(esp_netif_init());

	esp_err_t ret = nvs_flash_init();
	if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
    {
	ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK( ret );
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    //initialize IO
    ScanNetwork();
    wifi_connection();
	//ESP_ERROR_CHECK(example_connect());

	init();
	xTaskCreate(rx_task, //the task function that needs a thread to run in freeRTOS
				"uart_rx_task", //the description of the task in debugging mode where you want to check the running tasks
				1024*2,  //the needed stack size of the function. for complex functions, such as wifi, we need more stack size.
				NULL, //the parameters that the function gets
				configMAX_PRIORITIES, //the task priority to run in parallel
				NULL); //the task handler for other tasks to interact between them.



	//xTaskCreate(WifiStillAlive, "WiFi Still Alive task", 1000, NULL, configMAX_PRIORITIES, NULL);

	tcp_client();

}