TCP sockets with Bonjour-discovered address
TCP sockets with Bonjour-discovered address
- Subject: TCP sockets with Bonjour-discovered address
- From: Carl Hoefs <email@hidden>
- Date: Tue, 01 Jul 2014 14:22:42 -0700
I've been using udp sockets on OS X & iOS with Bonjour to success, but now I have more data to transfer so I'm trying to use IPv6 tcp sockets. Everything seems to work up to the point where I try to connect(2) to a Bonjour-discovered service address. It fails with "Address family not supported by protocol family". Looking at the structs in the debugger, I can't see what it's not liking. Code and struct dump follow.
Code (w/error checking removed):
// create and bind the tcp socket to the struct
int tcp_socket = socket(AF_INET6,SOCK_STREAM,IPPROTO_TCP);
bzero(&mysaddr6_struct, sizeof(mysaddr6_struct));
mysaddr6_struct.sin6_len = sizeof(mysaddr6_struct);
mysaddr6_struct.sin6_family = AF_INET6;
mysaddr6_struct.sin6_addr = in6addr_any;
mysaddr6_struct.sin6_port = htons(0);
bind(tcp_socket,(struct sockaddr *)&mysaddr6_struct,sizeof(mysaddr6_struct));
socklen_t len = sizeof(mysaddr6_struct);
getsockname(tcp_socket,(struct sockaddr *)&mysaddr6_struct,&len);
// create bonjour service, publish, etc.
svc = [[NSNetService alloc] initWithDomain:AP2P_BONJOUR_DOMAIN
type:bonjourType
name:[[UIDevice currentDevice]name]
port:ntohs(bjtcpsaddr6_struct.sin6_port)];
[svc publish];
[svc resolveWithTimeout:15];
tcp_browser = [[NSNetServiceBrowser alloc] init];
[tcp_browser setDelegate:self];
[tcp_browser searchForServicesOfType:bonjourType
inDomain:AP2P_BONJOUR_DOMAIN];
. . .
// after discovery, try to connect(2) to the other side...
for (NSNetService *service in tcp_services) {
for (NSData *address in [service addresses]) {
if (sockaddr_struct6->sin6_family == AF_INET) continue;
struct sockaddr_in6 *sockaddr_struct6 = (struct sockaddr_in6 *)[address bytes];
connect(tcp_socket,(struct sockaddr *)&sockaddr_struct6, sizeof(struct sockaddr_in6));
*** ERROR ==> Address family not supported by protocol family; return status = -1
}
}
sockaddr struct going into the connect(2) call:
sockaddr_struct6 sockaddr_in6 * 0x145aa130 0x145aa130
sin6_len __uint8_t '\x1c' '\x1c'
sin6_len __uint8_t '\x1c' '\x1c'
sin6_family sa_family_t '\x1e' '\x1e'
sin6_port in_port_t 55257 55257
sin6_flowinfo __uint32_t 0 0
sin6_addr in6_addr <1c1ed9d7 00000000 fe800000 00000000 7e6d62ff fe722337 06000000>
__u6_addr in6_addr::<anonymous union>
__u6_addr8 __uint8_t [16]
[0] unsigned char '\xfe' '\xfe'
[1] unsigned char '\x80' '\x80'
[2] unsigned char '\0' '\0'
[3] unsigned char '\0' '\0'
[4] unsigned char '\0' '\0'
[5] unsigned char '\0' '\0'
[6] unsigned char '\0' '\0'
[7] unsigned char '\0' '\0'
[8] unsigned char '~' '~'
[9] unsigned char 'm' 'm'
[10] unsigned char 'b' 'b'
[11] unsigned char '\xff' '\xff'
[12] unsigned char '\xfe' '\xfe'
[13] unsigned char 'r' 'r'
[14] unsigned char '#' '#'
[15] unsigned char '7' '7'
__u6_addr16 __uint16_t [8]
__u6_addr32 __uint32_t [4]
sin6_scope_id __uint32_t 6 6
I hope someone can point out the mistake I'm making here.
Thx!
-Carl
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden