发送端
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
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