iOS:发送缓冲区中的套接字连接错误
<p><p>我们尝试了太多不同的方法来解决我们的问题,并且还在网上搜索,但我们只找到了发送字符串而不是字节数组的示例。我们需要通过套接字连接发送字节数组。请阅读下面的问题说明。</p>
<p>我们需要将 wi-fi 设备与 iOS 应用程序连接。我们已经成功连接了设备,但是当我们以字节数组格式发送命令时,它返回 NSStreamEventHasSpaceAvailable 作为响应。这是错误的,我们需要 NSStreamEventHasBytesAvailable 事件。 </p>
<p>下面是连接代码:</p>
<pre><code>-(void) initNetworkCommunication:(NSString*)strHostName {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)strHostName, 2000, &readStream, &writeStream);
inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;
;
;
forMode:NSDefaultRunLoopMode];
forMode:NSDefaultRunLoopMode];
;
;
}
</code></pre>
<p>当连接打开时,我们调用下面的方法来发送命令:</p>
<pre><code> - (IBAction) sendCommand {
unsigned char *buffer = {0x3a,0x04,0x01,0x07,0x0c};
NSData *data = ;
maxLength:5];
}
</code></pre>
<p>所以 sendCommand 方法存在一些问题,因为我们正在接收 NSStreamEventHasSpaceAvailable ,这是错误的。因为它应该在响应中返回 NSStreamEventHasBytesAvailable。谁能帮助我们如何在 iOS 中发送字节数组 {0x3a,0x04,0x01,0x07,0x0c},以便我们可以接收 NSStreamEventHasBytesAvailable 事件。</p>
<p>根据命令手册,当设备接收到正确格式的命令时,它将返回确认。以下是手册说明。</p>
<blockquote>
<p>All commands are 16 bits and are placed in the Data byte 1(MSB) and
Data byte 2(LSB).
The response to a command can either be a specific response relating
to the command or a simple response. The three simple responses are
ACK, NAK and UNK. An ACK signifies that the command was received
successfully. A NAK indicates that there was an error with either the
length byte or an incorrect checksum was calculated. An UNK response
indicates that the recipient does not recognize the command that was
sent.</p>
</blockquote>
<ul>
<li>值回答状态
<ul>
<li>0x06 = 确认(正常)</li>
<li>0x15 = NAK(不正常)</li>
<li>0x09 = UNK(未知命令)</li>
</ul></li>
</ul>
<p>所以我们应该收到上述任何标志(ACK OR NAK OR UNK),但我们收到的是 NSStreamEventHasSpaceAvailable,这是错误的。任何人请帮我解决我的问题。</p>
<p>提前致谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><code>NSStreamEventHasSpaceAvailable</code> 是在输出流上发送数据后接收的正确事件 - 它表明您可以写入至少一个字节而不会阻塞(即有空间可写入数据)。 <code>NSstreamEventHasBytesAvailable</code> 将在与输入流关联的套接字上接收到数据时针对输入流发出信号,以指示您可以在不阻塞的情况下发出读取。 </p>
<p>如果您发送数据以响应该数据的设备,那么我希望您在 <code>inputStream</code></p> 上收到 <code>NSStreamEventHasBytesAvailable</code></p>
<p style="font-size: 20px;">关于iOS:发送缓冲区中的套接字连接错误,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28741114/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28741114/
</a>
</p>
页:
[1]