我正在尝试在 MacOSX 10.7.2 (Lion) 下设置 SilverLight 策略服务器。这要求我创建一个套接字并将其绑定到端口 943,因为 SilverLight 请求此端口上的策略文件。不幸的是,我似乎无法在该端口绑定,因为绑定调用失败,errno=49.
我想我无权访问该端口。也许我需要root权限?还是我需要将此端口转发到另一个我可以绑定的端口?我对网络编程有点陌生,所以非常感谢任何帮助!我还附上了我的源代码。也许我做错了什么,尽管如果我使用 SilverLight 受限端口 4502-4532 可以正常工作,一旦成功提供策略文件,SilverLight 就会在该端口上进行通信。
- (void) start {
CFSocketRef socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL);
if (!socket) {
[self errorWithName:@"Unable to create socket."];
return;
}
int reuse = true;
CFSocketNativeHandle fileDescriptor = CFSocketGetNative(socket);
if (setsockopt(fileDescriptor, SOL_SOCKET, SO_REUSEADDR,
(void *)&reuse, sizeof(int)) != 0) {
NSLog(@"Unable to set socket options.");
return;
}
struct sockaddr_in address;
memset(&address, 0, sizeof(address));
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
address.sin_port = htons(943);
CFDataRef addressData = CFDataCreate(NULL, (const UInt8 *)&address, sizeof(address));
[(id)addressData autorelease];
CFSocketError error = CFSocketSetAddress(socket, addressData);
if (error < 0) {
NSLog(@"Error bind %d\n", errno); //fails here for port 943.
return;
}
NSFileHandle *listeningHandle = [[NSFileHandle alloc]
initWithFileDescriptor:fileDescriptor
closeOnDealloc:YES];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(receiveIncomingConnectionNotification:)
name:NSFileHandleConnectionAcceptedNotification
object:nil];
[listeningHandle acceptConnectionInBackgroundAndNotify];
}