This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

network cable is disconnected for a moment

why it can not access the destination Host when the network cable is disconnected for a moment and later this connected again?

//----------------------------------------------------------------------------
// Thread 'Server': BSD Server socket process
//----------------------------------------------------------------------------
static void Server (void const *arg) { SOCKADDR_IN addr; int sock, sd, res; int type = (int)arg; char dbuf[4];

while (1) { sock = socket (AF_INET, type, 0); addr.sin_port = htons(PORT_NUM); addr.sin_family = PF_INET; addr.sin_addr.s_addr = INADDR_ANY; bind (sock, (SOCKADDR *)&addr, sizeof(addr)); if (type == SOCK_STREAM) { listen (sock, 1); sd = accept (sock, NULL, NULL); closesocket (sock); sock = sd; } while (1) { res = recv (sock, dbuf, sizeof (dbuf), 0); if (res <= 0) { break; } if (dbuf[0] == BLINKLED) { LED_Out (dbuf[1]); } } closesocket (sock); }
}

  • This is example code:

    //----------------------------------------------------------------------------
    // Thread 'Server': BSD Server socket process
    //----------------------------------------------------------------------------

    static void Server (void const *arg) {

    SOCKADDR_IN addr;

    int sock, sd, res;

    int type = (int)arg;

    char dbuf[4];

    while (1) {

    sock = socket (AF_INET, type, 0);

    addr.sin_port = htons(PORT_NUM);

    addr.sin_family = PF_INET;

    addr.sin_addr.s_addr = INADDR_ANY;

    bind (sock, (SOCKADDR *)&addr, sizeof(addr));

    if (type == SOCK_STREAM) {

    listen (sock, 1);

    sd = accept (sock, NULL, NULL);

    closesocket (sock);

    sock = sd;

    }

    while (1) {

    res = recv (sock, dbuf, sizeof (dbuf), 0);

    if (res <= 0) {

    break;

    }

    if (dbuf[0] == BLINKLED) {

    LED_Out (dbuf[1]);

    }

    }

    closesocket (sock);

    }

    }

  • Are you using a fixed IP number or DHCP? That often matters when a TCP/IP stack reacts to link loss.

  • That and how the unspecified host reacts to the change, a) by getting an interrupt or whatever, and b) by reprogramming the PHY and renegotiating the link to the switch/hub

    Please see also Posting Tips about using the PRE tags for source code.


  • Where did it come from?

    The trouble with "example code" or "sample code" is that it often just handles the "normal" case - so it may well not be designed to cope with this scenario.

    In software projects in general - and comms in particular - the "normal" case is the easy bit; it's handling the "exceptions" properly that takes most of the work!

    The 90:10 Rule:

    www.8052.com/.../182513

    www.8052.com/.../81002

    www.8052.com/.../76017

    en.wikipedia.org/.../Ninety-ninety_rule

  • Hello,

    If you disconnect and reconnect a cable when using a fixed IP address, you may be falling victim to 'TIME-WAIT '

    en.wikipedia.org/.../File:Tcp_state_diagram_fixed_new.svg

    in your case he server may be

    "... represents waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its connection termination request. [According to RFC 793 a connection can stay in TIME-WAIT for a maximum of four minutes known as a MSL (maximum segment lifetime)."

    so if you are always sending to the same destination IP address and port, and if you are using the same fixed IP address, the server may think these are just lost packets floating around and garbage collect them (and not respond to them). After the MSL expires (I believe 4ish minutes on windows) you would be able to trasmit again.

    I believe this idea is in RFC 763 (1981) pg 27.