繁体   English   中英

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

[英]Using NSNetService class to make an SMB tcp ip connection to a folder shared on windows machine

我一直在想办法使用 iPhone 访问我的 Windows 共享文件夹。 所需的功能是我正在构建的更大企业应用程序的一部分。 这是已经问过类似问题但没有运气的人 - iOS 是否支持通过 SMB 进行文件操作?

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

这是他们如何做到的 -

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”,我就可以访问我的共享文件夹。 它会打开 finder 应用程序,并为我提供一个选项来安装“临时共享”文件夹。


更新 -从上面取出的大量冗余文本以及关于 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];
  1. 正如苹果文档所说

“你可以直接创建一个 NSNetService 对象(如果你知道确切的主机和端口信息)或者你可以使用一个 NSNetServiceBrowser 对象来浏览服务。”

为承载 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 委托捕获 NSStreamEventHasSpaceAvailable 我们在输入文件中缓冲的位置,然后将该缓冲区写入我们的 NSOutputStream 对象,即 networkStream

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

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

试试bonjour 浏览器看看 NSNetService 做了什么。 您将看到的所有条目都是已发布的 NSNetServices。
或者您可以发布类型为 _afpovertcp._tcp 的服务。 并查看 finder 中的侧边栏以了解如何使用 NSNetServices。


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

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

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

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

你有没有试过: self.netService = [[[NSNetService alloc] initWithDomain:@"10.212.19.121" type:@"_smb._tcp.local" name:@"lanmanserver"] autorelease];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM