lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-users] TCP connection problem


From: flavio
Subject: [lwip-users] TCP connection problem
Date: Fri, 12 Jul 2013 12:36:48 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

Please, can someone help me ?

I'm using lwIP 1.3.2 (NO_SYS=1) on LM3S9B96 Stellaris Cortex M3 and
want to use the controller working as a client.

My implementation works fine while the server (PC) is waiting for the
connection, and the client (my controller) connects, read (and write)
datas and close the connection.
I can do this many times and it works fine.

The problem occours in this cases:

1) If the client try to connect .... but the server isn't waiting the
connection. Then the server is actived (to wait connection) and the
client connects. After a time, if client disconnects and try to
connect again, the program crash.

2) If the cable is unplugged ... and the client try to connect. After
the cable is plugged and the client connects. After a time, if client
disconnects and try to connect again, the program crash.

In my program, when an error of connection is detected ... 
ModTCPslave_Close()
is called (see the code). 

Always tcp_new() return a pointer and tcp_connect() return ERR_OK. 

What am I doing wrong or additonal things do I need to do ?


Below is a piece of the code:


//-------------------------------------------------------------------------
struct tcp_pcb *pcbMBUS;
struct ip_addr *ipaddr_con_MBUS, IPADDR_CON_MBUS;
unsigned int error;
unsigned char Conexao;
unsigned char passo;
unsigned short TCPAT, PORTA;


//-------------------------------------------------------------------------
void MBUS__Init(void)
{

  PORTA = 501;
  time_ModTCPslave    = 0;
  passo               = 0;
  IndexMBUS           = 0;
  flgRecebeuDadosMBUS = 0;
  pcbMBUS = NULL;

  Conexao = 0;
  TCPAT   = 0;
}


//-------------------------------------------------------------------------
// This function is called cyclically
//-------------------------------------------------------------------------
void MBUS__Trat(void)
{
  unsigned short i;


  if (TCPAT == 1)               //Start
  {
    ModTCPslave_Close();

    TCPAT = 2;
    time_ModTCPslave = 0;
    ModTCPslave_cont_tempo_sem_pacotes_Rx = 0;
    passo = 0;
  }
  else
  {
    if (TCPAT == 2)             //Try to connect ?
    {
        if (!passo)
        {                                       
          passo = 1;

          pcbMBUS = tcp_new();

          IPADDR_CON_MBUS.addr = htonl(0xC0A83262);     //IP 192.168.50.98
          ipaddr_con_MBUS = &IPADDR_CON_MBUS; 

          if (pcbMBUS)
          {
            tcp_err (pcbMBUS, MBUS_conect_err);

            tcp_connect(pcbMBUS, ipaddr_con_MBUS, PORTA,
                        MBUS_comunic_connected))
          }

        }
        else
        {
          if (passo==1)
          {
            if (Conexao)                //Server connected ?
            {                           //yes:
              TCPAT = 3;                //Connection Ok
              passo = 2;        
            }
            else                        //no:
            {                           //After 5s 
              if (time_ModTCPslave >= TEMP_5s)
              {

                TCPAT = 6;              //Error (client didn't
                                        //obtain connect)

              }
            }
          }     
        }
    }
    if (TCPAT == 3)                     //If server connected:
    {

        if (!Conexao)                   //Server disconnected ?
        {

          TCPAT = 5;                    //Error (Server has disconnected)

        }
        else
        {
          if (flgRecebeuDadosMBUS)      //Receiving datas ?
          {                     
            if(IndexMBUS>=6)
            {

              Trat_Datas();             //Treat datas

            }
            flgRecebeuDadosMBUS=0;
            IndexMBUS=0;
          }
          else
          {
                                        //After 5s without data
            if (ModTCPslave_cont_tempo_sem_pacotes_Rx >= TEMP_5s)
            {
              ModTCPslave_cont_tempo_sem_pacotes_Rx = 0;

              ModTCPslave_Close();

              Conexao = 0;

              TCPAT = 4;                //Error (Client has disconnected)

            }
          }
        }
    }
  }


} 


//-------------------------------------------------------------------------
static err_t MBUS_comunic_connected(void *arg, struct tcp_pcb *pcb,
                                    err_t err)
{
//LWIP_UNUSED_ARG(arg);

  tcp_sent(pcb, MBUS_sent);
  tcp_recv(pcb, MBUS_recv);

  pcbMBUS = pcb;
  Conexao = 1;
  ModTCPslave_cont_tempo_sem_pacotes_Rx = 0;

  return ERR_OK;
}


//-------------------------------------------------------------------------
static err_t MBUS_recv(void *arg,  struct tcp_pcb *pcb,
                                  struct pbuf *pBuf, err_t err)
{
  struct pbuf *pBuf_orig;
  unsigned char *dados, *p;
  unsigned short ntotal, nparcial, i, j;

//LWIP_UNUSED_ARG(arg);

  if (err==ERR_OK)
  {                             //-----------------------------------------
    if (pBuf==NULL)             //Server disconnected
    {                           

      Conexao = 0; 
      MBUS_close(pcb);
      pcbMBUS = NULL;

    }           
    else                        //-----------------------------------------
    {                           //Receiving data
      flgRecebeuDadosMBUS = 1;
      pBuf_orig = pBuf;
      p = (unsigned char*)&ModbusBufferSlave_Ativo + IndexMBUS;
      IndexMBUS += pBuf->tot_len;
      ntotal   = pBuf->tot_len;
      nparcial = pBuf->len; 
      dados    = pBuf->payload;
      i = 0;    
      j = 0;    

      if (IndexMBUS < TAM_BUF_MBUS)
      {                          
        while(ntotal)           
        {                       
          *(p+i++)=dados[j++];  
          ntotal--;
          if (ntotal)
          {     
            nparcial--;
            if (!nparcial &&     
                  pBuf->next)   
            {                   
              pBuf=pBuf->next;          
              nparcial = pBuf->len;
              dados = pBuf->payload;
              j = 0;
            } 
          }
        }
      }
      else
      {
        flgRecebeuDadosMBUS = 0;
        IndexMBUS = 0;
      }
      tcp_recved(pcb, pBuf_orig->tot_len);
      pbuf_free(pBuf_orig);

      ModTCPslave_cont_tempo_sem_pacotes_Rx = 0;

    }
  }
  else
  {


  }

  return ERR_OK;
}


//-------------------------------------------------------------------------
void MBUS_conect_err(void *arg, err_t err)
{
//LWIP_UNUSED_ARG(arg);

                        //Here appears:
                        //- If the cable is unplugged  ->  ERR_ABRT
                        //- If the cable is plugged, but the server isn't 
waiting
                        //  a connection               ->  ERR_RST
  error = err;


  ModTCPslave_Close();

}


//-------------------------------------------------------------------------
void MBUS_close(struct tcp_pcb *pcb)
{
  tcp_sent  (pcb, NULL);
  tcp_recv  (pcb, NULL);
  tcp_err   (pcb, NULL);

  tcp_close (pcb);
}


//-------------------------------------------------------------------------
void ModTCPslave_Close(void)
{

  if (pcbMBUS!=NULL)
  {
    MBUS_close(pcbMBUS); 
    pcbMBUS = NULL;
  }
}







reply via email to

[Prev in Thread] Current Thread [Next in Thread]