Most effective way to do networking on Mac/iPhone?

2019-01-09 14:38发布

What would be the most effective way (that is, best ratio of effort vs readability and maintainability) to establish TCP connection or send UDP datagrams on Mac and iPhone? I'm very familiar with classic BSD sockets, but I'm not aiming for portability right now; I'm just trying to be quickly done with a small project.

I'd love to have an Apple-provided Objective-C wrapper, but I'd like to hear some thoughts on what people are commonly doing. If you think BSD sockets are the way to go, feel free to punch in some thoughts, too: I'm really just interested in finding the "right way" to do it.

Let's presume the protocol to be a custom one, so our needs cannot be serviced by the Cocoa-based HTTP client classes :)


My current aim is to stream device status (e.g. accelerometer) over the network over UDP while also maintaining signaling and delivery-guaranteed-event (e.g. button pressed) connection over TCP. A desktop application would display this status.

However, my current application is just what made me ask the question; I'm wondering what people generally do. When I last seriously worked with VB6 about 7-8 years ago, WinSock ActiveX control did things a bit different than what is done with BSD sockets, no matter what it did under the hood.

2条回答
仙女界的扛把子
2楼-- · 2019-01-09 14:45

For networking, the simple answer is,

(1) ASIHttpRequest ........ NOTE - this QA is very old. Unfortunately ASIHttpRequest is of historic interest only and is no longer available.

(2) AsyncSocket

(3) When you're starting out ... GameKit (a child can use it)

(4) Just use Bonjour (two lines of code) to find other devices.

"an Apple-provided Objective-C wrapper" ... AsyncSockets (written originally by the mysterious Dustin J. Voss) is SO GOOD that Apple just won't bother trying to write one!

AsyncSockets is exactly what you're after. It is used everywhere in the 300,000 iFone apps. It "is" networking on the iFone. Hope it helps.

AGAIN NOTE - this QA is very old. You can easily now find "modern versions of" software like AsyncSockets.

BTW, it's worth nothing that there is absolutely nothing wrong - at all - with just using GK .. the performance can be spectacular. About the only downside is that pairing simply takes a plain long time with GK. And it's a childish mess created for ten year olds learning programming. You will be able to do 100x faster pairing working directly with AsyncSockets.

You may prefer to use some toy GK code, while you figure out Bonjour and all that.

I would suggest the consensus of opinion is that there is no point working at an even lower level (ie, raw sockets) than AsyncSocket .. there's not much more performance to be had in there.

NOTE - if you are new to gamekit / iOS be sure to read this critical tip!
Client/Server GKSessions

hope it helps.

查看更多
倾城 Initia
3楼-- · 2019-01-09 15:00

Your question can't really be answered with any detail because you haven't described what you want to do. The best answer I can give is to read the introductory networking guides for Mac and iOS.

Update

I don't think there's any "easy Cocoa way" built in for UDP. I recall this discussion has come up several times on the cocoa-dev list. The cocoaasyncsocket project has been mentioned various places. It claims:

AsyncUdpSocket is a UDP/IP socket networking library that wraps CFSocket. It works almost exactly like the TCP version, but is designed specifically for UDP. This includes queued non-blocking send/receive operations, full delegate support, run-loop based, self-contained class, and support for IPv4 and IPv6.

查看更多
登录 后发表回答