接收大文件(ios)

发送端
socket=[[AsyncSocket alloc] initWithDelegate:self];
[socket connectToHost:@"192.168.1.121" onPort:9999 error:nil];
NSString * filePath=[[NSBundle mainBundle] pathForResource:@"123.zip" ofType:nil];
 
 
NSData * fileData=[NSData dataWithContentsOfFile:filePath];
int fileLength=[fileData length];
NSData * lengthData=[NSData dataWithBytes:&fileLength length:sizeof(int)];
NSMutableData * sendData=[[NSMutableData alloc] initWithLength:0];
[sendData appendData:lengthData];
[sendData appendData:fileData];
[socket writeData:sendData withTimeout:-1 tag:1];
[sendData release];


接收端
 
//
//  ServerViewController.m
//  Server
//
//  Created by tuyuer tuyuer on 2/4/12.
//  Copyright tuyuer 2012. All rights reserved.
//


#import "ServerViewController.h"


@implementation ServerViewController


static long readTimes;
- (void)viewDidLoad{
[super viewDidLoad];
fileLength=0;
serverSocket=[[AsyncSocket alloc] initWithDelegate:self];
NSError * err=nil;
if ([serverSocket acceptOnPort:9999 error:&err]) {
NSLog(@"accept ok");
}else {
UIAlertView * alView=[[UIAlertView alloc] initWithTitle:@"绑定端口失败"
message:@"绑定端口失败" delegate:nil
  cancelButtonTitle:@"ok" 
  otherButtonTitles:nil];
[alView show];
[alView release];
}
if (err) { 
NSLog(@"error: %@",err); 
}
 
imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
imageView.backgroundColor=[UIColor yellowColor];
[self.view addSubview:imageView];
}


- (void)dealloc {
[serverSocket release];
    [super dealloc];
}
#pragma mark -
#pragma mark AsyncSocket delegate
 
- (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{
if (!acceptSocket) {
acceptSocket=[newSocket retain];
receiveData=[[NSMutableData alloc] initWithLength:0];
NSLog(@"did accept new socket"); 
}
}
 
- (NSRunLoop *)onSocket:(AsyncSocket *)sock wantsRunLoopForNewSocket:(AsyncSocket *)newSocket{
NSLog(@"wants runloop for new socket."); 
    return [NSRunLoop currentRunLoop]; 
}
- (BOOL)onSocketWillConnect:(AsyncSocket *)sock{
NSLog(@"will connect"); 
    return YES; 
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{ 
    NSLog(@"did connect to host"); 
[acceptSocket readDataToLength:sizeof(int) withTimeout:-1 tag:1];
}


 
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{ 
    NSLog(@"did read data"); 
if(fileLength==0){
[data getBytes:&fileLength length:sizeof(long)];
[acceptSocket readDataToLength:1000 withTimeout:-1 tag:1];
}else {
if ([receiveData length] < fileLength-sizeof(int)) {
[receiveData appendData:data];
int leftLength=fileLength-4-[receiveData length];
if(leftLength<1000){
[acceptSocket readDataToLength:leftLength withTimeout:-1 tag:2];
}else {
[acceptSocket readDataToLength:1000 withTimeout:-1 tag:2];
}
NSLog(@"receiveData Length===%d",[receiveData length]);
if([receiveData length]==fileLength-4){
NSString * destPath= NSHomeDirectory();
NSString * strDir=[destPath stringByAppendingPathComponent:@"Documents"];
NSString * de=[strDir stringByAppendingPathComponent:@"111.zip"];
[receiveData writeToFile:de atomically:YES];
}
}else {
[receiveData appendData:data];
NSLog(@"receive Finished && dataLength=%d",[receiveData length]);
}


}
}
- (void)onSocket:(AsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag;
{
[acceptSocket readDataToLength:partialLength withTimeout:-1 tag:3];
}


//- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag{ 
//    NSLog(@"message did write"); 
//    [acceptSocket readDataWithTimeout:-1 tag:1]; 
//}


- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err{
    NSLog(@"onSocket:%p willDisconnectWithError:%@", sock, err); 
}


- (void)onSocketDidDisconnect:(AsyncSocket *)sock{ 
    NSLog(@"socket did disconnect"); 
    [acceptSocket release]; 
    acceptSocket=nil; 
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值