我正在尝试使用此代码建立套接字连接
//Server code
- (void)startServer{
sockPort = [[NSSocketPort alloc] initWithTCPPort:9786];
NSConnection *theConnection;
theConnection = [[NSConnection alloc] initWithReceivePort:sockPort sendPort:nil];
theConnection=[[NSConnection alloc] init];
[theConnection setRootObject:self];
[theConnection registerName:@"Server Connection"];
}
//Client Code
-(void)startClient{
NSConnection *conn=[NSConnection connectionWithReceivePort:nil sendPort:sockPort];
NSLog(@"Socket descriptor=%d",[sockPort socket]);
}控制台
Socket descriptor=-1为什么套接字描述符显示为-1?
发布于 2012-02-23 02:48:27
发布的代码没有特别的意义-但是如果我们假设代码的客户端更传统一些,看起来像这样:
- (void)startClient
{
NSSocketPort *sockPort = [[NSSocketPort alloc] initRemoteWithTCPPort: 1234
host: @"hostname"];
NSConnection *connection = [NSConnection connectionWithReceivePort: nil
sendPort: sockPort];
....这里sockPort上的套接字应该是-1,因为它不代表该进程中的套接字,而是这样的套接字应该连接到的目的地。
更有趣的是connection对象上的receivePort和sendPort属性。
发布于 2011-12-21 19:57:23
你有一个双倍的配额。第二个应该删除。
https://stackoverflow.com/questions/8587665
复制相似问题