如何使用 NSStream 类创建 tcp 连接 iPhone

发布于 2024-11-24 14:51:02 字数 1355 浏览 1 评论 0原文

我需要将一个字符串发送到与 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);
enter image description here

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:
enter image description here

Let me show you the code it is very simple to understand just to show what's going on...

the client code:

enter image description here

and the server code:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法少女 2024-12-01 14:51:02

+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...].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文