1

i'm trying to build a server with a PIC24F.

This is a piece of code i'm isuing:

switch(TCPServerState) {
            case SM_HOME:
                // Allocate a socket for this server to listen and accept connections on
                socket.Socket = TCPOpen(0, TCP_OPEN_SERVER, SERVER_PORT, TCP_PURPOSE_GENERIC_TCP_SERVER);
                if(socket.Socket != INVALID_SOCKET) {
                    TCPServerState = SM_LISTENING;
                }
                break;
            case SM_LISTENING:
                // See if anyone is connected to us
                //if(TCPIsConnected(socket.Socket)) {
                if(!TCPWasReset(socket.Socket)){
                    if(socket.Connected == 0) {
                        socket.Connected = 1;
                        printf("Socket is CONNECTED: %d\n", socket.Socket);
                    }
                    uint16_t avaible = TCPIsGetReady(socket.Socket);
                   // Some stuff
                }
                else if(socket.Connected == 1){
                   printf("Socket RESET: %d\n", socket.Socket);
                   TCPServerState = SM_CLOSING;
                }
                break;
            case SM_CLOSING:
                // Close the socket connection.
                socket.Connected = 0;
                TCPClose(socket.Socket);
                TCPServerState = SM_HOME;
                printf("Socket is CLOSED: %d\n", socket.Socket);
                break;
        }

All works fine if i close my client socket properly, but if i disconnect ethernet cable i am not able to detect disconnection and my code does not close the socket because TCPWasReset still FALSE(or TCPIsConnected still TRUE).

So how can i detect the disconnection of network cable(without add a software keep_alive implementation) ?

Thanks

blow
  • 12,811
  • 24
  • 75
  • 112

1 Answers1

1

Check a few items:

  • Call TickInit(); before StackInit();

  • Select the correct TIMER1 clock source for your application - internal clock or external clock (T1CON.TCS)

    Otherwise, just use a debugger on the keepalive logic in TCP.C, which should default to 10 seconds in the latest Microchip Library for Applications TCP/IP stack 5.42.08.

Community
  • 1
  • 1
Mike
  • 1,276
  • 4
  • 16
  • 27