1

enter code here我通过 TCP 套接字从两个设备 (ios) 写入应用程序传输数据。我使用 NSString 成功,我尝试使用图像文件。

CODE SEND DATA :
NSString* docsDir;
NSArray*  dirPaths;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
filePath = [[NSBundle mainBundle] pathForResource:@"image1.png" ofType:@""];
NSFileManager * filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath:filePath] == YES)
{
    NSLog(@"OKie");
}

UInt64 offset = 0;
UInt32 chunkSize = 1024;

NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init];
NSFileManager *fm = [NSFileManager defaultManager];
NSLog(@"%llu", [[fm attributesOfItemAtPath:[filePath stringByStandardizingPath] error:NULL] fileSize]);
chunkSize = [[fm attributesOfItemAtPath:[filePath stringByStandardizingPath] error:NULL] fileSize];
NSData *data = [handle readDataOfLength:chunkSize];

NSLog(@"Start send");

while ([data length] > 0 ) {        
    NSLog(@"sending");        

    autoreleasePool = [[NSAutoreleasePool alloc] init];   
    const uint8_t *bytes = (const uint8_t*)[data bytes];
    if([_outStream write:bytes maxLength:sizeof(const uint8_t)] == -1)
    {
        break;
    }
    offset += [data length];
    [handle seekToFileOffset:offset];
    data = [handle readDataOfLength:chunkSize];
    NSLog(@"%d",[data length]);
}

NSLog(@"Send Finish.");

[handle closeFile];
[autoreleasePool release];

CODE RECEIVE DATA 



 - (void) stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
    {
        switch(eventCode) {
            case NSStreamEventOpenCompleted:
            {
                [self destroyPicker];

                [_server release];
                _server = nil;

                if (stream == _inStream)
                    _inReady = YES;
                else
                    _outReady = YES;

                if (_inReady && _outReady) {
                    NSLog(@"Connected");
                }
                break;
            }
            case NSStreamEventHasBytesAvailable:
            {
                NSString* docsDir;
                NSArray*  dirPaths;
                dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                 docsDir = [dirPaths objectAtIndex:0];
                filePath = [docsDir stringByAppendingPathComponent:@"image12.png"];
                if (stream == _inStream) {
                    NSLog(@"packet");
    //              uint8_t b;
                    uint8_t *Buffer;
                    NSUInteger length;
                    //uint8_t buf[1024];
    //                unsigned int len = 0;             
                    [(NSInputStream *)stream getBuffer:&Buffer length:&length];
    //                len = [_inStream read:&b maxLength:sizeof(uint8_t)];
                    NSData * data = [NSData dataWithBytes:&Buffer  length:6957];

                    NSLog(@"Entering Loop.");

                        if ([data length] > 0) 
                        {

                            NSFileHandle *file;
                            file = [NSFileHandle fileHandleForWritingAtPath: filePath];
                                //file = [NSFileHandle fileHandleForUpdatingAtPath: filePath];

                            if (file == nil)
                            {
                                     NSLog(@"Failed to open file");
                                    [data writeToFile: filePath atomically: YES];
                                NSFileManager *fm = [NSFileManager defaultManager];
                                NSLog(@"%llu", [[fm attributesOfItemAtPath:[filePath stringByStandardizingPath] error:NULL] fileSize]);
                            }
                            else
                            {
                                [file seekToEndOfFile];                            
                                [file writeData: data];                            
                                [file closeFile];
                                NSFileManager *fm = [NSFileManager defaultManager];
                                NSLog(@"%llu", [[fm attributesOfItemAtPath:[filePath stringByStandardizingPath] error:NULL] fileSize]);
                            }    
                        }
    //                }
                    NSLog(@"Exited Loop.");              

                }
                break;
            }
            case NSStreamEventErrorOccurred:
            {
                [self _showAlert:@"Error encountered on stream!"];          
                break;
            }

            case NSStreamEventEndEncountered:
            {

                UIAlertView *alertView;


                //Notify all tap views
    //          for(view in array)
    //              [view touchUp:YES];

                alertView = [[UIAlertView alloc] initWithTitle:@"Peer Disconnected!" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Continue", nil];
                [alertView show];
                [alertView release];

                break;
            }
        }
    }

*注意 fileSize = 6957(文件 image1.png)。我检查文件 image12.png 已写入并且大小 = 6957;(检查服务器已发送并且数据已发送到客户端 6957 字节)。问题我无法将其作为图像文件读取。(不是图片)。-link down 项目发送-接收 NSString(聊天):http ://www.mediafire.com/? f80if0y2x3lcoea -link down 项目发送-接收图像(问题):http ://www.mediafire.com/?d2l9fl63i176m2z (我从 dev.apple 的 witap 编辑)希望你能帮助我

4

0 回答 0