使用NSNetService类与Windows机器上共享的文件夹建立SMB tcp ip连接 [英] Using NSNetService class to make an SMB tcp ip connection to a folder shared on windows machine

查看:186
本文介绍了使用NSNetService类与Windows机器上共享的文件夹建立SMB tcp ip连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to figure out a way to access my windows shared folder using iPhone. The desired functionality is part of bigger enterprise app I am building. Here is someone who has already asked a similar question but no luck - Does iOS support file operations via SMB?

现在,我找到了名为 SimpleNetworkStreams 通过将NSNetService实例的类型设置为协议x-SNSUpload._tcp来使用NSNetService在tcp上使用x-SNSUpload协议

Now, I have found apple developer tutorial called "SimpleNetworkStreams" which employs NSNetService to use x-SNSUpload protocol over tcp by setting type of NSNetService instance to protocol x-SNSUpload._tcp

他们是如何做到的 -

Here is how they have done it -

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

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

So if I just replace _x-SNSUpload._tcp with _smb._tcp and run SMB server on my macbook. I run following set of commands to start SMB on my macbook

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

nc -l 12345 > tmp.png

然后我可以将iPhone中的图片传输到我的macbook的根目录。我希望在Windows机器上的共享文件夹中也这样做。

Then I am able to transfer a picture in my iPhone to my macbook's root directory. I was hoping to do the same with shared folder on windows machine.

共享文件夹的名称是test sharing。我明确地在我的Windows机器上共享了我的测试共享文件夹,并且完全控制了所有人。代码的完整细节放在下面(更新后)

The name of the share folder is "test sharing". I have explicitly shared my 'test sharing' folder in my windows machine with full control to everyone. The complete details of code is put below (after Update)

如果我在浏览器上直接输入smb:\\10.212.19.121我能够访问我的共享文件夹。它打开了finder应用程序,并为我提供了安装temp sharing文件夹的选项。

If I directly type in "smb:\\10.212.19.121" on my browser I am able to access my shared folder. It opens the finder application and gives me an option to mount the "temp sharing" folder.

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

Update - lot of redundant text taken out from above and better details on how SimpleNetworkStreams work and what I have tweaked is put below.

代码取自 - < a href =http://developer.apple.com/library/ios/#samplecode/SimpleNetworkStreams/Introduction/Intro.html =nofollow noreferrer> SimpleNetworkStreams -

The code is taken from - SimpleNetworkStreams -


  1. 为我们要发送的文件打开NSInputStream类型的流

//打开我们要发送的文件的流

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

self.fileStream = [NSInputStream inputStreamWithFileAtPath:filepath]; 

assert(self.fileStream != nil); 

[self.fileStream open];




  1. 苹果文档如何说

  1. As how apple documentation says

您可以直接创建NSNetService对象(如果您知道确切的主机和端口信息),也可以使用NSNetServiceBrowser对象来浏览服务。

"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的对象

An object of NSNetService is instantiated for the server which hosts SMB server

// 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。

Get the output stream object of type NSOutputStream into 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

and then the NSOutputStream delegate catches NSStreamEventHasSpaceAvailable where we buffer in the input file and then write that buffer to our NSOutputStream object i.e. networkStream

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


推荐答案

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

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.

尝试使用 bonjour浏览器查看NSNetService的功能。您将看到的所有条目都是NSNetServices发布的。

或者您可以发布类型为_afpovertcp._tcp的服务。并查看finder中的侧栏以了解如何使用NSNetServices。

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

这些行与SMB完全无关。仅仅因为你在宣传你的服务,因为SMB并不意味着你的服务器实际上了解SMB。

和nc(又名netcat)正如它的名字所暗示的那样。它会将您发送的所有内容转储到您的文件中。不是SMB,根本不是。

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.

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

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

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

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.

这篇关于使用NSNetService类与Windows机器上共享的文件夹建立SMB tcp ip连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆