{"id":54,"date":"2005-07-15T08:47:05","date_gmt":"2005-07-15T07:47:05","guid":{"rendered":"http:\/\/www.paullynch.org\/blog\/?p=54"},"modified":"2005-07-15T08:47:05","modified_gmt":"2005-07-15T07:47:05","slug":"something-simple-like-a-socket-set-maybe","status":"publish","type":"post","link":"http:\/\/www.paullynch.org\/blog\/?p=54","title":{"rendered":"Something simple &#8211; like a socket set, maybe"},"content":{"rendered":"<p>Understand from the start that I have forgotten more about TCP port programming than many people ever get to know.  I&#8217;m not joking, either; I don&#8217;t remember large APIs at all well, and have always relied on decent documentation to keep it all together.  Liz and I sometimes remark that my modus operandi is that I am extremely good at deducing things, but awful at remembering anything.<!-- more --><\/p>\n<p>In the early 90s (probably &#8217;93), I wrote the core communication mechanism of a Reuters feed distribution system, using a copy of Stevens (probably Advanced Unix Programming, I don&#8217;t remember).  Since then I have written a few similar but smaller port handlers, but nothing recently.<\/p>\n<p>So, I approach a project to make a handler for the management interface of <a href=\"http:\/\/www.asterisk.org\/\">Asterisk<\/a> (a very good portable phone switchboard application).  I imagine that I can just look up a couple of classes in the Cocoa docs, and get started.  I&#8217;m sorry, it doesn&#8217;t work that way.  Check out the documentation for NSPort, which leads you to NSSocketPort; they sound like generic port handlers &#8211; but they aren&#8217;t.  Spotlight on NSSocketPort only finds discussions on the Omni macosx-dev list, which is ominous; it turns out that these two classes are poorly named low level components for Distributed Objects &#8211; which is great, but not what I want.<\/p>\n<p>At the moment, it looks like I am forced to choose between using CFSocket, or the basic BSD socket() call, with an NSFileHandle to manage it.  I have also found a link to a project called <a href=\"http:\/\/smallsockets.sourceforge.net\/\">smallsockets<\/a>; this is a wrapper on BSD sockets.  CFSocket is intended to push socket requests into the Run Loop for processing, which avoids the need to write multi-threaded code.  If you are interesting in pursuing this course, look in the documentation for <a href=\"http:\/\/developer.apple.com\/documentation\/Networking\/Conceptual\/CFNetwork\/index.html\">CFNetwork<\/a> to get the overview.<\/p>\n<p>CFSocket is in 10.0, but luckily it has a Cocoa wrapper class, NSStream (although actually it is a wrapper for CFStream), which is however only in 10.3 and above.  So that looks like the one to use.<\/p>\n<p>The Programming Topic in the documentation on Streams covers the basic code set up very well, and a test app mostly copied from there works very well as a pseudo telnet client.  If you want to write a server, you need to wait for connections, which the NSStreams won&#8217;t do, so you have to directly program CFSocket for that.  There&#8217;s a decent example <a href=\"http:\/\/homepage.mac.com\/jrc\/contrib\/\">here<\/a> (you&#8217;ll will have to scroll down to &#8220;Source Code&#8221; to find the files).<\/p>\n<p>Example code to come, once I write it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understand from the start that I have forgotten more about TCP port programming than many people ever get to know. I&#8217;m not joking, either; I don&#8217;t remember large APIs at all well, and have always relied on decent documentation to keep it all together. Liz and I sometimes remark that my modus operandi is that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[12],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-programming-for-apples","tag-cocoa"],"_links":{"self":[{"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=54"}],"version-history":[{"count":0,"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}