Sample code for 30+ languages & platforms
Swift

Check the IMAP Socket Connection

See more IMAP Examples

Demonstrates the Chilkat Imap.CheckConnection method, which checks whether the underlying TCP socket is currently connected to the IMAP server. This performs a low-level socket-state check and does not send an IMAP command. This example connects and checks the socket.

Background: CheckConnection sits between the cached IsConnected and a full protocol round trip: it inspects the actual socket without sending an IMAP command, so it can catch a locally-closed socket that the cached flag would miss. It still cannot detect every half-open connection, though — only a real command like Noop that expects a server reply proves the session is genuinely alive end to end.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Demonstrates the Imap.CheckConnection method, which checks whether the underlying TCP
    //  socket is currently connected to the IMAP server.  This is a low-level socket-state check
    //  and does not send an IMAP command.

    let imap = CkoImap()!

    imap.ssl = true
    imap.port = 993

    success = imap.connect(hostname: "imap.example.com")
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    //  Perform a low-level check of the socket connection state.
    var stillConnected: Bool = imap.checkConnection()
    if stillConnected == true {
        print("The socket is still connected.")
    }
    else {
        print("The socket is no longer connected.")
    }

    success = imap.disconnect()
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }


}