Author Topic: Socket Not Closing  (Read 10519 times)

Offline neil

  • Sr. Member
  • ****
  • Posts: 438
    • View Profile
Socket Not Closing
« on: November 14, 2008, 12:28:13 PM »
Hi Mark,
  I think I must be doing something wrong, but not sure what.

After a connection is made to socket, my windows application sends down about 270 bytes of data, which is read, and an "ACK" (this is an ascii "ACK" sent as a string) is sent back from 52235. If the windows app receives an "ACK"  then the procedure continues, until all data is sent, which works perfect. After  this , or anytime I attempt to close the connection from Windows with the following:
   shutdown(socket,SD_BOTH);
   closesocket(socket);
The TCP_EVENT_CLOSED never gets called, or never times out, even when closing the Windows application.

Here is the callback routine:
int fnTCPListener(USOCKET Socket, unsigned char ucEvent, unsigned char *ucIp_Data, unsigned short usPortLen)
{
   TCP_MESSAGE test_message;
   unsigned char Value;
   int Loop;
   
    switch (ucEvent) {
    case TCP_EVENT_CONNECTED:
               SendToUser((unsigned char *)"\r\nCONNECTED\r\n",strlen("\r\nCONNECTED\r\n")); //message sent through serial
    case TCP_EVENT_CONREQ:                                             
    case TCP_EVENT_CLOSE:
    case TCP_EVENT_ACK:
    case TCP_EVENT_ARP_RESOLUTION_FAILED:
    case TCP_EVENT_PARTIAL_ACK:
        break;
    case TCP_EVENT_REGENERATE:
       break;
    case TCP_EVENT_DATA:
          for(Loop=0;Loop<usPortLen;Loop++)
          {
             Value=ucIp_Data[Loop];
               if(!ReadInBlock(&TCBrxBuff,Value)) //simply reads into own buffer, and checks to see if all read..
              { //Yip all read in , now proceed with command request...
                      RxBuff=&TCPrxBuff;//point to structure being processed
                   ProcessCommand(); //process command..
              }
      }
        break;
    case TCP_EVENT_ABORT:
    case TCP_EVENT_CLOSED:
        fnTCP_Listen(Socket, TCP_INPORT, 0);                    // go back to listening state
         SendToUser((unsigned char *)"\r\nCLOSED\r\n",strlen("\r\nCLOSED\r\n"));
       
        break;
        }

    return APP_ACCEPT;
}

void ProcessCommand(void)
{
 //command carried out here.. ie saved into spi eeprom
      TCP_MESSAGE test_message;
   strcpy(test_message.ucTCP_Message,"ACK");
   fnSendTCP(Test_socket, (unsigned char *)&test_message.TCP_Header, 3, TCP_FLAG_PUSH);
}


But cant close connection..

Regards
Neil

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Socket Not Closing
« Reply #1 on: November 14, 2008, 12:42:37 PM »
Hi Neil

If the listener is not entering with the close event it seems as though the Windows TCP socket is not sending the close.
Can you record the Ethernet activity to see whether you see what the Windows PC is sending when your program closes?

Regards

Mark

Offline neil

  • Sr. Member
  • ****
  • Posts: 438
    • View Profile
Re: Socket Not Closing
« Reply #2 on: November 14, 2008, 12:49:24 PM »
Hi Mark,
  I tested straight after the connection command from the Windows App, with the same close procedure and it does close. Even when I exit the Windows Application, the tcp never times out.

I forget how I check the Ethernet activity.

Regards
Neil

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Socket Not Closing
« Reply #3 on: November 14, 2008, 12:52:43 PM »
Neil

If you use WireShark you can record the activity and then it is sure what is taking place on the network.
If you send be a recording it may help to identify any problems.

I can also play the recording through the uTasker simulator (using your listener).

regards

Mark


Offline neil

  • Sr. Member
  • ****
  • Posts: 438
    • View Profile
Re: Socket Not Closing
« Reply #4 on: November 14, 2008, 01:14:05 PM »
Hi Mark,
  I attach a copy of the file. I removed all traffic not to do with this problem (and lot of traffice between the 2 , as file was too large).  My computer is 192.168.0.189.

Thanks
Neil

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Socket Not Closing
« Reply #5 on: November 14, 2008, 04:11:14 PM »
Hi Neil

I am sorry but I just can't explain why you can't get the close call back. The Ethernet recording shows the PC sending a FIN and your board responding with a FIN + ACK (it is accepting the close) and the PC is then also sending the final ACK.

I have tried reproducing something but in every case it works normally here.

Please try the following:

1. When the PC sends the FIN your listener will be called with TCP_EVENT_CLOSE. Since you are doing nothing special on this it will return APP_ACCEPT and so the close will be accepted (seen from the recording). If you add a print out here you must see this. Please check.

2. When the PC sends the final ACK to the close you should enter your listener with the event TCP_EVENT_CLOSED. This you are not seeing, although the connection has been closed (seen from the recording).

Put a break point in the TCP code:

        case TCP_STATE_LAST_ACK:                                         // doing passive close
            if (rx_tcp_packet.usHeaderLengthAndFlags & TCP_FLAG_ACK) {               
                if (rx_tcp_packet.ulAckNo != ptr_TCP->ulNextTransmissionNumber) {
                    return;                                              // invalid ACK so ignore the frame
                }
               
                ptr_TCP->ulSendUnackedNumber = ptr_TCP->ulNextTransmissionNumber; // synchronise
                // inform application that the connection has been close. It is up to the application to command listen again if it so desires
                fnNewTCPState(ptr_TCP, TCP_STATE_CLOSED);
                ptr_TCP->event_listener(ptr_TCP->MySocketNumber, TCP_EVENT_CLOSED, ptr_TCP->ucRemoteIP, ptr_TCP->usRemport);
                return;
            }           
            fnCheckFIN(&rx_tcp_packet, ptr_TCP, (unsigned short)(usLen - ucHeadLen), (TCP_FLAG_FIN | TCP_FLAG_ACK)); // check FIN and return FIN ACK if correct
            break;

 

The final ACK is seen from the recording so it must also arrive to the line above. If it does arrive, why is it not calling the listener? Is the ACK sequence number corrupted? Is your code processing frames and overwriting some of the control structure?

Regards

Mark
« Last Edit: November 14, 2008, 04:12:53 PM by mark »

Offline neil

  • Sr. Member
  • ****
  • Posts: 438
    • View Profile
Re: Socket Not Closing
« Reply #6 on: November 15, 2008, 01:36:51 PM »
Hi Mark,
  This was a really strange one. Within my windows application, I am using the closesocket function within' a thread. This doesnt seem to cause a problem, as I went through the debug side when connecting to my other server application (windows). But when I use closesocket with the 52235 this is when I get the problem. I simply now posted a message to close the socket, which it is now not in a thread, and it works.

Thanks for all your help ..

Regards
Neil

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Socket Not Closing
« Reply #7 on: November 15, 2008, 02:32:49 PM »
Hi Neil

Thanks for the feedback. I can't explain this but if it now works, hopefully all will be well...


regards

Mark