Home/Support/Support Forum/problem with Connection closed socket (re-loop)
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

problem with Connection closed socket (re-loop)

0 votes
Hello,

To communicate with my server i have tried active.c who works but my problem is this part :

" while(tcp_tick(&socket));

sock_abort(&socket);
printf("\nConnection closed...\n");"

doesn't work.

I must implement a interrupt function but as my connection doesn't close, i can't re-loop my interrupt, excepted that it works. So if you have any ideas to help me i will appreciate this.
asked Sep 2, 2020 in Rabbit by JPal New to the Community (7 points)

Please log in or register to answer this question.

1 Answer

0 votes
If the remote end closes its end of the socket, then the Rabbit should eventually close the socket and exit that while loop.

You can call sock_close() if you want to initiate closure of the socket.

After that call to sock_abort(), you should be able to go right back to calling tcp_open() on the socket structure to open another connection.
answered Sep 2, 2020 by TomCollins Veteran of the Digi Community (2,082 points)
void main(){

    auto int key, channel;
      // Initialize the controller
    brdInit();

   // Configure all inputs to be general digital inputs
    for(channel = 0; channel < BL_DIGITAL_IN; ++channel)
    {
      setDigIn(channel);
   }

    if (digIn(3) != 0)
    {
       char buffer[2048];
        int bytes_read;
           longword  destIP;
          tcp_Socket socket;
        int digIn();

   //création de la socket
    sock_init_or_exit(1);

    if( 0L == (destIP = resolve(DEST)) ) {
        printf( "ERROR: Cannot resolve \"%s\" into an IP address\n", DEST );
        exit(2);
    }

    tcp_open(&socket,0,destIP,PORT,NULL);

    printf("Waiting for connection...\n");
    while(!sock_established(&socket) && sock_bytesready(&socket)==-1) {
        tcp_tick(NULL);
    }

    printf("Connection established, sending request...\n");


    /*
     *  When tcp_tick()-ing on a specific socket, we get non-zero return while
     *  it is active, and zero when it is closed (as used here).
     */

    sock_write(&socket,"client actif \n",30);

      do {
        bytes_read=sock_fastread(&socket,buffer,sizeof(buffer)-1);

        if(bytes_read>0) {
            buffer[bytes_read] = '\0';
            /*
             * By using the "%s" format, if there are "%" in the buffer, printf()
             *  won't try to interpret them!
             */
            printf("%s",buffer);
        }
    }

  while(tcp_tick(&socket));

    sock_abort(&socket);
    printf("\nConnection closed...\n");
           }
   }

As you see (in this part) it's a the end, i have tried sock_close() without succes. I don't understand why my function stay in the do and doesn't continue.
Do you understand that the "while" is part of the "do" above it?  "do { ... } while (condition);" is a loop that executes the statements at least once, and continues to loop as long as the condition is true.  In this case, you'll leave the "do" when tcp_tick() returns false (0), which it should do if the remote end has closed the socket correctly.
Thank for this answer TomCollins, so I need to correctly close my server.
...