cancel
Showing results for 
Search instead for 
Did you mean: 

LwIP Ethernet based TCP/IP socket communication

shreya_awati
Associate III

Post edited by ST moderator mainly for the code sharing.

i want to use simple socket based communication . using socket API.

nucleo-F746ZG connected directly to the linux laptop through ethernet cable (Orange led is ON , on both side). i make controller as client and laptop as server . my server is always listen mode but my controller not able to send any data .

In Freertos option i didnt changes anything . enabled as CMSIS_v2. 

shreya_awati_0-1780551352834.png

 



LWIP enables . in that DHCP disabled 

shreya_awati_1-1780551421373.pngshreya_awati_2-1780551439678.pngshreya_awati_3-1780551454303.pngshreya_awati_4-1780551461685.png



shreya_awati_5-1780551474780.png



Ethernet is enabled . didnt changed anything . 

shreya_awati_6-1780551645212.png



Clock configuration 

shreya_awati_7-1780551708965.png


tcp_clinet.c 

/*

* tcp_client.c

*

* Created on: 02-Jun-2026

* Author: sawati

*/



#include "lwip/sockets.h" /* socket, connect, send, recv */

#include "lwip/netdb.h" /* inet_addr */

#include "cmsis_os.h" /* osDelay */

#include <string.h>

#include <stdio.h>

#include "tcp_client.h"



#define SERVER_IP "169.254.0.220" /* your laptop IP */

#define SERVER_PORT 5000



void TCP_Client_Task(void)

{

/* client code here */



HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_2); // or your LED pin

HAL_Delay(500);



int sock;



struct sockaddr_in server_addr;



char msg[] = "Hello Laptop";



sock = socket(AF_INET,SOCK_STREAM,0);



if(sock < 0)

{

return;

}



server_addr.sin_family = AF_INET;

server_addr.sin_port = htons(5001);



server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);



if(connect(sock,(struct sockaddr *)&server_addr,sizeof(server_addr)) < 0)

{

closesocket(sock);

return;

}



while(1)

{

send(sock, msg, strlen(msg), 0);



HAL_Delay(1000);

}

}



server.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

int main()
{
int server_fd;
int client_fd;

struct sockaddr_in server_addr;
struct sockaddr_in client_addr;

char buffer[100];

socklen_t len;

server_fd = socket(AF_INET, SOCK_STREAM, 0);

server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(5001);

bind(server_fd,
(struct sockaddr *)&server_addr,sizeof(server_addr));

listen(server_fd, 1);

printf("Waiting for STM32...\n");

len = sizeof(client_addr);

client_fd = accept(server_fd,(struct sockaddr *)&client_addr,&len);

printf("STM32 Connected\n");

while(1)
{
memset(buffer,0,sizeof(buffer));

int n = recv(client_fd,buffer,sizeof(buffer)-1,0);

if(n > 0)
{
printf("Received: %s\n",buffer);
}
}
}
3 REPLIES 3
Andrew Neil
Super User

Welcome to the forum

Please see How to write your question to maximize your chances to find a solution for best results.

In particular, please see How to post source code - you need to use the </> button on the toolbar.

 

Implementing a LwIP and FreeRTOS™ v1 UDP echo server on the STM32F7 series

 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

[Part Number] :

STM32-NUCLEO-F746ZG

Ethernet cable

Linux laptop :  

PRETTY_NAME="Ubuntu 22.04.3 2024.11.13 LTS (Cubic 2024-11-14 12:23)"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.5 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

 [Environment] :

STM32_MX : 6.17.0

STM32_IDE : 2.1.1

 

[Schematics] : Refer from google.

 

What i am trying to do :  TCP/IP Socket communication . 

I have NUCLEO-F746 controller and linux laoptop .

Both connected through Ethernet cable. 

i want NUCLEO-F746ZG as client and laptop as server .

shreya_awati_3-1780638147092.pngshreya_awati_4-1780638241969.png

 

 

 

[Details]  : I want to use socket API .
socket() , bind() , listen() , accept() , connect() , send() , recv() ,close()

For use above API's i enabled FREERTOS in MX and then LWIP_SOCKET option is automatically enabled.

but i added USART3 for debugging , so on minicom terminal print is not coming.

 

But when i disable FREERTOS that time USART3 print is displyed on minicom terminal , but not able to enable  LWIP_SOCKET option in MX .

shreya_awati_0-1780638063264.png

shreya_awati_1-1780638085283.png

shreya_awati_2-1780638118794.png

 

 

[Expected behavior] : simple socket based communication 

Server is in listen mode . client can be connect and simple communication is done .

 

[How to reproduce]  : above i shared steps to" what i trying to do" you can follow those steps.

client .c
	  int sock;
	  struct sockaddr_in server_addr;

	  char tx_buffer[100];
	  char rx_buffer[100];
	  strcpy (msg , "before Socket created\r\n");
	  	 	  	      HAL_UART_Transmit(&huart3,
	  	 	  	                        (uint8_t*)msg,
	  	 	  	                        strlen(msg),
	  	 	  	                        HAL_MAX_DELAY);

	  sock = socket(AF_INET, SOCK_STREAM, 0);

	  strcpy (msg , "after Socket created\r\n");
	  	 	  	      HAL_UART_Transmit(&huart3,
	  	 	  	                        (uint8_t*)msg,
	  	 	  	                        strlen(msg),
	  	 	  	                        HAL_MAX_DELAY);

	  if(sock < 0)
	  {
	      char msg[] = "Socket Failed\r\n";
	      HAL_UART_Transmit(&huart3,(uint8_t*)msg,strlen(msg),HAL_MAX_DELAY);
	  }

	  server_addr.sin_family = AF_INET;
	  server_addr.sin_port   = htons(5000);

	  inet_aton("169.254.0.220",&server_addr.sin_addr);

	  strcpy (msg ,"before connect created\r\n");
	  	 	  	      HAL_UART_Transmit(&huart3,
	  	 	  	                        (uint8_t*)msg,
	  	 	  	                        strlen(msg),
	  	 	  	                        HAL_MAX_DELAY);

	  if(connect(sock,(struct sockaddr*)&server_addr,sizeof(server_addr)) < 0)
	  {
	      char msg[] = "Connect Failed\r\n";
	      HAL_UART_Transmit(&huart3,
	                        (uint8_t*)msg,
	                        strlen(msg),
	                        HAL_MAX_DELAY);
	  }
	  else
	  {
	      char msg[] = "Connected\r\n";
	      HAL_UART_Transmit(&huart3,
	                        (uint8_t*)msg,
	                        strlen(msg),
	                        HAL_MAX_DELAY);
	  }

	  while(1)
	  {
	      strcpy(tx_buffer, "Hello From STM32");

	      send(sock,
	           tx_buffer,
	           strlen(tx_buffer),
	           0);

	      int len = recv(sock,
	                     rx_buffer,
	                     sizeof(rx_buffer)-1,
	                     0);

	      if(len > 0)
	      {
	          rx_buffer[len] = '\0';

	          HAL_UART_Transmit(&huart3,
	                            (uint8_t*)rx_buffer,
	                            strlen(rx_buffer),
	                            HAL_MAX_DELAY);

	          HAL_UART_Transmit(&huart3,
	                            (uint8_t*)"\r\n",
	                            2,
	                            HAL_MAX_DELAY);
	      }

	      HAL_Delay(1000);
	  }


server.py
# server.py
import socket

HOST = "169.254.0.220"   # listen on all interfaces
PORT = 5000

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((HOST, PORT))
    s.listen(5)
    print(f"Listening on port {PORT} ...")
    while True:
        conn, addr = s.accept()
        with conn:
            print(f"Connected from {addr}")
            while True:
                data = conn.recv(1024)
                if not data:
                    break
                print(f"Received: {data.decode(errors='replace')}", end="")
                conn.sendall(b"ACK\r\n")   # optional reply

 

 

want to know : 

I read we can use 3 types of API's 
Socket()

Netconn

Raw 

How can i identify which one will select ?? 


i uopdated query format , which you shared with me . can you please check my query ??