简体   繁体   English

服务器未收到iOS发送的数据

[英]Server don't receive data send by iOS

I'm send a string to my server (PHP) with the code below: 我使用以下代码将字符串发送到服务器(PHP):

    NSString* str = @"teststring";
    NSData* dataToSend = [str dataUsingEncoding:NSUTF8StringEncoding];

    uint8_t *dataBytes = (uint8_t *)[dataToSend bytes];
    uint64_t length = dataToSend.length;

    [outputStream write:dataBytes maxLength:length];

The problem with this is code, is that I don't receive the string (Server recognize that a user enter and I receive a handshake in Xcode console) I test others codes and works very well (receive data), but in this time I'm trying by my own. 这是代码的问题,是我没有收到字符串(服务器识别出用户输入并且我在Xcode控制台中收到了握手),我测试了其他代码并且运行良好(接收数据),但是这次我我自己尝试。 I believe I'm not converting the message to the appropriate format, and that's why the message is not being received, am I right? 我相信我没有将邮件转换为适当的格式,这就是为什么未收到邮件的原因,对吗? If so how I could convert this message to the appropriate format? 如果是这样,我如何将该消息转换为适当的格式?

Are you sure the output stream has space for writing that many bytes at the moment when you call the write:... method? 您确定在调用write:...方法时输出流有空间可以写入那么多字节吗? You should only call write:... on an output stream from within your stream:handleEvent: delegate method, and only if the event code is NSStreamEventHasSpaceAvailable . 仅当事件代码为NSStreamEventHasSpaceAvailable ,才应在stream:handleEvent:委托方法内的输出流上调用write:...

And you must check the result to see how many bytes were actually written. 并且您必须检查结果以查看实际写入了多少字节。 If it is fewer bytes than you sent, you must enqueue the remaining data to resend later, whenever your stream:handleEvent: delegate method gets called again. 如果字节数少于发送的字节数,则每当再次调用stream:handleEvent:委托方法时,都必须将其余数据放入队列以便稍后重新发送。

For more details, see: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Streams/Articles/WritingOutputStreams.html 有关更多详细信息,请参见: https : //developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Streams/Articles/WritingOutputStreams.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM