cancel
Showing results for 
Search instead for 
Did you mean: 

LwIP Ethernet based TCP/IP socket communication

shreya_awati
Associate II

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);
}
}
}
1 REPLY 1
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.