networking the "right" way

Currently to implement icmp,udp,tcp based protocols(other than HTTP) we have

  • BSD sockets,
  • GCD queues on top of it,
  • CFSocket and NSStream at out disposal.


Is there any single framework that would introduce smallest boilerplate to my code to implement any IP based protocol ?


From higher level API’s Apple recently introduced

  • NWTCPSession NWUDPSession,... but seems mainly for VPN connections.
  • I liked the NSConnection,NSPort,NSHost class hierarchy approach, but is for only distributed objects(i guess xgrid used it in a days) and seems like it’s not updated anymore.
  • CF,NSNetService is Bonjour oriented
  • NSURLSessionStreamTask could be used, but only for TCP

Is there anything i missed ?

(Convenience wrappers like CocoaAsyncSocket, SwifierSockets are not part of this discussion)


I was thinking what is the right way to do networking, which would be reusable and extendable ?

What do you think about approach when protocol structures would be defined in ASN.1 and then you only manipulate protocol logic in your code.

It's standard and used a lot in protocol RFC's. Some of those protocols already have ASN files ready.

I just don’t see how i could make it native for Xcode other than to make ASN compiler that would generate ObjC/Swift code so we could have some autocompletion/syntax check.


Quinn always seemed the one who cares the most about networking and had most useful comments in my opinion.

I would appreciate if he would have something to say.

Answered by DTS Engineer in 136631022

Is there any single framework that would introduce smallest boilerplate to my code to implement any IP based protocol ?

Not really. The only API that handles all IP-based protocols is BSD Sockets, and it requires a lot of boilerplate. As you head up the stack things get easier to use but you get less flexibility. There’s nothing networking-specific about that trend; the same process happens for all other types of frameworks.

NWTCPSession NWUDPSession,... but seems mainly for VPN connections.

Not just mainly. Currently these APIs are only usable by code plugged into the Network Extension framework.

I liked the NSConnection, NSPort, NSHost class hierarchy approach …

NSConnection and NSPort (and its friends) were only intended to be used in a Distributed Objects context and, as such, are not suited for general-purpose networking.

NSHost can be used in other contexts but it has its own set of problems.

None of this stuff is available on iOS.

What do you think about approach when protocol structures would be defined in ASN.1 …

Most folks find ASN.1 quite scary. I kinda like it myself, but I don’t recommend it to folks given that there’s no APIs for it on iOS.

The logical successor to ASN.1 is protobufs, although a lot of folks are happy with JSON.

I was thinking what is the right way to do networking, which would be reusable and extendable ?

There’s no way to answer that without more context. The most critical factor is your intended server infrastructure. By definition, networking involves communicating between peers, so you can make design decisions like this in the context of just one peer.

Share and Enjoy

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

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

Is there any single framework that would introduce smallest boilerplate to my code to implement any IP based protocol ?

Not really. The only API that handles all IP-based protocols is BSD Sockets, and it requires a lot of boilerplate. As you head up the stack things get easier to use but you get less flexibility. There’s nothing networking-specific about that trend; the same process happens for all other types of frameworks.

NWTCPSession NWUDPSession,... but seems mainly for VPN connections.

Not just mainly. Currently these APIs are only usable by code plugged into the Network Extension framework.

I liked the NSConnection, NSPort, NSHost class hierarchy approach …

NSConnection and NSPort (and its friends) were only intended to be used in a Distributed Objects context and, as such, are not suited for general-purpose networking.

NSHost can be used in other contexts but it has its own set of problems.

None of this stuff is available on iOS.

What do you think about approach when protocol structures would be defined in ASN.1 …

Most folks find ASN.1 quite scary. I kinda like it myself, but I don’t recommend it to folks given that there’s no APIs for it on iOS.

The logical successor to ASN.1 is protobufs, although a lot of folks are happy with JSON.

I was thinking what is the right way to do networking, which would be reusable and extendable ?

There’s no way to answer that without more context. The most critical factor is your intended server infrastructure. By definition, networking involves communicating between peers, so you can make design decisions like this in the context of just one peer.

Share and Enjoy

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

let myEmail = "eskimo" + "1" + "@apple.com"
networking the "right" way
 
 
Q