objective-c – 如何使用NSOutputStream的写消息?

objective-c – 如何使用NSOutputStream的写消息?,第1张

概述我想通过套接字将UI Image发送到服务器. a)我打开输出流: - (IBAction)send:(id)sender { NSURL *website = [NSURL URLWithString:str_IP]; NSHost *host = [NSHost hostWithName:[website host]]; [NSStream getStreamsToHo 我想通过套接字将UI Image发送到服务器.

a)我打开输出流:

- (IBAction)send:(ID)sender {    NSURL *website = [NSURL URLWithString:str_IP];    NSHost *host = [NSHost hostWithname:[website host]];    [Nsstream getStreamsToHost:host port:1100 inputStream:nil outputStream:&oStream];    [oStream retain];    [oStream setDelegate:self];    [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];    [oStream open];}

b)在打开完成后我将NSData写入outputstream并在发生错误时处理错误.

- (voID) stream: (Nsstream *) stream handleEvent: (NsstreamEvent) eventCode{    //printf("EVENT: Start.\n");    switch(eventCode)    {            case NsstreamEventopenCompleted:            {                //printf("EVENT: Open completed.\n");                if(stream == oStream)                    {                        //printf("Sending...\n");                        NSData *data = UIImageJPEGRepresentation(drawImage.image,90);                        NSInteger x = [oStream write:[data bytes] maxLength:[data length]];                    }                break;            }            case NsstreamEventEndEncountered:            {                //printf("EVENT: End encountered.\n");                break;            }            case NsstreamEventHasspaceAvailable:            {                //printf("EVENT: Has space available.\n");                break;            }            case NsstreamEventHasBytesAvailable:            {                //printf("EVENT: Has bytes available.\n");                break;            }            case NsstreamEventErrorOccurred:            {                UIAlertVIEw *alert = [[UIAlertVIEw alloc] initWithTitle:@"Error Occurred"                                                                       message:nil                                                                delegate:nil                                                                   cancelbuttonTitle:@"OK"                                                                     otherbuttonTitles:nil];                                   [alert show];                [alert release];                break;            }            case NsstreamEventNone:            {                //printf("EVENT: None.\n");                break;            }    }    //printf("EVENT: End.\n");}

当我运行此代码时,将调用NsstreamEventopenCompleted和NsstreamEventErrorOccurred.成功调用NSOutputStream的write方法,并且所有数据都不是nil.但是在将数据写入oStream之后,eventCode将更改为NsstreamEventErrorOccurred.

所以我想也许这不是使用[oStream write]的正确方法.那么使用此消息的正确方法是什么?我发现这条消息返回一个-1073748088的NSInteger,可能是什么问题?

解决方法 只有在输出流中有空间可用后才应该写入数据.当流完成打开时,它并不总是立即有可用空间,因此写入它将不起作用.如果将write调用移动到NsstreamEventHasspaceAvailable处理程序,它应该会成功.

此外,套接字另一端的计算机无法知道您发送的数据的长度.除非您通过关闭套接字发出数据结束信号,否则应该明确地将数据长度与数据一起发送:

case NsstreamEventHasspaceAvailable:{    if(stream == oStream)    {        NSData *data = UIImageJPEGRepresentation(drawImage.image,90);        // Convert from host to network endianness        uint32_t length = (uint32_t)htonl([data length]);        // Don't forget to check the return value of 'write'        [oStream write:(uint8_t *)&length maxLength:4];        [oStream write:[data bytes] maxLength:length];    }    break;}
总结

以上是内存溢出为你收集整理的objective-c – 如何使用NSOutputStream的写消息?全部内容,希望文章能够帮你解决objective-c – 如何使用NSOutputStream的写消息?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1246534.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存