lwip-users
[Top][All Lists]
Advanced

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

[lwip-users] TCP half-open problem


From: James Classen
Subject: [lwip-users] TCP half-open problem
Date: Wed, 20 Nov 2013 15:01:01 -0600

In the development process of certain software, we are routinely (unfortunately) running into situations where the TCP connection winds up half open--the hardware (for which I'm writing) sits listening, content to do so forever (or at least longer than we have the patience to wait). I've been trying various solutions to come up with a way in which this can be remedied. The problems that relate to lwIP (and why I'm posting this message) are as follows:

If I run two separate threads with netconn_accept in them, bound to the appropriate IP/port, one seems to usurp the other, yet both appear to be tied up. Essentially, this second thread is intended to remedy the half-open problem by closing the connection in the primary thread. This can't happen if both are trying to handle the same request.

If instead I try to use a non-blocking connection, I can't find an example that provides a way to receive data without using netconn_accept (which, as best I can tell, blocks) in a while(true) loop, nor can I find a way to "unaccept" to allow the non-blocking to "take control" again once the received data has been dealt with. Likewise, this non-blocking connection is supposed to react to received data, yet not either retain the connection for a "new" client (when the original attempts to re-connect) or drop and re-open the connection.

Closing the connection after each command is dealt with isn't desirable, as establishing the TCP connection takes the longest of all the functions in the interface between the software and my hardware.

Does this make sense?

Any creative approaches to this problem? Or suggestions as to what I may not be searching for online (and thus what I may have missed)? Is there some repository of lwIP examples for me to peruse that may hold the solution?

I'm back to the threaded code now, which is shown below, more-or-less copy-pasted from the few examples I was able to find (PORT is #defined elsewhere, everything else should be GNU C or lwIP):

void tcp_server(void) {
    struct netconn * conn, * newconn;
    err_t            err;
    int              status = 0;

    /* Create a new connection identifier. */
    conn = netconn_new(NETCONN_TCP);

    /* Bind connection to TCP Port. */
    netconn_bind(conn, NULL, PORT);

    /* Tell connection to go into listening mode. */
    netconn_listen(conn);

    while (1) {
        err = netconn_accept(conn, & newconn);
        //Process the new connection.
        if (err == ERR_OK) {
            printf("TCP Server -- Accepted new connection: %p\n", newconn);
            struct netbuf *rxbuf;
            void  *rxdata;
            uint16_t rxlen;
            while ((err = netconn_recv(newconn, &rxbuf)) == ERR_OK) {
                netbuf_data(rxbuf,&rxdata,&rxlen);
                char *s=strdup(rxdata);
                //***deal with data in s
                free(s);
            }
            sched_yield();
            printf("Got EOF, looping\n");
            //Close connection and discard connection identifier.
            netconn_close(newconn);
            netconn_delete(newconn);
        } else {
            printf(lwip_strerr(err));
        }
        sched_yield();
    }
}


Thanks,

James C

reply via email to

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