lwip-users
[Top][All Lists]
Advanced

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

Re: [lwip-users] Closing tcp-Connection


From: Markus
Subject: Re: [lwip-users] Closing tcp-Connection
Date: Sun, 19 Oct 2014 13:50:03 +0200

Hey,

I have ripped out the essential Parts of the Code to create a simple
example. On the PC must run iPerf or jPerf to do the Benchmark
(https://code.google.com/p/xjperf/). Configure it to run as a server.
I call Initialize() once after StartUp and Start() every time I want to run
the Benchmark. The Start() Function connect to the PC where iPerf-Server is
running (Alter the IP in the Code if you want to try it). After the
Connection is established I send for 10 seconds as much data as possible.
When the Timeout is reached I try to close the connection by calling
tcp_close (). The Function returns ERR_OK.
The next call to tcp_connect() failes because the pcb is in the state
TIME_WAIT.

Did I something wrong in the Code? 

Regards

Markus






#include "tcp.h"
#include "sys.h"
#include <stdint.h>

static struct tcp_pcb *iPerf_PCB = 0;
static u32_t StopTime = 0;
static uint8_t Data[TCP_MSS];

static err_t OnConnected (void *arg, struct tcp_pcb *tpcb, err_t err);
static err_t OnReceive (void *arg, struct tcp_pcb *pData, struct pbuf *p,
err_t err);
static void OnError (void *arg, err_t err);
static err_t OnSent (void *arg, struct tcp_pcb *pcb, u16_t len);
static err_t OnPoll (void *arg, struct tcp_pcb *pcb);
static void SendData (void);

void Initialize (void)
{
        iPerf_PCB = tcp_new ();
}

void Start (void)
{
        tcp_recv        (iPerf_PCB, OnReceive);
        tcp_err         (iPerf_PCB, OnError);
        tcp_sent        (iPerf_PCB, OnSent);
        tcp_poll        (iPerf_PCB, 0, 1);

        struct ip_addr addr;
        IP4_ADDR (&addr, 192,168,0,40);

        tcp_connect (iPerf_PCB, &addr, 5001, OnConnected);
}

err_t OnConnected (void *arg, struct tcp_pcb *tpcb, err_t err)
{
        StopTime = sys_now () + 10 * 1000;      // Run for 10 seconds
        SendData ();
        return ERR_OK;
}


void SendData (void)
{
        if (StopTime >= sys_now ())
        {
                do 
                {
                } while (tcp_write (iPerf_PCB, Data, sizeof (Data),
TCP_WRITE_FLAG_COPY) == ERR_OK);
        }
        else
        {
                err_t err = tcp_close (iPerf_PCB);
                if (err != ERR_OK)
                        tcp_poll (iPerf_PCB, OnPoll, 1);
                }
        }
}

static err_t OnSent (void *arg, struct tcp_pcb *pcb, u16_t len)
{
        SendData ();
        return ERR_OK;
}

static err_t OnPoll (void *arg, struct tcp_pcb *pcb)
{
        tcp_close (iPerf_PCB);
        return ERR_OK;
}

static err_t OnReceive (void *arg, struct tcp_pcb *pData, struct pbuf *p,
err_t err)
{
        pbuf_free (p);
        return ERR_OK;
}

static void OnError (void *arg, err_t err)
{
        err = err;
}




-----Ursprüngliche Nachricht-----
Von: address@hidden
[mailto:address@hidden Im Auftrag von Sergio R.
Caprile
Gesendet: Freitag, 17. Oktober 2014 18:42
An: address@hidden
Betreff: Re: [lwip-users] Closing tcp-Connection

OK, another thing I assumed is that you were using NO_SYS=1, and forgot to
tell.
I know nothing on RTOS ports, so I will assume you are calling the right
function and respecting the single-thread restrictions, as your other apps
work.

My tcp closure is:
         state = myCLOSING;
         if(tcp_close(pcb) == ERR_OK){
                tcp_recv(pcb, NULL);
                state = myCLOSED;
        }

tcp_close()  may fail due to a number of reasons, mostly involving low
memory situations (and I'm too lazy to dig more into the source), so if it
fails you'll have to retry later, maybe in the poll callback:
        if(state == myCLOSING){
                // Retry closing the connection
                myclose(pcb);
        }

Are you checking tcp_close()'s return value ? That should give a hint.
If somehow it fails and you don't retry later, it won't close.
(Should've started there... didn't I ?)



_______________________________________________
lwip-users mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/lwip-users




reply via email to

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