Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective-C中的套接字编程_Objective C_Macos_Sockets_Stream_Nsstream - Fatal编程技术网

Objective-C中的套接字编程

Objective-C中的套接字编程,objective-c,macos,sockets,stream,nsstream,Objective C,Macos,Sockets,Stream,Nsstream,目前我想编程一个聊天服务器。不过,我真的很难理解这些文档 目前,这是我的代码,基本上是从开发人员库中提取的: #import "ServerSide.h" @implementation ServerSide @synthesize socket; @synthesize socketAddress; @synthesize handleConnect; @synthesize portNumber; - (id)init{ self = [super init]; if

目前我想编程一个聊天服务器。不过,我真的很难理解这些文档

目前,这是我的代码,基本上是从开发人员库中提取的:

#import "ServerSide.h"

@implementation ServerSide

@synthesize socket;
@synthesize socketAddress;
@synthesize handleConnect;
@synthesize portNumber;

- (id)init{
    self = [super init];
    if (self) {
        self.socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, handleConnect, NULL);
    }
    return self;
}

- (void) bind {
    memset(&socketAddress, 0, sizeof(socketAddress));
    socketAddress.sin_len = sizeof(socketAddress);
    socketAddress.sin_family = AF_INET; /* Address family */
    socketAddress.sin_port = htons(self.portNumber); /* Or a specific port */
    socketAddress.sin_addr.s_addr= INADDR_ANY;

    CFDataRef sincfd = CFDataCreate(kCFAllocatorDefault, (UInt8 *)&socketAddress, sizeof(socketAddress));
    CFSocketSetAddress(socket, sincfd);
    CFRelease(sincfd);
}

- (void) listen {
    CFRunLoopSourceRef socketsource = CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), socketsource, kCFRunLoopDefaultMode);
    CFSocketGetNative(socket);
}
现在,“handleconnect”是一个没有初始化的CFSocketCallBack ivar

现在我看到其他人使用不同的实现来创建套接字服务器,但这是文档中的一个,我想在此基础上构建

我目前正在尝试连接到服务器,但看起来这甚至没有打开套接字。我似乎也无法使用telnet localhost“port#”通过终端连接到它

以下是客户端实现:

#import "Client.h"

@implementation Client
@synthesize host;
@synthesize port;
@synthesize readStream;
@synthesize writeStream;
@synthesize inputStream;
@synthesize outputStream;

- (void)setup {

    NSLog(@"Setting up connection to %@ : %i", [[NSURL URLWithString:host] absoluteString], port);
    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef)[[NSURL URLWithString:host] host], port, &readStream, &writeStream);

    if(!CFWriteStreamOpen(writeStream)) {
        NSLog(@"Error, writeStream not open");
        return;
    }
}

- (void)open {
    NSLog(@"Opening streams.");

    inputStream = (__bridge_transfer NSInputStream *)readStream;
    outputStream = (__bridge_transfer NSOutputStream *)writeStream;

    [inputStream setDelegate:self];
    [outputStream setDelegate:self];

    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream open];
    [outputStream open];
}

- (void)close {
    NSLog(@"Closing streams.");

    [inputStream close];
    [outputStream close];

    [inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream setDelegate:nil];
    [outputStream setDelegate:nil];

    inputStream = nil;
    outputStream = nil;
}

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event {
    NSLog(@"Stream triggered.");

    switch(event) {
        case NSStreamEventHasSpaceAvailable: {
            if(stream == outputStream) {
                NSLog(@"outputStream is ready.");
            }
            break;
        }
        case NSStreamEventHasBytesAvailable: {
            if(stream == inputStream) {
                NSLog(@"inputStream is ready.");

                uint8_t buf[1024];
                unsigned int len = 0;

                len = (int)[inputStream read:buf maxLength:1024];

                if(len > 0) {
                    NSMutableData* data=[[NSMutableData alloc] initWithLength:0];
                    [data appendBytes:(const void *)buf length:len];
                    [self readIn:[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]];
                    data = nil;
                }
            }
            break;
        }
        default: {
            NSLog(@"Stream is sending an Event: %lu", event);
            break;
        }
    }
}

- (void)readIn:(NSString *)s {
    NSLog(@"Reading in the following:");
    NSLog(@"%@", s);
}

- (void)writeOut:(NSString *)s {
    uint8_t *buf = (uint8_t *)[s UTF8String];

    [outputStream write:buf maxLength:strlen((char *)buf)];

    NSLog(@"Writing out the following:");
    NSLog(@"%@", s);
}


@end
我在指定的端口上运行服务器,然后告诉客户端连接到指定的主机和端口号

但是现在我如何在我打开的套接字中传递数据

我不希望有什么大的解释,但如果有人能让我更好地理解需要做些什么来推进这个实现,我将不胜感激