1

enter code here2つのデバイス(ios)からのアプリ転送データをTCPソケットで書き込みます。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バイト送信されたことを確認します)。問題画像ファイルとして読み取れません。(画像ではありません)。-リンクダウンプロジェクト送信-NSString(チャット)を受信:http ://www.mediafire.com/? f80if0y2x3lcoea-リンクダウンプロジェクト送信-画像を受信(問題):http ://www.mediafire.com/?d2l9fl63i176m2z (私はdev.appleのwitapから編集します)私はあなたが私を助けることができることを願っています

4

0 に答える 0