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;
}
}
}
* note fileSize = 6957(file image1.png)。 我检查文件image12.png已写入并且大小= 6957;(检查服务器已发送,数据转到客户端6957字节)。 问题我无法将其作为图像文件读取。 (不是图像)。 -link down project sent-receive NSString(chat):http://www.mediafire.com/?f80if0y2x3lcoea -link down project sent-receive a image(问题):http://www.mediafire.com/?d2l9fl63i176m2z (我从dev.apple的witap编辑) 我希望你能帮助我