iphone - 使用 NSNetService 类与 Windows 计算机上共享的文件夹建立 SMB tcp ip 连接

标签 iphone share smb nsnetservice

我一直在尝试找出一种使用 iPhone 访问我的 Windows 共享文件夹的方法。所需的功能是我正在构建的大型企业应用程序的一部分。有人已经问过类似的问题但没有运气 - Does iOS support file operations via SMB?

现在,我找到了名为“SimpleNetworkStreams ”的苹果开发人员教程,该教程通过将 NSNetService 实例的类型设置为协议(protocol) x-SNSUpload._tcp 来使用 NSNetService 通过 tcp 使用 x-SNSUpload 协议(protocol)

他们是这样做的 -

self.netService = [[[NSNetService alloc] initWithDomain:@"local." type:@"_x-SNSUpload._tcp." name:@"Test"] autorelease];

所以如果我只是替换 _x-SNSUpload._tcp_smb._tcp并在我的 MacBook 上运行 SMB 服务器。我运行以下命令集在我的 MacBook 上启动 SMB

dns-sd -R Test _smb._tcp. "" 12345

nc -l 12345 > tmp.png

然后我就可以将 iPhone 中的图片传输到 MacBook 的根目录中。我希望对 Windows 计算机上的共享文件夹执行相同的操作。

共享文件夹的名称是“测试共享”。我已明确共享 Windows 计算机中的“测试共享”文件夹,并让每个人都可以完全控制。完整的代码细节如下(更新后)

如果我直接在浏览器上输入“smb:\\10.212.19.121”,我就可以访问我的共享文件夹。它会打开查找器应用程序,并为我提供安装“临时共享”文件夹的选项。


更新 - 从上面取出的大量冗余文本以及有关 SimpleNetworkStreams 如何工作以及我调整的内容的更好详细信息放在下面。

代码取自-SimpleNetworkStreams -

  1. 打开我们要发送的文件的 NSInputStream 类型的流
//Open a stream for the file we're going to send

//filepath is a NSString with path to the file on iPhone

self.fileStream = [NSInputStream inputStreamWithFileAtPath:filepath]; 

assert(self.fileStream != nil); 

[self.fileStream open];
  • 正如苹果文档所说
  • "you can either create an NSNetService object directly (if you know the exact host and port information) or you can use an NSNetServiceBrowser object to browse for services. "

    为托管 SMB 服务器的服务器实例化 NSNetService 对象

    // Open a stream to the server, finding the server via Bonjour.  Then configure 
    // the stream for async operation.
    
    //here's the tweak.
    //original code looked like - 
    //self.netService = [[[NSNetService alloc] initWithDomain:@"local." type:@"_x-SNSUpload._tcp." name:@"Test"] autorelease];
    
    self.netService = [[[NSNetService alloc] initWithDomain:@"10.212.19.121" type:@"_smb._tcp." name:@"lanmanserver"] autorelease];
    
    assert(self.netService != nil);
    
    NSDictionary *newDict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"domain\\username",@"password",@"C:\\Documents and Settings\\username\\Desktop\\test%20sharing",nil] forKeys:[NSArray arrayWithObjects:@"u",@"p",@"path",nil]];
    
    [self.netService setTXTRecordData:[NSNetService dataFromTXTRecordDictionary:newDict]];
     
    

    将NSOutputStream类型的输出流对象获取到self.networkStream中。

    success = [self.netService getInputStream:NULL outputStream:&output];
    assert(success);
    
    self.networkStream = output;
    
    [output release];
    
    self.networkStream.delegate = self;
    [self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    
    [self.networkStream open];
    

    然后 NSOutputStream 委托(delegate)捕获我们在输入文件中缓冲的 NSStreamEventHasSpaceAvailable ,然后将该缓冲区写入我们的 NSOutputStream 对象,即 networkStream

    bytesWritten = [self.networkStream write:&self.buffer[self.bufferOffset] maxLength:self.bufferLimit - self.bufferOffset];
    

    最佳答案

    我认为您误解了 NSNetservice。 NSNetService可用于发布Bonjour有关网络中的网络服务的信息。它不会为您创建服务器,它只是告诉客户端有一个提供可用服务的服务器。即使没有这样的服务器,它也会告诉客户端有一个。

    尝试bonjour browser看看 NSNetService 做了什么。您将看到的所有条目均已发布 NSNetServices。
    或者您可以发布类型为 _afpovertcp._tcp 的服务。并观看 Finder 中的侧边栏,了解如何使用 NSNetServices。


    dns-sd -R Test _smb._tcp. "" 12345
    nc -l 12345 > tmp.png
    

    这些行与 SMB 完全无关。仅仅因为您将您的服务宣传为 SMB 并不意味着您的服务器实际上理解 SMB。
    nc(又名 netcat)正如它的名字所暗示的那样。它将您发送给它的所有内容转储到您的文件中。不是中小型企业,根本不是。

    使用 TXTRecords 进行身份验证是一个坏主意,连接到您网络的每个人都会获得 TXTRecords。

    长话短说,NSNetServices不会帮助您创建 SMB 连接。 完成 SMB 服务器的使用后,您可以使用 NSNetServices 告诉网络中的客户端存在 SMB 服务器。但它不会帮助您创建此服务器。

    关于iphone - 使用 NSNetService 类与 Windows 计算机上共享的文件夹建立 SMB tcp ip 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4975469/

    相关文章:

    ios - 单元格间距为零的 UICollectionView

    ios - 升级到 Xcode 8 后设备构建失败

    android - Android 无法创建 Samba 目录

    Android Phonegap 版本 2.0 或更高版本 facebook、twitter、消息等共享插件的任何更新

    java - 尝试列出共享文件夹中的所有文件时连接重置

    Powershell New-SmbMapping 驱动器只能从 Powershell 访问

    iphone - 在应用程序内购买订阅续订未按预期进行

    java - 为什么在 Android 10 操作系统中共享内容时不显示标记为 Intent LinkedIn 的共享托盘图标

    c++ - 有没有办法使用从 GetFileInformationByHandle() 获得的文件 id(FILE_ID_DESCRIPTOR) 打开共享文件夹中的文件

    iphone - 自定义导航栏