mikeash.com pyblog/friday-qa-2014-03-14-introduction-to-the-sockets-api.html commentshttp://www.mikeash.com/?page=pyblog/friday-qa-2014-03-14-introduction-to-the-sockets-api.html#commentsmikeash.com Recent CommentsTue, 19 Mar 2024 07:20:22 GMTPyRSS2Gen-1.0.0http://blogs.law.harvard.edu/tech/rssNic - 2015-03-19 02:32:43http://www.mikeash.com/?page=pyblog/friday-qa-2014-03-14-introduction-to-the-sockets-api.html#commentsAfter developing iOS apps for two years, I stared to want to know this lower-API is written and how to use those in iOS programming. And Thank God, I came across your blog. Really appreciate it. <br /> <br />But, how to port these code to Xcode ? Can we use these code in iOS programming ? Much appreciate if there is example for porting to Xcode and iOS <br /> <br />Thanks 83141113f7bc5507dae881f17c3f2d7aThu, 19 Mar 2015 02:32:43 GMTJose Vazquez - 2014-04-15 15:35:22http://www.mikeash.com/?page=pyblog/friday-qa-2014-03-14-introduction-to-the-sockets-api.html#commentstes: He used asprintf() to create the string. Check out the docs and you'll notice that unlike sprint(), asprintf() will allocate memory for the string. The docs also say it must later be free()'d, which he did.ff1016b857f2415e901d6d661dd30042Tue, 15 Apr 2014 15:35:22 GMTtes - 2014-04-12 21:55:56http://www.mikeash.com/?page=pyblog/friday-qa-2014-03-14-introduction-to-the-sockets-api.html#commentsGreat write up Mike. One small issue, you forgot to allocate memory for portString, but did free it later. <br />8a813aa05d37bccb6fec2160278c0518Sat, 12 Apr 2014 21:55:56 GMTmikeash - 2014-03-19 01:43:32http://www.mikeash.com/?page=pyblog/friday-qa-2014-03-14-introduction-to-the-sockets-api.html#commentsThanks for all the comments. I've fixed up a couple of problems with the article, including a failure to handle the important <code>EINTR</code> error which is not at all optional, and that silly <code>ntohs</code> mixup. <br /> <br />Regarding the AF/PF debate, I'm afraid it's completely obsolete. You'll find that <code>PF_INET</code> and friends no longer exist in the latest POSIX spec. The distinction has never mattered in practice, and now it seems it doesn't even matter in theory.4cd08c5f7d6414d26206f919f736eabaWed, 19 Mar 2014 01:43:32 GMTJakob Egger - 2014-03-18 08:05:53http://www.mikeash.com/?page=pyblog/friday-qa-2014-03-14-introduction-to-the-sockets-api.html#commentsDaniel: Are you sure you can leave out the sin6_len field? I had a problem in one of my apps where connect() would fail on some version of Mac OS X. I thought I fixed it by setting the length field in the sockaddr struct.9ce12b7aaac41cae9d1c4d9af3039045Tue, 18 Mar 2014 08:05:53 GMTAdnako - 2014-03-18 05:54:52http://www.mikeash.com/?page=pyblog/friday-qa-2014-03-14-introduction-to-the-sockets-api.html#comments+1 for PF_INET6 <br /> <br /><a href="http://tools.ietf.org/html/rfc2133">http://tools.ietf.org/html/rfc2133</a> <br />... <br />3.1. <br />... <br />The PF_INET6 is used in the first argument to the socket() function <br />&nbsp;&nbsp;&nbsp;to indicate that an IPv6 socket is being created. <br />...043e1735627bb0336ac9c02fdc7dc74aTue, 18 Mar 2014 05:54:52 GMTAlex Johnson - 2014-03-15 20:21:06http://www.mikeash.com/?page=pyblog/friday-qa-2014-03-14-introduction-to-the-sockets-api.html#commentsI think there's a mistake in the sample socket-writing code. I think you meant to check `<code>writeCursor</code>`, rather then `<code>writeCount</code>`, as in `<code>while (writeCursor &lt; count)</code>`. Really, though, I think it should be `<code>while (writeCount != -1)</code>`, since otherwise it will loop forever when the write fails (right?).0fe735ddeea9c94a3f8887c9f1a7981dSat, 15 Mar 2014 20:21:06 GMTXavier Morel - 2014-03-14 20:45:11http://www.mikeash.com/?page=pyblog/friday-qa-2014-03-14-introduction-to-the-sockets-api.html#comments<code>ntohs</code> converts from network byte order to host byte order, if the port must be in network byte order don't we want <code>htons</code> instead, to convert from our literal in host byte order to network BO? (since it's a 16 bit value it's just going to swap both bytes and work either way, but…)a7dc91e48ba1f89a7ace847bb311a420Fri, 14 Mar 2014 20:45:11 GMTSteve Madsen - 2014-03-14 15:35:56http://www.mikeash.com/?page=pyblog/friday-qa-2014-03-14-introduction-to-the-sockets-api.html#commentsJesper: my best guess is that "XSI" refers to the X/Open System Interface, i.e., POSIX. That comment appears in several header files on other platforms and Mike likely copied it straight out of the header. <br /> <br />A general comment, though: there are many reasons to avoid dropping to POSIX for client-side networking. Apple mentions many of the advantages to sticking with the higher level APIs in the '11 and '12 WWDC videos. Two big ones: automatic tuning of socket buffer sizes for performance and simultaneous connection attempts over both IPv4 and v6 when a destination host name resolves to both. <br /> <br />Unfortunately, if you're writing server-side code, the higher level APIs are far less useful (NS/CFStream can't listen).1e321c43541b0636d242a89e7ca0d34fFri, 14 Mar 2014 15:35:56 GMTDaniel - 2014-03-14 15:12:54http://www.mikeash.com/?page=pyblog/friday-qa-2014-03-14-introduction-to-the-sockets-api.html#commentssockets first parameter (domain) is PF_INET6, not AF_INET6. It chooses the ProtocolFamily, not the AddressFamily. <br /> <br />You don't have to fill the sin6_len field. It's better to leave it alone, just in case you want to port your code to a different POSIX platform one day. <br /> <br />getaddrinfo's port parameter is called "servname" for a reason: It's not (just) the port but the service name. You can say "http". getaddrinfo will look up the default port for this service. (The list it uses is in /etc/services) <br /> <br />Maybe it's good to know that you always should iterate over the results of getaddrinfo until you succeed to connect: In your example, the first result is IPv4 but your machine might run IPv6 only.9a9a6e05276e537826beae9b14ef846bFri, 14 Mar 2014 15:12:54 GMTScott Talbot - 2014-03-14 14:56:41http://www.mikeash.com/?page=pyblog/friday-qa-2014-03-14-introduction-to-the-sockets-api.html#commentsWouldn't you want to use "htons", not "ntohs" for that port specification? <br />Considering that you're translating from Host to Network byte ordering?6fe1019f1ca26b924b6db3638b7b6742Fri, 14 Mar 2014 14:56:41 GMTJesper - 2014-03-14 14:17:26http://www.mikeash.com/?page=pyblog/friday-qa-2014-03-14-introduction-to-the-sockets-api.html#commentsWhat does "XSI" in the code comments mean?bc7038e2077706cf5b66439ace029491Fri, 14 Mar 2014 14:17:26 GMT