所以我正在创建一个输入套接字
CFSocketCreateWithSocketSignature (NULL, &signature, kCFSocketDataCallBack, receiveData, &socket_context);
在receiveData函数(正确调用)中,我试图使用该CFDataRef address
参数找出这个"包"的发件人地址.
发送方PC的IP地址为192.168.1.2.
我正在使用
char buffer[INET_ADDRSTRLEN];
NSLog([NSString stringWithFormat:@"incoming connection from: %s", inet_ntop(AF_INET, address, buffer, INET_ADDRSTRLEN)]);
但是我总是从日志中得到192.6.105.48.是什么赋予了?我在Cocoa/C网络方面真的不大,所以非常感谢任何帮助/解释.
提前致谢!
这是我为我的一个项目实现的NSData的Category类.使用CFDataRef和NSData之间的免费网桥,您可以使用以下类.
@implementation NSData (Additions) - (int)port { int port; struct sockaddr *addr; addr = (struct sockaddr *)[self bytes]; if(addr->sa_family == AF_INET) // IPv4 family port = ntohs(((struct sockaddr_in *)addr)->sin_port); else if(addr->sa_family == AF_INET6) // IPv6 family port = ntohs(((struct sockaddr_in6 *)addr)->sin6_port); else // The family is neither IPv4 nor IPv6. Can't handle. port = 0; return port; } - (NSString *)host { struct sockaddr *addr = (struct sockaddr *)[self bytes]; if(addr->sa_family == AF_INET) { char *address = inet_ntoa(((struct sockaddr_in *)addr)->sin_addr); if (address) return [NSString stringWithCString: address]; } else if(addr->sa_family == AF_INET6) { struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr; char straddr[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, &(addr6->sin6_addr), straddr, sizeof(straddr)); return [NSString stringWithCString: straddr]; } return nil; } @end