Arm Mbed OS support forum

Hardfault error mbed

Hi Every one:

When I apply my below program which is a TCP server sending UDP IP address to two UDP clients threads

#include "mbed.h"
#include "EthernetInterface.h"
#include <string> 
//#include "TCPServer.h" // not needed anymore
//#include "TCPSocket.h" // not needed anymore
char mbedIP[15];
char mbedMASK [15];
char mbedGATEWAY [15];
char* pFields[5];

Thread UDP_Socket_AITV01(osPriorityNormal, 2000);
Thread UDP_Socket_AITV02(osPriorityNormal, 2000);
Thread TCP_Server(osPriorityNormal, 2000);

Mutex Network_AITV01_Mutex;
Mutex Network_AITV02_Mutex;
Mutex RceiverIP_1_Mutex;
Mutex RceiverIP_2_Mutex;


// LEDs
DigitalOut led_1(D8);
DigitalOut led_2(D9);
DigitalOut led_3(D10);
DigitalOut led_4(D11);


EthernetInterface eth;

//static const char* recvIP_1;
static const char* recvIP_1 ;
static const char* recvIP_2 ;

void ParseFields(char* inputBuffer, char** pFields, uint32_t numFields, char* delimiterChars)
{
    char* pString = inputBuffer;
    char* pField;
    
    for(uint32_t i=0; i<numFields; i++)
    {
        pField = strtok(pString, delimiterChars);
 
        if(pField != NULL)
        {
            pFields[i] = pField;
        }
        else
        {
            pFields[i] = "";
        }
 
        pString = NULL; //to make strtok continue parsing the next field rather than start again on the original string (see strtok documentation for more details)
    }
   /* for(uint32_t i=0; i<numFields; i++)
    {
         printf("splitted Msg: %s\n\r", pFields[i]);  //this was missing in original example. 
    }*/
    
}

void UDP_Client_AITV01_Thread()
{
    printf("UDP_Thread_AITV01\n\r");   
    led_3=!led_3;
    
    //char Local_IP[15];
    static const char* Local_recvIP_1;

    char *sbuffer_AITV01 = new char[256];
    
    for(int i=0;i<256;i++)
    {
        sbuffer_AITV01[i]=NULL;
    //rxbuffer[i]=NULL;
    }
    
    RceiverIP_1_Mutex.lock();
    //sprintf(Local_IP,"%s\n\r",recvIP_1);
    Local_recvIP_1 = recvIP_1;
    RceiverIP_1_Mutex.unlock();
    //printf("IP1 is: %s\n\r", Local_recvIP_1);


    //SocketAddress td_addr_AITV01(Local_recvIP_1, 10110);
    Network_AITV01_Mutex.lock();
    UDPSocket td_sock_AITV01(&eth);
    Network_AITV01_Mutex.unlock();
    
    // Loop to send data to Socket address "td_sock"
    while(true)
    {
        
        RceiverIP_1_Mutex.lock();
        Local_recvIP_1 = recvIP_1;
        RceiverIP_1_Mutex.unlock();
        //printf("IP1 is: %s\n\r", Local_recvIP_1);
   
        SocketAddress td_addr_AITV01(Local_recvIP_1, 10110);
        sprintf(sbuffer_AITV01,"%s\n\r", "hello World");
        int ret_01 = td_sock_AITV01.sendto(td_addr_AITV01, sbuffer_AITV01, strlen(sbuffer_AITV01));
  
        ThisThread::sleep_for(1000);
      }
    
}

void UDP_Client_AITV02_Thread()
{
    printf("UDP_Thread_AITV02\n\r");   
    led_3=!led_3;
    
    static const char* Local_recvIP_2;
    char *sbuffer_AITV02 = new char[256];
    
    for(int i=0;i<256;i++)
    {
        sbuffer_AITV02[i]=NULL;
    //rxbuffer[i]=NULL;
    }
    
     RceiverIP_2_Mutex.lock();
    
    Local_recvIP_2 = recvIP_2;
    RceiverIP_2_Mutex.unlock();
    
    Network_AITV02_Mutex.lock();
    UDPSocket td_sock_AITV02(&eth);
    Network_AITV02_Mutex.unlock();
    
    // Loop to send data to Socket address "td_sock"
    while(true)
    {
        
        RceiverIP_2_Mutex.lock();
        Local_recvIP_2 = recvIP_2;
        RceiverIP_2_Mutex.unlock();
        //printf("IP2 is: %s\n\r", Local_recvIP_2);
        
        
        SocketAddress td_addr_AITV02(Local_recvIP_2, 10110);
        sprintf(sbuffer_AITV02,"%s\n\r", "hello Nada");
        int ret_02 = td_sock_AITV02.sendto(td_addr_AITV02, sbuffer_AITV02, strlen(sbuffer_AITV02));
        

        ThisThread::sleep_for(1000);
      }
    
}


//TCP Server Thread

void TCP_Server_Thread()
{
    printf("TCP server\r\n");

       //TCPServer srv;  //TCPServer was migrate to TCPSocket
    TCPSocket srv;
    TCPSocket *client_sock;  // srv.accept() will return pointer to socket
    SocketAddress client_addr;
    char *txbuffer = new char[256];
    char *rxbuffer = new char[256];
    for(int i=0;i<256;i++){
    txbuffer[i]=NULL;
    rxbuffer[i]=NULL;
    }
    //static const char* PB_IP_1;
    static const char* PB_IP_1;
    static const char* PB_IP_2;
    
    
    /* Open the server on ethernet stack */
    srv.open(&eth);
    
    /* Bind the HTTP port (TCP 80) to the server */
    srv.bind(eth.get_ip_address(), 4466);
    
    /* Can handle x simultaneous connections */
    srv.listen(5);
  
    while(true)
    {
        client_sock = srv.accept();  //return pointer of a client socket
        
        client_sock->getpeername(&client_addr);  //this will fill address of client to the SocketAddress object
        //printf("Accepted %s:%d\n\r", client_addr.get_ip_address(), client_addr.get_port());
        strcpy(txbuffer, "Hello \n\r");
        client_sock->send(txbuffer, strlen(txbuffer));
        client_sock->recv(rxbuffer, 256);
       // printf("Received Msg: %s\n\r", rxbuffer);  //this was missing in original example. 
        ParseFields(rxbuffer, pFields, 5, ",");
        //for(uint32_t i=0; i<5; i++)
       // {
            // printf("splitted Msg: %s\n\r", pFields[i]);  //this was missing in original example. 
       // }
        
        if (strcmp(pFields[4], "<EOF>") == 0)
        {
            if (strcmp(pFields[0], "Pinning-001") == 0)
            {
                if (strcmp(pFields[1], "start") == 0)
                {

                    if (strcmp(pFields[2], "1") == 0)
                    {
                        PB_IP_1 = pFields[3];
                        RceiverIP_1_Mutex.lock();
                        //sprintf(recvIP_1,"%s",PB_IP_1);
                        recvIP_1 = PB_IP_1;
                        //printf("IP TCP:%s\n\r",recvIP_1);
                        RceiverIP_1_Mutex.unlock();
                        
                    }
                    else if (strcmp(pFields[2], "2") == 0)
                    {
                        PB_IP_2 = pFields[3];
                        RceiverIP_2_Mutex.lock();
                        //sprintf(recvIP_1,"%s",PB_IP_1);
                        recvIP_2 = PB_IP_2;
                        //printf("IP TCP:%s\n\r",recvIP_1);
                        RceiverIP_2_Mutex.unlock();
                        

                    }
                }
            }
        }
        //delete[] rxbuffer;
        client_sock->close();
        ThisThread::sleep_for(1000);
 }
}
int main()
{
    printf("TCP server example\n\r");
    
    printf("Ethernet socket example\n\r");
    sprintf(mbedIP,"192.168.220.45");  
    sprintf(mbedMASK,"255.255.252.0");  
    sprintf(mbedGATEWAY,"192.168.220.1");     
    nsapi_error_t set_network=eth.set_network(mbedIP,mbedMASK,mbedGATEWAY);
    eth.connect();

    TCP_Server.start(TCP_Server_Thread);
    UDP_Socket_AITV01.start(UDP_Client_AITV01_Thread);
    UDP_Socket_AITV02.start(UDP_Client_AITV02_Thread);
     while(true)
     {
        led_1=!led_1;
        ThisThread::sleep_for(1000);
     }
    //delete[] txbuffer;
    // delete[] rxbuffer;
    
 }

I’m getting this error

Please your advise

Nada

Hello Nada,

it seems it is working without crash for me, on local network.
I only commented out the set_network because I am behind a router and also the IF statement with <EOF> because I am lazy .

Nucleo-F767ZI, MbedOs 5.15.7

BR, Jan

Thank you Jan for your reply.

I’m using Ublox-C027 and I will try again to do it. is there any idea why this happened to me and if the error is still there what I can add or delete.

Thank you really

Nada

Hi,

try to place some printfs across the code for check via serial terminal where the code stops (chrash).

BR, Jan

Actually I’ve already tried and once I connect one of the TCP clients, the program crashes. I will try to do something and I will keep you updated.

Thank you for your continuas help.

Nada

Dear Jan,

I still have the same problem.

in the program I’m sending the IP address to both UDP clients to send the msg to.

When I started one of the TCP clients, everything is fine but when I started the second one to send the second IP address the system crashes and show me the same msg.

Any other idea please

Regards
Nada

Is there anyway to use try and exception like c#. I read that it’s already disabled.

Any update regarding this.

Nada

Dear Nada,

according to your description I focused more to TCP server and now I also see the crash report.
I will let you know if I find something more. Currently I not found a logic, it seems to be random but I believe it is not.

BR, Jan

Thank you so much Jan.

I will also try at the moment to solve it and I will also keep you updated.

Regards
Nada

Dear Nada,

I do not know how your TCP clients are working but it seems it can be caused by multiple attempt of connect at the same time, during processing one already connected client.

BR, Jan

Dear Jan,

My TCP will accept more than one client. I tried the TCP server alone. and it was working.

I don’t know if in the UDP client, I should put the socket connection out of the loop.

I will try another C# program for the TCP client and let me see.

Thank you Jan.

Regards
Nada

Dear Jan,

I want to ask you if you know an example for multiclient TCP server. I was using one which was accepting more than one client but now it deosn’t.

Regards
Nada

Dear Nada,

you can not process multiple clients at the same time with this code. Because your TCP server accept a client and wait for a data. After data are received and processed then is socket closed (+ 1s delay in your code) and after that is Server able to process another client but not before.

Possible solution can be accept a client (client must send data immediately ) and close the client socket. Maybe data can be passed to a event queue or a thread for processing without any braking of the TCP server.

BR, Jan

Thank you so much Jan,

I will try to send the data from both clients and then pass the data to be splitted and processed in another thread. or using the queue .

I will keep you updated

Appreciet you help
Nada

Dear Jan,

I’m now using the event queue and I was testing the program step by step. everything was OK and I’m recieving from both TCP clients but when I want to start sending the UDP packet, with one TCP connection it worked but when I tried to connect the other TCP clients, the system crashed and the same error message appears to me.

The whole idea is the TCP clients will send two different IP addresses to start two different UDP sockets.

Please find below my modified program

#include "mbed.h"
#include "EthernetInterface.h"
#include <string> 
//#include "TCPServer.h" // not needed anymore
//#include "TCPSocket.h" // not needed anymore
char mbedIP[15];
char mbedMASK [15];
char mbedGATEWAY [15];
char* pFields[5];

Thread UDP_Socket_AITV01(osPriorityNormal, 2000);
Thread UDP_Socket_AITV02(osPriorityNormal, 2000);
Thread TCP_Server(osPriorityNormal, 2000);

Mutex Network_AITV01_Mutex;
Mutex Network_AITV02_Mutex;
Mutex Network_Mutex;
Mutex RceiverIP_1_Mutex;
Mutex RceiverIP_2_Mutex;


// LEDs
DigitalOut led_1(D8);
DigitalOut led_2(D9);
DigitalOut led_3(D10);
DigitalOut led_4(D11);


EthernetInterface eth;

typedef struct {
    char buffer[256];
} message_t;

MemoryPool<message_t, 16> mpool;
Queue<message_t, 16> queue;

message_t *message;

//static const char* recvIP_1;
static const char* recvIP_1 ;
static const char* recvIP_2 ;
//static const char* recvIP = "192.168.220.100";


void ParseFields(char* inputBuffer, char** pFields, uint32_t numFields, char* delimiterChars)
{
    char* pString = inputBuffer;
    char* pField;
    
    for(uint32_t i=0; i<numFields; i++)
    {
        pField = strtok(pString, delimiterChars);
 
        if(pField != NULL)
        {
            pFields[i] = pField;
        }
        else
        {
            pFields[i] = "";
        }
 
        pString = NULL; //to make strtok continue parsing the next field rather than start again on the original string (see strtok documentation for more details)
    }
   /* for(uint32_t i=0; i<numFields; i++)
    {
         printf("splitted Msg: %s\n\r", pFields[i]);  //this was missing in original example. 
    }*/
    
}

void UDP_Client_AITV01_Thread()
{
    printf("UDP_Thread_AITV01\n\r");   
    led_3=!led_3;
    
    //char Local_IP[15];
    static const char* Local_recvIP_1;

    char *sbuffer_AITV01 = new char[256];
    
    for(int i=0;i<256;i++)
    {
        sbuffer_AITV01[i]=NULL;
    //rxbuffer[i]=NULL;
    }
    


    //SocketAddress td_addr_AITV01(Local_recvIP_1, 10110);
    Network_AITV01_Mutex.lock();
    UDPSocket td_sock_AITV01(&eth);
    Network_AITV01_Mutex.unlock();
    
    // Loop to send data to Socket address "td_sock"
    while(true)
    {
        
        RceiverIP_1_Mutex.lock();
        Local_recvIP_1 = recvIP_1;
        RceiverIP_1_Mutex.unlock();
        //printf("IP1 is: %s\n\r", Local_recvIP_1);
   
        SocketAddress td_addr_AITV01(Local_recvIP_1, 10110);
        sprintf(sbuffer_AITV01,"%s\n\r", "hello World");
        int ret_01 = td_sock_AITV01.sendto(td_addr_AITV01, sbuffer_AITV01, strlen(sbuffer_AITV01));
        
        if(ret_01 < 0)
        {
            led_1 = !led_1;
        }
  
        ThisThread::sleep_for(1000);
      }
    
}

void UDP_Client_AITV02_Thread()
{
    printf("UDP_Thread_AITV02\n\r");   
    led_3=!led_3;
    
    static const char* Local_recvIP_2;
    char *sbuffer_AITV02 = new char[256];
    
    for(int i=0;i<256;i++)
    {
        sbuffer_AITV02[i]=NULL;
    //rxbuffer[i]=NULL;
    }
    

    Network_AITV02_Mutex.lock();
    UDPSocket td_sock_AITV02(&eth);
    Network_AITV02_Mutex.unlock();
    
    // Loop to send data to Socket address "td_sock"
    while(true)
    {
        
        RceiverIP_2_Mutex.lock();
        Local_recvIP_2 = recvIP_2;
        RceiverIP_2_Mutex.unlock();
        //printf("IP2 is: %s\n\r", Local_recvIP_2);
        
        
        SocketAddress td_addr_AITV02(Local_recvIP_2, 10111);
        sprintf(sbuffer_AITV02,"%s\n\r", "hello Nada");
        int ret_02 = td_sock_AITV02.sendto(td_addr_AITV02, sbuffer_AITV02, strlen(sbuffer_AITV02));
        if(ret_02 < 0)
        {
            led_4 = !led_4;

        }

        ThisThread::sleep_for(1000);
        
      }
    
}


//TCP Server Thread

void TCP_Server_Thread()
{
    printf("TCP server\r\n");

       //TCPServer srv;  //TCPServer was migrate to TCPSocket
    TCPSocket srv;
    TCPSocket *client_sock;  // srv.accept() will return pointer to socket
    SocketAddress client_addr;
    char *txbuffer = new char[256];
    char *rxbuffer = new char[256];
    for(int i=0;i<256;i++){
    txbuffer[i]=NULL;
    rxbuffer[i]=NULL;
    }

    
    
    //message_t *message;
    
    /* Open the server on ethernet stack */
    Network_Mutex.lock();
    srv.open(&eth);
    Network_Mutex.unlock();
    
    /* Bind the HTTP port (TCP 80) to the server */
    Network_Mutex.lock();
    srv.bind(eth.get_ip_address(), 4466);
    Network_Mutex.unlock();
    /* Can handle x simultaneous connections */
    srv.listen(5);
  
    while(true)
    {
        client_sock = srv.accept();  //return pointer of a client socket
        
        client_sock->getpeername(&client_addr);  //this will fill address of client to the SocketAddress object
        //printf("Accepted %s:%d\n\r", client_addr.get_ip_address(), client_addr.get_port());
        strcpy(txbuffer, "Hello \n\r");
        client_sock->send(txbuffer, strlen(txbuffer));
        client_sock->recv(rxbuffer, 256);
        message_t *message = mpool.alloc();

        strcpy(message->buffer, rxbuffer);
        
        queue.put(message);

        client_sock->close();
        ThisThread::sleep_for(1000);
 }
}
int main()
{
   
   // printf("TCP server example\n\r");
        //static const char* PB_IP_1;
    static const char* PB_IP_1;
    static const char* PB_IP_2;
    //printf("Ethernet socket example\n\r");
    sprintf(mbedIP,"192.168.220.45");  
    sprintf(mbedMASK,"255.255.252.0");  
    sprintf(mbedGATEWAY,"192.168.220.1");     
    nsapi_error_t set_network=eth.set_network(mbedIP,mbedMASK,mbedGATEWAY);
    eth.connect();

    TCP_Server.start(TCP_Server_Thread);
    UDP_Socket_AITV01.start(callback(UDP_Client_AITV01_Thread));
    UDP_Socket_AITV02.start(callback(UDP_Client_AITV02_Thread));
     while(true)
     {
        osEvent evt = queue.get();
        if (evt.status == osEventMessage) 
        {
            message_t *message = (message_t*)evt.value.p;
            //printf("Received Msg: %s\n\r", message->buffer);  //this was missing in original example. 
            ParseFields(message->buffer, pFields, 5, ",");
            for(uint32_t i=0; i<5; i++)
            {
                printf("splitted Msg: %s\n\r", pFields[i]);  //this was missing in original example. 
            }
               if (strcmp(pFields[4], "<EOF>") == 0)
                {
                    if (strcmp(pFields[0], "Pinning-001") == 0)
                    {
                        if (strcmp(pFields[1], "start") == 0)
                        {
        
                            if (strcmp(pFields[2], "1") == 0)
                            {
                                PB_IP_1 = pFields[3];
                                RceiverIP_1_Mutex.lock();
                                //sprintf(recvIP_1,"%s",PB_IP_1);
                                recvIP_1 = PB_IP_1;
                                printf("IP TCP01:%s\n\r",recvIP_1);
                                RceiverIP_1_Mutex.unlock();
                                
                            }
                            if (strcmp(pFields[2], "2") == 0)
                            {
                                PB_IP_2 = pFields[3];
                                RceiverIP_2_Mutex.lock();
                                //sprintf(recvIP_1,"%s",PB_IP_1);
                                recvIP_2 = PB_IP_2;
                                printf("IP TCP02:%s\n\r",recvIP_2);
                                RceiverIP_2_Mutex.unlock();
                                
        
                            }
                        }
                    }
                }
            mpool.free(message);
        }
        led_1=!led_1;
        ThisThread::sleep_for(1000);
     }
    //delete[] txbuffer;
    // delete[] rxbuffer;
    
 }

Could you please help me , maybe you can find what am doing wrong.

Regards
Nada

Should I close the udp client socket???

Dear Jan, thank you for your help. I solved my issue already.

Now I need to handle 4 TCP connection. when I connect the 4 TCP client am getting the same error:


I’ve already changed the MEMP_NUM_NETCONN in the opt.h file from 4 to 6 and I added the mbed_app.json file which contain these information:

{
“target_overrides”: {
“*”: {
“platform.stdio-convert-newlines”: true,
“target.network-default-interface-type”: “ETHERNET”,
“lwip.socket-max”: 6,
“lwip.tcp-socket-max”: 6
}
}
}

My modified program below and I’ve already deactivated the UDP clients thread for now:

#include "mbed.h"
#include "EthernetInterface.h"
#include <string> 
//#include "TCPServer.h" // not needed anymore
//#include "TCPSocket.h" // not needed anymore
char mbedIP[15];
char mbedMASK [15];
char mbedGATEWAY [15];
char* pFields[5];

Thread UDP_Socket_AITV01(osPriorityNormal, 2000);
Thread UDP_Socket_AITV02(osPriorityNormal, 2000);
Thread TCP_Server(osPriorityNormal, 2000);
Thread Process_TCP(osPriorityNormal, 2000);

Mutex Network_AITV01_Mutex;
Mutex Network_AITV02_Mutex;
Mutex Network_Mutex;
Mutex RceiverIP_1_Mutex;
Mutex RceiverIP_2_Mutex;
Mutex RceiverIP_3_Mutex;
Mutex RceiverIP_4_Mutex;



// LEDs
DigitalOut led_1(D8);
DigitalOut led_2(D9);
DigitalOut led_3(D10);
DigitalOut led_4(D11);


EthernetInterface eth;

typedef struct {
    char buffer[256];
} message_t;

MemoryPool<message_t, 16> mpool;
Queue<message_t, 16> queue;

message_t *message;

//static const char* recvIP_1;
static const char* recvIP_1 ;
static const char* recvIP_2 ;
static const char* recvIP_3 ;
static const char* recvIP_4 ;
//static const char* recvIP = "192.168.220.100";


void ParseFields(char* inputBuffer, char** pFields, uint32_t numFields, char* delimiterChars)
{
    char* pString = inputBuffer;
    char* pField;
    
    for(uint32_t i=0; i<numFields; i++)
    {
        pField = strtok(pString, delimiterChars);
 
        if(pField != NULL)
        {
            pFields[i] = pField;
        }
        else
        {
            pFields[i] = "";
        }
 
        pString = NULL; //to make strtok continue parsing the next field rather than start again on the original string (see strtok documentation for more details)
    }
   /* for(uint32_t i=0; i<numFields; i++)
    {
         printf("splitted Msg: %s\n\r", pFields[i]);  //this was missing in original example. 
    }*/
    
}

void Processing_TCP_DATA_Thread()
{
    printf("Start Process\n\r");
    static const char* PB_IP_1;
    static const char* PB_IP_2;
    static const char* PB_IP_3;
    static const char* PB_IP_4;
    while(true)
    {
        osEvent evt = queue.get();
        
        if (evt.status == osEventMessage) 
        {
            message_t *message = (message_t*)evt.value.p;
            //printf("Received Msg: %s\n\r", message->buffer);  //this was missing in original example. 
            ParseFields(message->buffer, pFields, 6, ",");
            for(uint32_t i=0; i<6; i++)
            {
                printf("splitted Msg: %s\n\r", pFields[i]);  //this was missing in original example. 
            }
               if (strcmp(pFields[5], "<EOF>") == 0)
                {
                    if (strcmp(pFields[0], "Pinning-001") == 0)
                    {
                        if (strcmp(pFields[1], "start") == 0)
                        {
        
                            if (strcmp(pFields[2], "1") == 0)
                            {
                                if(strcmp(pFields[3], "1") == 0)
                                {
                                    PB_IP_1 = pFields[4];
                                    RceiverIP_1_Mutex.lock();
                                    //sprintf(recvIP_1,"%s",PB_IP_1);
                                    recvIP_1 = PB_IP_1;
                                    printf("IP TCP01:%s\n\r",recvIP_1);
                                    RceiverIP_1_Mutex.unlock();
                                }
                                if(strcmp(pFields[3], "2") == 0)
                                {
                                    PB_IP_2 = pFields[4];
                                    RceiverIP_2_Mutex.lock();
                                    //sprintf(recvIP_1,"%s",PB_IP_1);
                                    recvIP_2 = PB_IP_2;
                                    printf("IP TCP02:%s\n\r",recvIP_2);
                                    RceiverIP_2_Mutex.unlock();
                                }
                                
                            }
                            if (strcmp(pFields[2], "2") == 0)
                            {
                                if(strcmp(pFields[3], "1") == 0)
                                {
                                    PB_IP_3 = pFields[4];
                                    RceiverIP_3_Mutex.lock();
                                    //sprintf(recvIP_1,"%s",PB_IP_1);
                                    recvIP_3 = PB_IP_3;
                                    printf("IP TCP03:%s\n\r",recvIP_3);
                                    RceiverIP_3_Mutex.unlock();
                                }
                                if(strcmp(pFields[3], "2") == 0)
                                {
                                    PB_IP_4 = pFields[4];
                                    RceiverIP_4_Mutex.lock();
                                    //sprintf(recvIP_1,"%s",PB_IP_1);
                                    recvIP_4 = PB_IP_4;
                                    printf("IP TCP04:%s\n\r",recvIP_4);
                                    RceiverIP_4_Mutex.unlock();
                                }
                                
        
                            }
                        }
                    }
                }
            mpool.free(message);
        }
    }
}

void UDP_Client_AITV01_Thread()
{
    printf("UDP_Thread_AITV01\n\r");   
    led_3=!led_3;
    
    //char Local_IP[15];
    static const char* Local_recvIP_1;

    char *sbuffer_AITV01 = new char[256];
    
    for(int i=0;i<256;i++)
    {
        sbuffer_AITV01[i]=NULL;
    //rxbuffer[i]=NULL;
    }

    
    // Loop to send data to Socket address "td_sock"
    while(true)
    {
        led_1 = !led_1;
        Network_AITV01_Mutex.lock();
        UDPSocket td_sock_AITV01(&eth);
        Network_AITV01_Mutex.unlock();
        
        RceiverIP_1_Mutex.lock();
        Local_recvIP_1 = recvIP_1;
        RceiverIP_1_Mutex.unlock();
        printf("IP1 is: %s\n\r", Local_recvIP_1);
   
        SocketAddress td_addr_AITV01(Local_recvIP_1, 10110);
        sprintf(sbuffer_AITV01,"%s\n\r", "hello World");
        int ret_01 = td_sock_AITV01.sendto(td_addr_AITV01, sbuffer_AITV01, strlen(sbuffer_AITV01));
        
        td_sock_AITV01.close();
        ThisThread::sleep_for(1000);
      }
    
}

void UDP_Client_AITV02_Thread()
{
    printf("UDP_Thread_AITV02\n\r");   
    //led_3=!led_3;
    
    static const char* Local_recvIP_2;
    char *sbuffer_AITV02 = new char[256];
    
    for(int i=0;i<256;i++)
    {
        sbuffer_AITV02[i]=NULL;
    }

    
    // Loop to send data to Socket address "td_sock"
    while(true)
    {
        led_4 = !led_4;
        Network_AITV02_Mutex.lock();
        UDPSocket td_sock_AITV02(&eth);
        Network_AITV02_Mutex.unlock();
        
        RceiverIP_2_Mutex.lock();
        Local_recvIP_2 = recvIP_2;
        RceiverIP_2_Mutex.unlock();
        printf("IP2 is: %s\n\r", Local_recvIP_2);
        
        
        SocketAddress td_addr_AITV02(Local_recvIP_2, 10111);
        sprintf(sbuffer_AITV02,"%s\n\r", "hello Nada");
        int ret_02 = td_sock_AITV02.sendto(td_addr_AITV02, sbuffer_AITV02, strlen(sbuffer_AITV02));
     
        td_sock_AITV02.close();
        
        ThisThread::sleep_for(1000);
        
      }
    
}


//TCP Server Thread

void TCP_Server_Thread()
{
    printf("TCP server\r\n");

       //TCPServer srv;  //TCPServer was migrate to TCPSocket
    TCPSocket srv;
    TCPSocket *client_sock;  // srv.accept() will return pointer to socket
    SocketAddress client_addr;
    char *txbuffer = new char[256];
    char *rxbuffer = new char[256];
    for(int i=0;i<256;i++){
    txbuffer[i]=NULL;
    rxbuffer[i]=NULL;
    }

    
    
    //message_t *message;
    
    /* Open the server on ethernet stack */
    Network_Mutex.lock();
    srv.open(&eth);
    Network_Mutex.unlock();
    
    /* Bind the HTTP port (TCP 80) to the server */
    Network_Mutex.lock();
    srv.bind(eth.get_ip_address(), 4466);
    Network_Mutex.unlock();
    /* Can handle x simultaneous connections */
    srv.listen(5);
  
    while(true)
    {
        client_sock = srv.accept();  //return pointer of a client socket
        
        client_sock->getpeername(&client_addr);  //this will fill address of client to the SocketAddress object
        printf("Accepted %s:%d\n\r", client_addr.get_ip_address(), client_addr.get_port());
        strcpy(txbuffer, "Hello \n\r");
        client_sock->send(txbuffer, strlen(txbuffer));
        client_sock->recv(rxbuffer, 256);
        led_2 = !led_2;
        message_t *message = mpool.alloc();

        strcpy(message->buffer, rxbuffer);
        
        queue.put(message);

        client_sock->close();
        ThisThread::sleep_for(1000);
 }
}
int main()
{

    static const char* PB_IP_1;
    static const char* PB_IP_2;
    //printf("Ethernet socket example\n\r");
    sprintf(mbedIP,"192.168.220.45");  
    sprintf(mbedMASK,"255.255.252.0");  
    sprintf(mbedGATEWAY,"192.168.220.1");     
    nsapi_error_t set_network=eth.set_network(mbedIP,mbedMASK,mbedGATEWAY);
    eth.connect();

    TCP_Server.start(TCP_Server_Thread);
    //UDP_Socket_AITV01.start(UDP_Client_AITV01_Thread);
    //UDP_Socket_AITV02.start(UDP_Client_AITV02_Thread);
    Process_TCP.start(Processing_TCP_DATA_Thread);
    while(true)
     {
        led_1=!led_1;
        ThisThread::sleep_for(1000);
     }
    //delete[] txbuffer;
    // delete[] rxbuffer;
    
 }

Please your help and support again will be apprecieted .

Regards
Nada

Hello,

I’m sorry I didn’t have much time.

Here you can found what it can be override in mbed_app.json for LWIP.
It seems you need to set socket-max according to sum of :

  • 2 for UDP
  • 1 TCP for server - maybe, I am not sure
  • X for TCP clients
  • +1 for Internal for DNS how is written in the help of “socket-max”

So maybe you need something like this

{
    "target_overrides": {
        "*": {
            "platform.stdio-convert-newlines": true,
            "target.network-default-interface-type": "ETHERNET",
            "lwip.socket-max": 8,
            "lwip.tcp-socket-max": 6,
            "lwip.udp-socket-max":2
        }
    }
}

From what I tested it seems, it will crash every time when you reach max socket -1.

For the rest of code. Maybe decrease the delay time in TCP server thread to something smaller like 100ms. Because you want to process clients as soon as possible and not wanting 1s between every one.

BR, Jan

Thank you som much Jan,

I will try to do that,. yes the code crashes when I reches socket -1. I hope that will solve the whole issue.

Really appreaciete your continous help

Regards
Nada

Sorry for my late reply Jan.

Thank you so much. my program working fine till now.

Really appreciete your support.

Regards
Nada