2

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

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

他们是这样做的——

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

因此,如果我只是在我的 macbook 上替换_x-SNSUpload._tcp_smb._tcp运行 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];
4

2 回答 2

2

I think you misunderstood NSNetservice. NSNetService can be used to publish Bonjour Information about network services in your network. It doesn't create a server for you, it just tells the clients that there is a server with the service available. Even if there is no such server it will tell the client that there is one.

Try bonjour browser to see what NSNetService does. All the entries you will see are published NSNetServices.
Or you could publish a service with the type _afpovertcp._tcp. and watch the sidebar in finder to get an idea how NSNetServices are used.


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

These lines have absolutely nothing to do with SMB. Just because you are advertising your service as SMB doesn't mean that your server actually understands SMB.
And nc (aka netcat) does exactly what its name suggests. It dumps everything you send to it into your file. Not SMB, not at all.

And using TXTRecords for Authentication is a bad idea, everybody who is connected to your network will get the TXTRecords.

To make a long story short, NSNetServices won't help you with creating SMB connections. Once you are done with the SMB Server you can use NSNetServices to tell clients in your network that there is a SMB Server. But it won't help you in creating this server.

于 2011-03-05T07:55:30.763 回答
1

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

于 2011-03-05T06:52:53.703 回答