cancel
Showing results for 
Search instead for 
Did you mean: 

Hello ! I am trying to start an echo server, parallel to TCP and UDP. When I start them separately - it works. When two separate threads are started - TCP immediately rejects connections and UDP does not respond. How should I do it?

TDrzy
Associate II
int main(void)
{
   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  SystemClock_Config();
 
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_SPI3_Init();
  MX_USART1_UART_Init();
 
  osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
 
  osKernelStart();
  
  while (1) {
 
  }
 
}
 
int main(void)
{
   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  SystemClock_Config();
 
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_SPI3_Init();
  MX_USART1_UART_Init();
 
  osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
 
  osKernelStart();
  
  while (1) {
 
  }
 
}
 
/* USER CODE BEGIN 4 */
static void Echo_TCP(void) {
	struct netconn *connection, *newconnection;
	err_t err, accept_err;
	struct netbuf* buffer;
	void* data;
	u16_t len;
	err_t recv_err;
	//Makro tworzące nową strukturę oraz inuicjalizyjące ją nowymi wartościami.
	connection = netconn_new(NETCONN_TCP);
	if (connection != NULL) {
		err = netconn_bind(connection, NULL, 6610); //Przypisz połączenie do portu 6610
		if (err == ERR_OK) {
			netconn_listen(connection); //Połączenie zamienione na tryb słuchania
			while (1) {
				//Funkcja oczekujaca na polaczenie od klienta
				accept_err = netconn_accept(connection, &newconnection);
				//Obsluga polaczenia
				if (accept_err == ERR_OK) {
					while ((recv_err = netconn_recv(newconnection, &buffer))
							== ERR_OK) {
						do {
							netbuf_data(buffer, &data, &len);
							HAL_GPIO_TogglePin(PK_1_GPIO_Port, PK_1_Pin);
							netconn_write(newconnection, data, len, NETCONN_COPY);
						} while (netbuf_next(buffer) >= 0);
						netbuf_delete(buffer);
					}
					//Zamknij polaczenie
					netconn_close(newconnection);
					netconn_delete(newconnection);
				}
				osDelay(10);
			}
		} else {
			netconn_delete(newconnection);
		}  //Nie udalo się nawiazac polaczenia
	}
}
 
static void Echo_UDP(void){
	struct netconn *connection;
	struct netbuf* buffer;
	static ip_addr_t *addr;
	static unsigned short port;
	char bufor[100];
	err_t err;
 
	connection = netconn_new(NETCONN_UDP);
	if(connection != NULL){
		err = netconn_bind(connection, NULL, 5109);
		if(err == ERR_OK){
			while(1){
				err = netconn_recv(connection, &buffer);
				if(err == ERR_OK){
					addr = netbuf_fromaddr(buffer);
					port = netbuf_fromport(buffer);
					netconn_connect(connection, addr, port);
					netbuf_copy(buffer, bufor, buffer->p->tot_len);
					bufor[buffer->p->tot_len] = '\0';
					netconn_send(connection, buffer);
					netbuf_delete(buffer);
				}
				osDelay(10);
			}
		}
	}
}
 
/* USER CODE END 4 */
 
/* StartDefaultTask function */
void StartDefaultTask(void const * argument)
{
  /* init code for LWIP */
  MX_LWIP_Init();
 
  /* USER CODE BEGIN 5 */
//  Echo_TCP();
//  Echo_UDP();
 
  /* definition and creation of taskTCP */
  osThreadDef(taskTCP, taskTCP_Start, osPriorityNormal, 0, 128);
  taskTCPHandle = osThreadCreate(osThread(taskTCP), NULL);
 
  /* definition and creation of taskUDP */
  osThreadDef(taskUDP, taskUDP_Start, osPriorityNormal, 0, 128);
  taskUDPHandle = osThreadCreate(osThread(taskUDP), NULL);
 
//  vTaskDelete(defaultTaskHandle);
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END 5 */ 
}
 
/* taskTCP_Start function */
void taskTCP_Start(void const * argument)
{
  /* USER CODE BEGIN taskTCP_Start */
	Echo_TCP();
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END taskTCP_Start */
}
 
/* taskUDP_Start function */
void taskUDP_Start(void const * argument)
{
  /* USER CODE BEGIN taskUDP_Start */
	Echo_UDP();
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END taskUDP_Start */
}

1 REPLY 1
Winfred LU
ST Employee

Maybe increase the MEMP_NUM_NETCONN definition in LwIP ?