lwip-users
[Top][All Lists]
Advanced

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

[lwip-users] Re: Problem with recv (2)


From: Oscar F
Subject: [lwip-users] Re: Problem with recv (2)
Date: Tue, 6 Oct 2009 07:46:37 +0200

Hello, yesterday i have tested with this result. i think that the problem can be other.

Remenber, i have 8 socket 1500h 1501h 1502h 1600h 1601h 1602h 1603h 1604h.

the accept connecttion works, but not always, 50%.

The recv: 1501h an 1502h OK but 1500h bad!! never recieve
The send: 1500h and 1600h bad, the rest sockets perfect
The close: 1500h and 1600h bad, the rest sockets perfect

My conclusion and i can´t proof the recv for the rest of socket, is that socket 1500 and 1600 doesn´t work, only accept the connection but no always.
I don't known what happend, can be a problem of my customer copmputer?. My connection is done with a router and connect my EVK1100 to PC by this router connecting to internet.

thanks
Oscar

the file code

static int aSocket[NUM_SOCKET]; //Accepted Socket. Static global for other task acce
static int lSocket[NUM_SOCKET]; //Local Socket
static char BufferRx[TAM_RX_RTU];
static struct sockaddr_in sLocalAddr[NUM_SOCKET];
static struct sockaddr_in sRemoteAddr[NUM_SOCKET];
static long AddressPCU;

static short State_TablePCU;
static short Mode_System;


//HEADER of function of connection
static void ProcessRequestPC_to_RTU (int s);
static void ProcessRequestPC_to_PCU (int s);
static void ProcessRequestPC_Finish (int s);
static void CloseConnectionPC(int *s1,int *s2 );
static void Send_ACK_or_NACK(int s,int tipo,int code );


portTASK_FUNCTION( vProtocolRTUServer, pvParameters )
{
    //Local variables of function ==> maybe put global
    int i,maxfd,ret,num_conexiones=0;
    fd_set readset;
    int PortConnection[NUM_SOCKET]={0x1600,0x1500,0x1501,0x1502,0x1601,0x1602,0x1603,0x1604}; //the order is important

    //Initialization of variable global of the this task
    Mode_System=MODE_INIT;
    State_TablePCU=TABLE_EMPTY;

    //init_dbg_rs232(66000000);
    //Create the NUM_SOCKET=8 connection to wait the accept
    print_dbg(" Inicializando Sockets  \n");
    for(i=0;i<NUM_SOCKET;i++)
    {
          //Create the 8 socket
        lSocket[i] = lwip_socket(AF_INET, SOCK_STREAM, 0);
        if (lSocket[i]<0)
         return;

          //Bind to this socket
          memset((char *)&sLocalAddr[i], 0, sizeof(sLocalAddr[i]));
        sLocalAddr[i].sin_family = AF_INET;
        sLocalAddr[i].sin_len = sizeof(sLocalAddr[i]);
        sLocalAddr[i].sin_addr.s_addr = htonl(INADDR_ANY); //!!!!CUIDADO SINO VALE CUALQUIER DIRECCION CAMBIAR!!!!
        sLocalAddr[i].sin_port = PortConnection[i]; //Ports of the table

        if (lwip_bind(lSocket[i],(struct sockaddr *)&sLocalAddr[i], sizeof(sLocalAddr[i])) < 0)
         {
           //Error. Close the socket
           lwip_close(lSocket[i]);
           print_dbg(" Error en bind \n");

           return;
         }

        //Listen to the 8 socket.Maximum 1 connection for socket
        if ( lwip_listen(lSocket[i],1) != 0 )
         {
           lwip_close(lSocket[i]);
           print_dbg(" Error en listen \n");
           return;
         }
      }


    //Using of select function to wait for the accept connection

      print_dbg(" Sockets preprarados para entrar en el accept secuencial \n");
      num_conexiones=0;
      do{

          aSocket[num_conexiones]= accept(lSocket[num_conexiones],(struct sockaddr*)&sRemoteAddr[num_conexiones],(socklen_t *)sizeof(sRemoteAddr[0]));
          if (aSocket[num_conexiones]>0)
          {
            print_dbg("socket Aceptado: ");
            print_dbg_ulong(aSocket[num_conexiones]);
            print_dbg("\n socket Inicial: ");
            print_dbg_ulong(lSocket[num_conexiones]);
            print_dbg(" \n");
            num_conexiones++;
          }
          else
          {
              CloseConnectionPC(aSocket,lSocket );
              print_dbg(" Error en accept \n");
              return;
          }

      }while (num_conexiones<NUM_SOCKET);

 while(1){
          FD_ZERO(&readset);
          maxfd=0;
          print_dbg(" Metemos los 3 Sockets por donde podemos recibir para entrar en el select \n");
          for(i=1;i<4;i++)
           {
             FD_SET(aSocket[i], &readset);
             print_dbg("socket : ");
             print_dbg_ulong(aSocket[i]);
             print_dbg("\n ");
             if (aSocket[i]> maxfd)
              maxfd = aSocket[i];
           }

          // Check for received packets on configured sockets (blocking )
          ret = select(maxfd+1, &readset, NULL, NULL, NULL);

          print_dbg("select ha entrado \n");

          if (ret > 0) //No error
            {
              //Check data for socket 1
              if (FD_ISSET(aSocket[1], &readset))
                ProcessRequestPC_to_RTU(aSocket[1]);

              //Check data for socket 2
              if (FD_ISSET(aSocket[2], &readset))
               ProcessRequestPC_to_PCU(aSocket[2]);

              //Check data for socket 3
              if (FD_ISSET(aSocket[3], &readset))
               ProcessRequestPC_Finish(aSocket[3]);

            }
          else
          {
              print_dbg(" Error en select \n");
              CloseConnectionPC(aSocket,lSocket );
              return;
          }
          }
          }




static void ProcessRequestPC_to_RTU (int s)
{
 int result,nbytesRx;
 long code_command,length_command;

  print_dbg("Recibido mensaje RTU \n");
  nbytesRx=0; //Total=0;

  result = recv(s, BufferRx, TAM_RX_RTU, 0);
  if (result<=0) //Error or close connection
   {
    print_dbg("Fallo Recv \n");
    CloseConnectionPC(aSocket,lSocket );
    return;
   }
  nbytesRx+=result; //Byte received
  print_dbg("Procesando RTU 1 \n");
  //Calculate the Length. ¡¡¡¡¡ I supposed that 4 byte are received always TAKE CARE!!!!!!!
  memcpy(&length_command, BufferRx, LENGHT_RX);
  if (length_command!=nbytesRx)
  {
   do{
       result = recv(s, BufferRx+nbytesRx, length_command-nbytesRx, 0);
       if (result<=0) //Error or close connection
          {
           print_dbg("Fallo Recv \n");
           CloseConnectionPC(aSocket,lSocket );
           return;
          }
       print_dbg("Procesando RTU 2 \n");
       nbytesRx+=result;

     } while (nbytesRx<length_command);
  }
  //All message is received
  memcpy(&code_command, BufferRx+LENGHT_RX,L_CODE_RX); //copy the code of message

  switch(code_command)
  {








  }
}

static void ProcessRequestPC_to_PCU (int s)
{
    int result,nbytesRx;
     long code_command,length_command;

     print_dbg("Recibido mensaje PCU \n");
     nbytesRx=0;

      result = recv(s, BufferRx, TAM_RX_RTU, 0);
      if (result<=0)
       {
        print_dbg("Fallo Recv \n");
        CloseConnectionPC(aSocket,lSocket );
        return;
       }
      nbytesRx+=result;

      //Calculate the Length
      memcpy(&length_command, BufferRx, LENGHT_RX);
      if (length_command!=nbytesRx)
      {
       do{
           result = recv(s, BufferRx+nbytesRx, length_command-nbytesRx, 0);
           if (result<=0)
              {
               print_dbg("Fallo Recv \n");
               CloseConnectionPC(aSocket,lSocket );
               return;
              }
             nbytesRx+=result;
             print_dbg("Procesando RTU \n");
         } while (nbytesRx<length_command);
      }
      //All message is received
     memcpy(&code_command, BufferRx+LENGHT_RX, L_CODE_RX); //copy the code of message
     print_dbg("Comando recibido= ");
     print_dbg_ulong(code_command);
     print_dbg("\n Longitud= ");
     print_dbg_ulong(length_command);
     print_dbg("\n");
      switch(code_command)
      {
       case ID_PCU_01:
                      if (Mode_System==MODE_INIT)
                      {
                       print_dbg("Comando Abrir la tabla PCU recibido \n ");
                       Send_ACK_or_NACK(aSocket[0],SEND_ACK,0);
                       //Reset Table
                       AddressPCU=ADDRESS_PCU;
                       State_TablePCU=TABLE_EMPTY;
                      }
                      else
                      {
                        print_dbg("No estamos en el modo INIT \n");
                        Send_ACK_or_NACK(aSocket[0],SEND_NACK,NO_MODE_INIT);
                      }
                      break;
       case ID_PCU_02:


                          break;
       case ID_PCU_03:

                          break;
       case ID_PCU_04:

                          break;


      }

}

static void  ProcessRequestPC_Finish (int s)
{
    int result,nbytesRx;
    long code_command,length_command;

     print_dbg("Recibido mensaje Finish \n");
     nbytesRx=0;

      result = recv(s, BufferRx, TAM_RX_RTU, 0);
      if (result<=0)
       {
        print_dbg("Fallo Recv \n");
        CloseConnectionPC(aSocket,lSocket );
        return;
       }
      nbytesRx+=result;

      print_dbg("Procesando finish 1 \n");
      //Calculate the Length
      memcpy(&length_command, BufferRx, LENGHT_RX);
      if (length_command!=nbytesRx)
      {
       do{
           result = recv(s, BufferRx+nbytesRx, length_command-nbytesRx, 0);
           if (result<=0)
              {
               print_dbg("Fallo Recv \n");
               CloseConnectionPC(aSocket,lSocket );
               return;
              }
             print_dbg("Procesando finish \n");
             nbytesRx+=result;

         } while (nbytesRx<length_command);
      }
      //All message is received
      memcpy(&code_command, BufferRx+LENGHT_RX, L_CODE_RX); //copy the code of message
      print_dbg("Comando recibido= ");
      print_dbg_ulong(code_command);
      print_dbg("\n Longitud= ");
      print_dbg_ulong(length_command);
      print_dbg("\n");
      switch(code_command)
      {
        case ID_SPR_01:

                       Send_ACK_or_NACK(aSocket[0],SEND_ACK,0);

                       print_dbg(" Enviado ACK a socket ");
                       print_dbg_ulong(aSocket[0]);
                       print_dbg("\n");
                       print_dbg(" cerramos pero falla.Estudiar porque \n");
                       //close(s); uno solo funciona
                       CloseConnectionPC(aSocket,lSocket );
                       return;
      }

}


static void CloseConnectionPC(int *s1,int *s2 )
{
  int i;

    print_dbg("Ordenamos cerrar los Socket \n");
    for (i=2;i<NUM_SOCKET;i++)
    {
     close(s1[i]);

     //close(s2[i]);
    }
}

static void Send_ACK_or_NACK(int s,int tipo,int code )
{
  char BufferACK[12];
  long aux;

  aux=0x0000000C;
  memcpy(BufferACK, &aux, 4);
  aux=ID_ACK_01;
  memcpy(BufferACK+4, &aux, 4);

  if (tipo==SEND_ACK)
  {
    aux=0x00010000;
    memcpy(BufferACK+8,&aux, 4);

    print_dbg("Enviamos el comando ACK \n");
  }
  else
  {
    aux=0x0002;
    memcpy(BufferACK+8,&aux, 2);
    aux=code;
    memcpy(BufferACK+10,&aux, 2);
    print_dbg("Enviamos el comando NACK con error ");
    print_dbg_ulong(code);
    print_dbg("\n ");
  }
   //Send the command. Maybe i have to deal with the posible error.
   send(s, BufferACK, 12, 0);
}









On Mon, Oct 5, 2009 at 8:18 PM, Oscar F <address@hidden> wrote:
Hello again, today i´ve tested several thing, and i have a little conclusión two port don´t work, port number 1500h and 1600h. It is very strange, because if i don´t touch this number i can send, recv and close this socket without problem.

More strange is that let me to accept the connection to the all port including 1500 and 1600.
any idea?

Thanks
Oscar


reply via email to

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