lwip-users
[Top][All Lists]
Advanced

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

Re: [lwip-users] TCP connection problem


From: flavio
Subject: Re: [lwip-users] TCP connection problem
Date: Tue, 6 Aug 2013 15:07:09 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

Pomeroy, Marty <address@hidden> writes:

> 
> 
> >> Thanks for reply, Marty.
> 
> >> But how can I definitively close the old connection and begin a new
> connection ? 
> 
> Flavio
> 
> You're welcome, but you can't force a socket fully closed quickly.  To
> connect on the same port, you just have to wait four minutes.  Microsoft
> says you can get that down to 30 seconds (Google up TcpTimedWaitDelay),
> but I have never tried.
> 
> This is how the standard Transition State table runs for TCP Ports.  You
> cannot re-use the same port until the WAIT expires, so that any old
> packets still floating around don't get delivered on the newly opened
> port with the same number.
> 
> I'm using the sockets interface in lwIP, and open a listener socket on
> my server, which assigns an available port number when you connect.  In
> case you can use a listener on the server and lwIP sockets, I posted the
> code below.
> 
> Anyone else have an alternative?
> 
> Marty
> 
> static unsigned int s_connectSocket = INVALID_SOCKET;
> 
> static int s_SetupTcpClient()
> {
>    sockaddr_in sockAddr;
>    unsigned char ipBytes[4] = {192, 168, 20, 85};    // Server IP
>    unsigned long *ip = (unsigned long *)&ipBytes;
> 
>    s_connectSocket = lwip_socket( AF_INET, SOCK_STREAM, IPPROTO_TCP);
>    if (s_connectSocket == -1)
>    {
>       // failed
>       return EXH_USER_ACTION_SOCKET_NO_CONNECT;
>    }
> 
>    sockAddr.sin_len = sizeof(struct sockaddr_in);
>    sockAddr.sin_family = AF_INET;
> 
>    sockAddr.sin_port = PP_HTONS( DEFAULT_PORT);
> 
>    sockAddr.sin_addr.s_addr = *ip;
> 
>    int err = lwip_connect( s_connectSocket, (const struct sockaddr *)
> &sockAddr, (socklen_t) sizeof(struct sockaddr_in));
>    if (err == ERR_OK)
>    {
>       return 0;
>    }
>    else
>    {
>       err = lwip_close( s_connectSocket);
> 
>       return 1;
>    }
> }
> 
> ...
> 
>       pktLen = recv(s_connectSocket, s_recvBuf, expectedSize, 0);
> 




About the problem reported, I yet haven't find the solution!
Please, can any help me ?


Below there are more informations:

1) I saw that the problem occurs running like client and server together!  
   My program works like a client and like a server in TCP/IP network,
   using StellarisWare and lwIP 1.3.2. 

   I use "callback" or "raw" API  and  NO_SYS=1.
   I have verified that:

   1.1) Operating only like server ... works very fine (included connection
        and disconnection)
   1.2) Operating only like client ... works very fine (included connection
        and disconnection)
   1.3) Operating like server and client ... works fine, but sometime,
        if disconnects and connects again, the program crashes.

   What is wrong in the program ?


2) Another question:
   In my file "lwipopts.h" I defined:
   
#define LWIP_DEBUG
#define LWIP_DBG_MIN_LEVEL  LWIP_DBG_LEVEL_OFF
#define LWIP_DBG_TYPES_ON  
(LWIP_DBG_ON|LWIP_DBG_TRACE|LWIP_DBG_STATE|LWIP_DBG_FRESH)
#define TCP_DEBUG           LWIP_DBG_ON
#define TCP_INPUT_DEBUG     LWIP_DBG_ON
#define TCP_FR_DEBUG        LWIP_DBG_ON
#define TCP_RTO_DEBUG       LWIP_DBG_ON
#define TCP_CWND_DEBUG      LWIP_DBG_ON
#define TCP_WND_DEBUG       LWIP_DBG_ON
#define TCP_OUTPUT_DEBUG    LWIP_DBG_ON
#define TCP_RST_DEBUG       LWIP_DBG_ON
#define TCP_QLEN_DEBUG      LWIP_DBG_ON

   When it occurs the problem sometimes appears this message
   in my serial channel (ASCII):
   C:/StellarisWare_8555/boards/dk-lm3s9b96/My_project/../../
   ../third_party/lwip-1.3.2/src/core/tcp_in.c[NUL][c0]F[NUL]
   [c9][NUL][SP][STX][c9][NUL][SP][cc][c8][NUL][SP][ACK][98]
   [ENQ][90][ACK][98][c0]h[ACK][90][ACK][98][NUL]([93][d1]HH
   [NUL]h[80][89][ETX][f0]F[fe][NUL][f0]?[NUL][ff][f7]Q[fd]
   [EOT][98][NUL]([NUL][f0][ESC][81][EOT][98][NUL]|[ff][f7]
   B[fd][84]I[NUL][SP][BS]`[STX][98][83]I[NUL][89][BS][80]
   address@hidden
   
   What this means ?  What is being missing to appears a readable
   message ?




//----------------------------------------------------------------//
// Below is a piece of code (operating like server and client)
//----------------------------------------------------------------//
struct tcp_pcb *pcbMBUS;
struct tcp_pcb *pcbMBUSslave;
...


//----------------------------------------------------------------//
// Part of the SERVER:
//----------------------------------------------------------------//
  ...

    pcbMBUS = tcp_new();

    if (tcp_bind(pcbMBUS, IP_ADDR_ANY, PORT_MODBUSTCP) == ERR_OK)
    {
      pcbMBUS = tcp_listen(pcbMBUS);
      if (pcbMBUS!=NULL)
      {
        tcp_accept(pcbMBUS, MBUS_comunic_accept);
      }
    }
  ...


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

  if (ModTCP_STAT_CON_usu)      //If already connected
  {
    return ERR_ISCONN;
  }             

  tcp_accepted(pcb);            //Acknowledge  that  we  have  accepted
                                //this connection
  tcp_sent(pcb, MBUS_sent);
  tcp_recv(pcb, MBUS_recv);
  tcp_err (pcb, MBUS_conect_err);
                        
  pcbMBUS = pcb;
  ModTCP_STAT_CON_usu = 1;      //Inform it's connected
        
  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)             //"Host" disconnected ?
    {
      MBUS_close(pcb); 
      pcbMBUS = NULL;
    }           
    else                        //----------------------
    {                           //Receive data

      ...

    }
  }
  else
  {

  }

  return ERR_OK;
}

//----------------------------------------------------------------
void MBUS_close(struct tcp_pcb *pcb)
{

  tcp_arg   (pcb, NULL);
  tcp_sent  (pcb, NULL);
  tcp_recv  (pcb, NULL);
  tcp_err   (pcb, NULL);
  tcp_close (pcb);

  ModTCP_STAT_CON_usu = 0;
}







//----------------------------------------------------------------//
// Part of the CLIENT:
//----------------------------------------------------------------//
  ...

  pcbMBUSslave = tcp_new();

  if (pcbMBUSslave)
  {
    tcp_err (pcbMBUSslave, MBUSslave_conect_err); 

    if (tcp_connect(pcbMBUSslave, ipaddr_con_MBUSslave, PORTA,
            MBUSslave_comunic_connected) != ERR_OK)
    {
      MBUSslave_close(pcbMBUSslave); 
      pcbMBUSslave = NULL;
      return 0x01;
    } 

    ...

    return 0x00;

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

  tcp_sent(pcb, MBUSslave_sent);
  tcp_recv(pcb, MBUSslave_recv);
  tcp_err (pcb, MBUSslave_conect_err);

  pcbMBUSslave = pcb;

  return ERR_OK;
}

//----------------------------------------------------------------
static err_t MBUSslave_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;
      MBUSslave_close(pcb);
      pcbMBUSslave = NULL;

    }           
    else                        //---------------------------
    {                           //Receiving data
      ...

      tcp_recved(pcb, pBuf_orig->tot_len);
      pbuf_free(pBuf_orig);

    }
  }

  return ERR_OK;
}

//----------------------------------------------------------------
void MBUSslave_conect_err(void *arg, err_t err)
{
//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 ModTCPslave_Close(void)
{

  if (pcbMBUSslave!=NULL)
  {
    MBUSslave_close(pcbMBUSslave); 
    pcbMBUSslave = NULL;
  }
}

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

  tcp_abort (pcb);
}








reply via email to

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