如何使用 NSStream 类创建 tcp 连接 iPhone
我需要将一个字符串发送到与 iPad 位于同一网络上的服务器。阅读此 我发现也许我需要使用 + getStreamsToHost:port:inputStream:outputStream:
方法,但我无法使用它。在 xcode 中,我尝试过:
//Try 1
NSStream *myStream;
//[myStream getStreamToHost// xcode cant find it]
//Try 2
NSStream *myStream2 = [[NSStream alloc] init];
[myStream2 getStreamToHost// I have initialized it and yet xcode can't find the method
//Try 3
[NSStream getStreamToHost] // does not work eather
//Try 4
[[[NSStream alloc] init] getStreamToHost// does not work
顺便说一句,我正在使用一个名为 autoit 的简单程序监听端口 403,让我向您展示如何操作:
我有两个脚本(客户端和服务器);
首先我运行服务器,它开始监听。然后我运行客户端脚本,当发生这种情况时,会出现一个消息框并开始播放歌曲:
让我向您展示代码,它很容易理解,只是为了展示正在发生的事情
......客户端代码:
和服务器代码:
请注意,客户端发送消息 hello,当发生这种情况时,会出现一个消息框 MsgBox(0,"Data Received", $TCPReceive)
并且开始播放歌曲 shellExecute ("t.mp3") 该 mp3 文件位于脚本的同一目录中。
如何使用 IPHONE 向我的计算机发送类似的消息?
I need to send a string to a server on the same network with the iPad. when reading This I found out that maybe I need to use the + getStreamsToHost:port:inputStream:outputStream:
method but I am not able use it. In xcode I have tried:
//Try 1
NSStream *myStream;
//[myStream getStreamToHost// xcode cant find it]
//Try 2
NSStream *myStream2 = [[NSStream alloc] init];
[myStream2 getStreamToHost// I have initialized it and yet xcode can't find the method
//Try 3
[NSStream getStreamToHost] // does not work eather
//Try 4
[[[NSStream alloc] init] getStreamToHost// does not work
and by the way I am listening on port 403 with a simple program called autoit let me show you how:
I have two scripts (the client and the server);
first I run the server and it starts listening. Then I run the client script and when that happens a message box appears and a song starts playing:
Let me show you the code it is very simple to understand just to show what's going on...
the client code:
and the server code:
note that the client sends the message hello and when that happens a message box appears MsgBox(0,"Data Received", $TCPReceive)
and a song starts playing shellExecute("t.mp3") that mp3 file is on the same directory of the script.
HOW CAN I SEND A SIMILAR MESSAGE BUT WITH AN IPHONE TO MY COMPUTER?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
+getStreamsToHost:port:inputStream:outputStream:
是一个类方法(由+
表示),因此您必须使用[NSStream getStreams...]< /code> 而不是
[[[NSStream alloc] init] getStreams...]
。+getStreamsToHost:port:inputStream:outputStream:
is a class method (indicated by the+
), so you have to use[NSStream getStreams...]
instead of[[[NSStream alloc] init] getStreams...]
.