Cocoa Touch Bonjour如何处理NSNetService地址和uint8_t

发布于 2024-11-24 00:30:23 字数 537 浏览 1 评论 0原文

我正在尝试使 iOS 应用程序与使用 Bonjour 并使用 HTTP 命令的服务器进行通信。到目前为止,我已经能够找到本地域并找到我正在寻找的特定服务。我能够解析服务的地址,但我不知道如何从该地址中获取有用的内容。 NSNetService 的地址是一个 NSData 对象,我不知道如何处理它。我需要发送 GET 和 PUT 等命令。哪些可可类可以处理这样的事情?

我还尝试从服务获取输入和输出流,但它们似乎是非常低级别的流,我不知道如何正确处理缓冲区等。

[service getInputStream:&inputStream outputStream:&outputStream]

NSOutputStream write 方法接受一个 uint8_t 缓冲区,我不知道如何创建它。 NSInputStream read 方法返回一个 uint8_t 缓冲区,我不知道如何解释它。

我可以使用终端命令与该服务器进行通信。例如,向它发送命令 LIST 会导致它打印出我正在查找的文件列表。我如何在 Cocoa 中发送和获取这样的信息?

I'm attempting to make an iOS app communicate with a server that uses Bonjour and uses HTTP commands. So far I have been able to find the local domain and locate the particular service I'm looking for. I am able to resolve the address of the service, but I don't know how to get something useful out of the address. The address from the NSNetService is a NSData object and I have no idea what to do with it. I need to send commands like GET and PUT. What cocoa classes handle things like this?

I also tried getting input and output streams from the Service, but they seem to be extremely low level streams and I don't know how to properly deal with buffers and all that.

[service getInputStream:&inputStream outputStream:&outputStream]

the NSOutputStream write method takes in a uint8_t buffer which I have no idea how to create.
the NSInputStream read method returns a uint8_t buffer and I don't know how to interpret it.

I am able to communicate with this server using terminal commands. For instance, sending it the command LIST causes it to print out the list of files I am looking for. How do I send and get information like this in Cocoa?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

段念尘 2024-12-01 00:30:23

将数据写入输出流,从而将其发送到服务器:

NSString * stringToSend = @"Hello World!\n"; //The "\n" lets the receiving method described below function correctly. I don't know if you need it or not.
NSData * dataToSend = [stringToSend dataUsingEncoding:NSUTF8StringEncoding];
if (outputStream) {
    int remainingToWrite = [dataToSend length];
    void * marker = (void *)[dataToSend bytes];
    while (0 < remainingToWrite) {
        int actuallyWritten = 0;
        actuallyWritten = [outputStream write:marker maxLength:remainingToWrite];
        remainingToWrite -= actuallyWritten;
        marker += actuallyWritten;
    }
}

您可以像这样发送任何数据,只需将其放入 NSData 对象中即可。

要从服务器接收数据,请在输入流的 NSStreamDelegate 中使用此代码:

- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)streamEvent {
    NSInputStream * istream;
    NSOutputStream * ostream;
    switch(streamEvent) {
        case NSStreamEventHasBytesAvailable:;
            istream = (NSInputStream *)aStream;
            ostream = (NSOutputStream *)CFDictionaryGetValue(connections, istream);

            uint8_t buffer[2048];
            int actuallyRead = [istream read:(uint8_t *)buffer maxLength:2048];
            if (actuallyRead > 0) {
                NSData *data;
                data = [NSData dataWithBytes:buffer length:actuallyRead];
                NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]autorelease];
                string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
                //Do something with the string...
            }
                break;
        case NSStreamEventEndEncountered:;
            istream = (NSInputStream *)aStream;
            ostream = nil;
            if (CFDictionaryGetValueIfPresent(connections, istream, (const void **)&ostream)) {
                [self shutdownInputStream:istream outputStream:ostream];
            }
                break;
        case NSStreamEventHasSpaceAvailable:
        case NSStreamEventErrorOccurred:
        case NSStreamEventOpenCompleted:
        case NSStreamEventNone:
        default:
            break;
    }
}

查看 Apple 的 CocoaEcho 示例代码。它应该对你有帮助。

To write data to the output stream, therefore sending it to the server:

NSString * stringToSend = @"Hello World!\n"; //The "\n" lets the receiving method described below function correctly. I don't know if you need it or not.
NSData * dataToSend = [stringToSend dataUsingEncoding:NSUTF8StringEncoding];
if (outputStream) {
    int remainingToWrite = [dataToSend length];
    void * marker = (void *)[dataToSend bytes];
    while (0 < remainingToWrite) {
        int actuallyWritten = 0;
        actuallyWritten = [outputStream write:marker maxLength:remainingToWrite];
        remainingToWrite -= actuallyWritten;
        marker += actuallyWritten;
    }
}

You can send any data like this, just put it in a NSData object.

To receive data from the server use this code in the input stream's NSStreamDelegate:

- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)streamEvent {
    NSInputStream * istream;
    NSOutputStream * ostream;
    switch(streamEvent) {
        case NSStreamEventHasBytesAvailable:;
            istream = (NSInputStream *)aStream;
            ostream = (NSOutputStream *)CFDictionaryGetValue(connections, istream);

            uint8_t buffer[2048];
            int actuallyRead = [istream read:(uint8_t *)buffer maxLength:2048];
            if (actuallyRead > 0) {
                NSData *data;
                data = [NSData dataWithBytes:buffer length:actuallyRead];
                NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]autorelease];
                string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
                //Do something with the string...
            }
                break;
        case NSStreamEventEndEncountered:;
            istream = (NSInputStream *)aStream;
            ostream = nil;
            if (CFDictionaryGetValueIfPresent(connections, istream, (const void **)&ostream)) {
                [self shutdownInputStream:istream outputStream:ostream];
            }
                break;
        case NSStreamEventHasSpaceAvailable:
        case NSStreamEventErrorOccurred:
        case NSStreamEventOpenCompleted:
        case NSStreamEventNone:
        default:
            break;
    }
}

Take a look at Apple's CocoaEcho Sample Code. It should help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文