使用NSNetService类与Windows机器上共享的文件夹建立SMB tcp ip连接
我一直试图找到一种方法来使用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];
所以如果我只是用_smb._tcp替换_x-SNSUpload._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应用程序并为我提供了安装“temp sharing”文件夹的选项。
更新 - 从上面取出的大量冗余文本以及有关SimpleNetworkStream如何工作以及我调整的内容的更多详细信息如下所示。
代码取自 - SimpleNetworkStreams -
打开我们要发送的文件的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];
正如苹果文档所说的那样
“您可以直接创建NSNetService对象(如果您知道确切的主机和端口信息),也可以使用NSNetServiceBrowser对象来浏览服务。”
NSNetService的对象是为托管SMB服务器的服务器实例化的
// 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];
没有找到相关结果
已邀请:
2 个回复
死簇
这些行与SMB完全无关。仅仅因为您正在宣传您的服务,因为SMB并不意味着您的服务器实际上了解SMB。 而nc(又名netcat)正如其名称所暗示的那样。它会将您发送到它的所有内容转储到您的文件中。不是SMB,根本不是。 使用TXTRecords进行身份验证是一个坏主意,连接到您的网络的每个人都将获得TXTRecords。 简而言之,NSNetServices无法帮助您创建SMB连接。 完成SMB服务器后,您可以使用NSNetServices告知网络中的客户端有SMB服务器。但它不会帮助您创建此服务器。
肉簧咸缮