[En-Nut-Discussion] TCP timeout problem - really interesting

Pavel Chromy chromy at asix.cz
Tue Feb 4 17:31:18 CET 2003


Concerning my previous posts - I think I found out what might be
causing the problem - quite interesting, here is a part of the
source, comments follow:

/*=====================================================*/

THREAD(MyThread, arg)
{
  for (;;) {
    // some quick code not affecting other threads
    NutSleep(50); // !!! notice this
    //NutThreadYield();
  }
}

THREAD(NutMain, arg)
{
    int count;
    unsigned char a;

    char mac[6]={0x00,0x06,0x98,0x00,0x00,0x00};
    u_long to=2000;

    NutRegisterDevice(&devEth0, 0x8300, 5);
    NutNetIfConfig("eth0", mac, inet_addr("0.0.0.0"), inet_addr("255.255.255.0"));

    NutThreadCreate("MyThread", MyThread, NULL, 512);
    for(;;) {
        socket=NutTcpCreateSocket();
        NutTcpSetSockOpt(socket,SO_RCVTIMEO,&to,sizeof(u_long));
        if (NutTcpAccept(socket,23)==0) {
          //NutPrintString_P(0, PSTR("connected\r\n"));

          do {
            count=NutTcpReceive(socket,&a,1);
            if (count==1) ProcessCharacter(a);
          } while (count!=-1);
        }
        NutTcpCloseSocket(socket);
        socket=NULL;
    }
}

/*=====================================================*/

When the time in NutSleep() is lower than 63
(granularity of system timer) the whole thing behaves really weird.

Sometimes it does not reply to ping after reset,
sometimes after reset it replies but hangs for a while.

Even in that rare case when it seems to work,
it likely hangs completely after about 10 minutes of running.
(That's what my first post was about)
Moreover the timeout for NutTcpReceive() does not work in this case!
In fact it works only for perios shorter that 62,5 ms.
(My second post).

I know that it is not possible to achieve more exact timing that
granularity of the timer by using NutSleep(),
but it seemed better to me than using just NutThreadYield(),
which, as later turned out, works fine.
BTW what is the difference between yielding the cpu and sleeping for
such short time?
I suppose that this is kind of bug - sleeping a process should be
safe, shouldn't it?

I even tried to use NutSleep() and NutThreadYield() together, which
did not work either.

Pavel




More information about the En-Nut-Discussion mailing list