Author Topic: TCP (Closing Connection)  (Read 10470 times)

Offline kmjackson

  • Newbie
  • *
  • Posts: 33
    • View Profile
TCP (Closing Connection)
« on: December 08, 2009, 07:14:28 AM »
I have a SAM7X board operating as a CLIENT, which initiates communication with a server.  After receiving the desired data, I want to the SAM7X to initiates the close sequence.  How is this done?

Thanks

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: TCP (Closing Connection)
« Reply #1 on: December 08, 2009, 02:12:05 PM »
Hi

Either a client of a server can initiate the close of a TCP connection by calling fnTCP_close(socket_handle);

There are some more details here:
http://www.utasker.com/forum/index.php?topic=472.0

Regards

Mark

Offline kmjackson

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: TCP (Closing Connection)
« Reply #2 on: December 09, 2009, 07:52:10 AM »
Hi Mark,

Thanks for the info.  I have another question regarding tracking the TCP states.  I noticed in fnTELNETListener, you track the states and set the the TCP_EVENT_ABORT: case back to the listening state.  Should I be tracking and assigning things likewise?



// Telnet client/server standard call back
//
static int fnTELNETListener(USOCKET Socket, unsigned char ucEvent, unsigned char *ucIp_Data, unsigned short usPortLen)
{

case TCP_EVENT_CONNECTED:                                            // TCP connection has been established
        TELNET_session->ucState = TELNET_STATE_CONNECTED;
        return (TELNET_session->fnApp(Socket, TCP_EVENT_CONNECTED, ucIp_Data, usPortLen));

    case TCP_EVENT_ABORT:
        fnTCP_Listen(Socket, TELNET_session->usTelnetPortNumber, ptrTELNET->usMaxWindow); // set back to listening state
        fnResetTelnetSession(TELNET_session);
        TELNET_session->fnApp(Socket, TCP_EVENT_ABORT, ucIp_Data, usPortLen);
        // fall through intentionally
    case TCP_EVENT_CLOSE:
        return (TELNET_session->fnApp(Socket, TCP_EVENT_CLOSE, ucIp_Data, usPortLen));    

    case TCP_EVENT_CLOSED:
        TELNET_session->fnApp(Socket, TCP_EVENT_CLOSED, ucIp_Data, usPortLen);
        fnResetTelnetSession(TELNET_session);
        fnTCP_Listen(Socket, TELNET_session->usTelnetPortNumber, ptrTELNET->usMaxWindow); // set TCP port back to listening state
        break;

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: TCP (Closing Connection)
« Reply #3 on: December 09, 2009, 02:13:05 PM »
Hi

TELNET is a sort of application on top of the TCP layer so it needs to also keep a bit of information for itself about the state of the TCP connection. Your TCP based application may also need to do this, or parts of it to keep track of things but that is mostly up to you.

In the case of the connection being closed at a server it is up to the server application to set it back to the listening state if it wants to be able to receive further connections on it (otherwise it will function as a 'single-shot' TCP connection and will no longer accept further connections after it has been closed). If your application is a server it is generally necessary to put it back to listening state on a connection close.

Regards

Mark