• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iOS:发送缓冲区中的套接字连接错误

[复制链接]
菜鸟教程小白 发表于 2022-12-12 14:22:29 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我们尝试了太多不同的方法来解决我们的问题,并且还在网上搜索,但我们只找到了发送字符串而不是字节数组的示例。我们需要通过套接字连接发送字节数组。请阅读下面的问题说明。

我们需要将 wi-fi 设备与 iOS 应用程序连接。我们已经成功连接了设备,但是当我们以字节数组格式发送命令时,它返回 NSStreamEventHasSpaceAvailable 作为响应。这是错误的,我们需要 NSStreamEventHasBytesAvailable 事件。

下面是连接代码:

-(void) initNetworkCommunicationNSString*)strHostName {

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)strHostName, 2000, &readStream, &writeStream);

inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;



[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];

}

当连接打开时,我们调用下面的方法来发送命令:

 - (IBAction) sendCommand {
 unsigned char *buffer[5] = {0x3a,0x04,0x01,0x07,0x0c};

NSData *data = [NSData dataWithBytes:buffer length:5];
[outputStream write:[data bytes] maxLength:5];

}

所以 sendCommand 方法存在一些问题,因为我们正在接收 NSStreamEventHasSpaceAvailable ,这是错误的。因为它应该在响应中返回 NSStreamEventHasBytesAvailable。谁能帮助我们如何在 iOS 中发送字节数组 {0x3a,0x04,0x01,0x07,0x0c},以便我们可以接收 NSStreamEventHasBytesAvailable 事件。

根据命令手册,当设备接收到正确格式的命令时,它将返回确认。以下是手册说明。

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.

  • 值回答状态
    • 0x06 = 确认(正常)
    • 0x15 = NAK(不正常)
    • 0x09 = UNK(未知命令)

所以我们应该收到上述任何标志(ACK OR NAK OR UNK),但我们收到的是 NSStreamEventHasSpaceAvailable,这是错误的。任何人请帮我解决我的问题。

提前致谢。



Best Answer-推荐答案


NSStreamEventHasSpaceAvailable 是在输出流上发送数据后接收的正确事件 - 它表明您可以写入至少一个字节而不会阻塞(即有空间可写入数据)。 NSstreamEventHasBytesAvailable 将在与输入流关联的套接字上接收到数据时针对输入流发出信号,以指示您可以在不阻塞的情况下发出读取。

如果您发送数据以响应该数据的设备,那么我希望您在 inputStream

上收到 NSStreamEventHasBytesAvailable

关于iOS:发送缓冲区中的套接字连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28741114/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap