Hallo,
I have one or two questions to sockets on iOS.
I have to work with UDP and TCP sockets on iOS.
What API oder Framework would you suggest me to solve this problem?
Working with low-level BSD sockets? Use a high-level framework? Which one (there are a lot)?
Use special OS classes with more support? I don't know what is the best thing for my issue.
My requirements for the socket issue:
- TCP and UDP needed
- NO_DELAY support needed
- TCP socket with long-term-connection (not only a few seconds)
- To use the sockets in Swift (if possible)
One more hit from me:
Currently, I worked with NWConnection and DispatchQueue API.
I liked this classes until now. But I find no solution for NO_DELAY issue here (which worked).
let tcp = NWProtocolTCP.Options.init()
tcp.noDelay = true
let params = NWParameters.init(tls: nil, tcp: tcp)
connection = NWConnection(to: NWEndpoint.hostPort(host: ipAddress, port: TcpSocket.PORT), using: params)
That was my test to set the NO_DELAY flag for the TCP socket. Is that right?
Would be fine and great to here from you.
Thanks a lot!
What exactly you mean with Network framework?
…
I think I found the first answer in the web
Correct.
Unfortunately I don't see any difference with and without the flag. I see the data in my log a few seconds later …
The Nagle algorithm won’t delay your data for a few seconds (unless your network latency is ginormous). Something else is causing this delay.
Looking at your code, I believe the delay is being triggered by your use of
receiveMessage(…)
. TCP is not a message-oriented protocol, and thus
receiveMessage(…)
doesn’t make any sense [1]. What you want is
receive(minimumIncompleteLength:maximumLength:completion:)
.
Note In iOS 13 beta and friends you can push a framer on to your
NWConnection
. The framer takes care of framing messages to the TCP connection and unframing messages from that connection, allowing you to work in terms of sending and receiving messages. This is super neat but, alas, not available on currently shipping systems.
For more details on this upcoming feature, see WWDC 2019 Session 713 Advances in Networking, Part 2.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
[1] I believe it’s delaying the delivery of data until the TCP connection closes, but I’ve not looked into that case in detail.