Creating a socket client in Objective-C - Mac OSX

孤者浪人 提交于 2020-01-04 04:55:12

问题


I need to basically send some "data" to 98.136.86.109 at port 587. How can I do this in obj-c in my mac app?


回答1:


As Yan notes in his answer, you could use the standard BSD-style networking APIs like socket(), connect(), etc. However, if you want to stay in Objective C and Foundation, then you're looking for NSInputStream and NSOutputStream, which are the stream classes for Cocoa. You should not, however, look at NSSocketPort as that's specifically for use with Distributed Objects.

Apple's "Introduction to Stream Programming Guide for Cocoa" is here: http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Streams/Streams.html

You should start with that document and then check out the class references which have pointers to sample code projects which use NSStream and its subclasses.




回答2:


If you don’t mind using third-party code, AsyncSocket is a popular library that wraps CFSocket and CFStream, providing an Objective-C API for communication via TCP and UDP (AsyncUdpSocket).



来源:https://stackoverflow.com/questions/6240696/creating-a-socket-client-in-objective-c-mac-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!