-
Re: Swift setsockopt SOL_SOCKET SO_RCVTIMEO gets invalid argument
Gargoyle Aug 19, 2015 7:49 PM (in response to jphughes)See the answer you got to your exact same post on Stack Overflow.
-
Re: Swift setsockopt SOL_SOCKET SO_RCVTIMEO gets invalid argument
eskimo Aug 20, 2015 2:19 AM (in response to Gargoyle)See the answer you got to your exact same post on Stack Overflow.
Alas, that answer is incorrect. If it were, then the following C code would also fail, which it does not.
BOOL success; int s; struct timeval tv = {3, 100000}; s = socket(AF_INET, SOCK_STREAM, 0); success = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == 0; if (success) { NSLog(@"success"); } else { NSLog(@"error %d", errno); }The actual issue relates to
sizeof. In C this returns 16 (at least in the 64-bit case, which is what I was testing), whereas in Swift it returns 12. That’s a well-known discrepancy related to how padding at the end of structures is handled. If you want the C value from Swift, usestrideof. So, with the following single line change, jphughes’s Swift code works just fine.let tvSize = socklen_t(strideof(timeval))
Except that you’re not allowed to look at
errnowithout confirming that you did actually get an error by first checking the function result. So the end of the code should look more like the C code I posted above:let success = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, tvSize) == 0 if success { NSLog("success"); } else { NSLog("error %d", errno); }ps I filed a bug to get the
strideofgotcha discussed in the "Interacting with C APIs" section of "Using Swift with Cocoa and Objective-C" (r. 22358549)Share and Enjoy
—
Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardwarelet myEmail = "eskimo" + "1" + "@apple.com"
-
Level 1
Level 2
Apple Staff