Socket server (swift 3)

Hi all,


I'm working on a swift 3.1 (XCode 8.2/El Capitan) application that will "listen" on a port for incomming socket client connections.

Where could I find some docs that describe the API and how to use it? I know how to do it in C or Java, if it helps.


Thank you in advance.

The best option for doing this on shipping systems is

NetService
. If you pass in the
.listenForConnections
option when you publish the service, it will listen for connections and call the
netService(_:didAcceptConnectionWith:outputStream:)
delegate method for each one. You can then either:
  • Use these

    InputStream
    and
    OutputStream
    instances for your networking
  • Or, if you have existing BSD Sockets-based networking code, extract the sockets from these streams (using

    CFStreamPropertyKey.socketNativeHandle
    ) and pass that down to you code

On iOS 12 and later you should look in to the Network framework.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Socket server (swift 3)
 
 
Q