我正在使用Apple的TCPServer类来打开来自传入连接的套接字。以下是有人在侦听套接字上连接时调用的回调:
static void TCPServerAcceptCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {
TCPServer *server = (TCPServer *)info;
if (kCFSocketAcceptCallBack == type) {
// for an AcceptCallBack, the data parameter is a pointer to a CFSocketNativeHandle
CFSocketNativeHandle nativeSocketHandle = *(CFSocketNativeHandle *)data;
uint8_t name[SOCK_MAXADDRLEN];
socklen_t namelen = sizeof(name);
NSData *peer = nil;
if (0 == getpeername(nativeSocketHandle, (struct sockaddr *)name, &namelen)) {
peer = [NSData dataWithBytes:name length:namelen];
}
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
CFStreamCreatePairWithSocket(kCFAllocatorDefault, nativeSocketHandle, &readStream, &writeStream);
if (readStream && writeStream) {
CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
[server handleNewConnectionFromAddress:peer inputStream:(NSInputStream *)readStream outputStream:(NSOutputStream *)writeStream];
} else {
// on any failure, need to destroy the CFSocketNativeHandle
// since we are not going to use it any more
close(nativeSocketHandle);
}
if (readStream) CFRelease(readStream);
if (writeStream) CFRelease(writeStream);
}
}
有没有办法获取正在连接的设备的主机名? 问候, 佐
[编辑] 我试过这样的东西来提取ip:
struct sockaddr_in *s = (struct sockaddr_in*)name;
char *ipstr = malloc(INET_ADDRSTRLEN);
ipstr = inet_ntoa(s->sin_addr);
NSLog(@"ip = %s", ipstr);
但它给了我0.0.0.0。
[编辑] 仍在努力,仍然没有成功。根据apple的doc,地址参数是:
保存结构sockaddr内容的CFData对象,适用于s的协议族(例如struct sockaddr_in或struct sockaddr_in6),标识s所连接的远程地址。除了kCFSocketAcceptCallBack和kCFSocketDataCallBack回调之外,此值为NULL。
但是我无法提取除132.20.0.1之外的任何其他IP(这显然不是我期望的IP)我真的很困惑,如果有人可以帮助我的话会很棒!
答案 0 :(得分:2)
在尝试按照您的方式获取地址后,我想出了一个适合我的代码的轻微变体。如果您正在处理IPv6地址,则此代码正常工作,但很容易更改为使用IPv4。无论如何,我将不得不更改代码。
- (void)getPublicClientAddress {
// Get public IP from stream
// Get hands on appropriate data structures via the socket number
CFSocketNativeHandle nativeSocketHandle = _socketnumber;
uint8_t name[SOCK_MAXADDRLEN];
socklen_t namelen = sizeof(name);
NSData *peer = nil;
if (0 == getpeername(nativeSocketHandle, (struct sockaddr *)name, &namelen)) {
peer = [NSData dataWithBytes:name length:namelen];
}
// TODO distiguish between ipv4 and ipv6
struct sockaddr_in6 *s = (struct sockaddr_in6*)name;
// convert ip to string
char *ipstr = malloc(INET6_ADDRSTRLEN);
struct in6_addr *ipv6addr = &s->sin6_addr; // used pointer here, but that really should make no difference
inet_ntop(AF_INET6, ipv6addr, ipstr, sizeof(ipstr)); // used a different function here
// convert port to int
int portnumber = s->sin6_port;
// Save in properties
_publicIP = [NSString stringWithFormat:@"%s", ipstr];
_publicPort = [NSString stringWithFormat:@"%d", portnumber];
}
所以这段代码对我有用,我希望它可以帮助那些在搜索答案时偶然发现这篇文章的人。