Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nStillConnected

nSuccess := 0

//  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.

oImap := CreateObject("Chilkat.Imap")

oImap:Ssl := 1
oImap:Port := 993

nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Perform a low-level check of the socket connection state.
nStillConnected := oImap:CheckConnection()
IF (nStillConnected == 1)
    ? "The socket is still connected."
ELSE
    ? "The socket is no longer connected."
ENDIF

nSuccess := oImap:Disconnect()
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

oImap:destroy()