Sample code for 30+ languages & platforms
Swift

Move a Connection into a Socket Set

See more Socket/SSL/TLS Examples

Demonstrates Socket.TakeSocket, which moves a connection into a newly created member of this object's socket set. After one or more calls, the object acts as a set of sockets.

Background. A socket set groups multiple connections so they can be monitored together with the SelectForReading and SelectForWriting methods. After success, the source socket holds no connection.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let listenSock = CkoSocket()!

    success = listenSock.bindAndListen(port: 5000, backlog: 25)
    if success == false {
        print("\(listenSock.lastErrorText!)")
        return
    }

    let connectedSock = CkoSocket()!
    success = listenSock.acceptNext(maxWaitMs: 20000, socket: connectedSock)
    if success == false {
        print("\(listenSock.lastErrorText!)")
        return
    }

    //  Move the connection out of connectedSock and into a newly created member of this object's socket
    //  set.  After one or more such calls, socketSet acts as a set of connected sockets (useful with the
    //  SelectForReading / SelectForWriting methods).  After success, connectedSock holds no connection.
    let socketSet = CkoSocket()!
    success = socketSet.take(sock: connectedSock)
    if success == false {
        print("\(socketSet.lastErrorText!)")
        return
    }

    print("Connection added to the socket set.")

}