sockaddr-in

Swift 3 - CFHostScheduleWithRunLoop crash

时间秒杀一切 提交于 2019-12-25 09:10:09
问题 I am doing a reverse DNS in Swift, my previous code on Swift 2.2 was working fine, also I have implemented it in Objective-C and it works to. However I am not able to make it works in Swift 3.0 Swift 2.2 //: Let's set up the `sockaddr_in` C structure using the initializer. var sin = sockaddr_in( sin_len: UInt8(sizeof(sockaddr_in)), sin_family: sa_family_t(AF_INET), sin_port: in_port_t(0), sin_addr: in_addr(s_addr: inet_addr(ip)), sin_zero: (0,0,0,0,0,0,0,0) ) //: Now convert the structure

How to legally use type-punning with unions to cast between variations of struct sockaddr without violating the strict aliasing rule?

孤街醉人 提交于 2019-12-21 17:32:54
问题 POSIX intends pointers to variations of struct sockaddr to be castable, however depending on the interpretation of the C standard this may be a violation of the strict aliasing rule and therefore UB. (See this answer with comments below it.) I can, at least, confirm that there may at least be a problem with gcc: this code prints Bug! with optimization enabled, and Yay! with optimization disabled: #include <sys/types.h> #include <netinet/in.h> #include <stdio.h> sa_family_t test(struct

Getting an IP address and port number from a sockaddr_in struct in Swift?

允我心安 提交于 2019-12-10 11:40:50
问题 After much trial and error, and without any success, it seems I might need a bit of help on this one: How can I get IP address and port number from a sockaddr_in in the latest version of Swift? I saw some related questions but can't seem to find a proper example anywhere. Also, I don't really seem to grasp how C type structs and pointers need to be handled in Swift, which doesn't really help. Can anyone please supply me with an example or link to useful resources? Many thanks in advance! 回答1:

How to legally use type-punning with unions to cast between variations of struct sockaddr without violating the strict aliasing rule?

試著忘記壹切 提交于 2019-12-04 09:25:47
POSIX intends pointers to variations of struct sockaddr to be castable, however depending on the interpretation of the C standard this may be a violation of the strict aliasing rule and therefore UB. (See this answer with comments below it.) I can, at least, confirm that there may at least be a problem with gcc: this code prints Bug! with optimization enabled, and Yay! with optimization disabled: #include <sys/types.h> #include <netinet/in.h> #include <stdio.h> sa_family_t test(struct sockaddr *a, struct sockaddr_in *b) { a->sa_family = AF_UNSPEC; b->sin_family = AF_INET; return a->sa_family;