Details for NWConnection.send(...)?

I'm attempting to port LightMQTT to Apple's new Network stuff. But I'm finding it really tough to figure things out, because documentation seems really sparse. In particular, I'm trying to figure out what I can do with

NWConnection.func send(content: Data?, contentContext: NWConnection.ContentContext = .defaultMessage, isComplete: Bool = true, completion: NWConnection.SendCompletion)
.


Is content my bytes to send? Why is it optional?

What is

isComplete
supposed to be set as?

Are there even any examples/sample code for this stuff?

Or is it just too early to be delving into this?

Accepted Answer

Is content my bytes to send?

Yes.

Why is it optional?

Because you might want to send metadata without sending data. This more often crops up in datagram-based protocols rather than stream-based protocols (like TCP). However, even in TCP there is one case of interest here, namely EOF. If you want to end the send side of a TCP connection without sending any data, you’d pass nil to

content
and true to
isComplete
.

What is

isComplete
supposed to be set as?

Again, this is more interesting in the datagram case. For TCP, see above.

Are there even any examples/sample code for this stuff?

In terms of official sample code, the only one currently available is nwcat. This uses the C API, but most of the concepts just map across.

On the Swift front, I’ve been working on that but my ‘day job’ (answering questions) keeps getting in the way )-: I posted some preliminary snippets to this Swift Forums thread.

Or is it just too early to be delving into this?

Not IMO. My experience with Network framework has been very positive, especially when using it from Swift. There’s still a few things you can’t do — like UDP multicast — but for the things you can do it tends to work really well.

Keep in mind that the underlying code for Network framework (the private

libnetcore
library) is also used by CFNetwork, so the core code is very solid because it’s exercised every time someone uses
URLSession
.

Share and Enjoy

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

let myEmail = "eskimo" + "1" + "@apple.com"

It's not too early. I've been playing with Network.framework for an iOS SDR app, for both TCP and UDP streaming, and have found it far easier to use than CFNetworking. You have to look for some of the documentation in the header filles.

Details for NWConnection.send(...)?
 
 
Q