#import "TRViewController.h"
@interface TRViewController ()
@property (weak, nonatomic) IBOutlet UITextField *hostTF;
@property (weak, nonatomic) IBOutlet UITextField *sendInfoTF;
@property (weak, nonatomic) IBOutlet UITextView *myTV;
@property (nonatomic, strong)AsyncSocket *clientSocket;
@property (nonatomic, strong)AsyncSocket *serverSocket;
@property (nonatomic, strong)AsyncSocket *acceptNewSocket;
@property (nonatomic, copy)NSString *host;
@property (nonatomic, strong)NSMutableData *allFileData;
@property (nonatomic, copy)NSString *fileName;
@property (nonatomic)int fileLength;
@end
@implementation TRViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.allFileData = [NSMutableData data];
//1.創建Socket對象
self.serverSocket = [[AsyncSocket alloc]initWithDelegate:self];
//2.監聽某一個端口等待別人連接進來
[self.serverSocket acceptOnPort:8000 error:Nil];
}
//當有新的連接進來的時候
-(void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{
self.acceptNewSocket = newSocket;
}
//當連接到某個ip的時候
-(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
NSLog(@"已經連接到:%@",host);
self.host = host;
[self.acceptNewSocket readDataWithTimeout:-1 tag:0];
}
//當接收到數據的時候 此方法不會自動調用 需要手動調用讀取的方法
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSData *headerData = [data subdataWithRange:NSMakeRange(0, 100)];
NSString *headerString = [[NSString alloc]initWithData:headerData encoding:NSUTF8StringEncoding];
//判斷是否包含消息頭
if (headerString && [headerString componentsSeparatedByString:@"&&"].count == 3) {
NSArray *headers = [headerString componentsSeparatedByString:@"&&"];
NSString *type = headers[0];
if ([type isEqualToString:@"file"]) {
self.fileName = headers[1];
self.fileLength = [headers[2] intValue];
self.allFileData = [NSMutableData data];
NSData *subFileData = [data subdataWithRange:NSMakeRange(100, data.length-100)];
[self.allFileData appendData:subFileData];
}else{//如果接收到的是文本
NSData *textData = [data subdataWithRange:NSMakeRange(100, data.length-100)];
NSString *text = [[NSString alloc]initWithData:textData encoding:NSUTF8StringEncoding];
NSLog(@"接收到文本:%@",text);
}
}else{//如果不是第一次沒有包含消息頭 就直接把data放進allFileData裏面
[self.allFileData appendData:data];
}
//判斷有沒有下載完成
if (self.allFileData.length == self.fileLength) {
NSString *newFilePath = [@"/Users/tarena/Desktop/" stringByAppendingPathComponent:self.fileName];
[self.allFileData writeToFile:newFilePath atomically:YES];
}
//繼續讀取數據
[self.acceptNewSocket readDataWithTimeout:-1 tag:0];
}
//當斷開連接的時候
-(void)onSocketDidDisconnect:(AsyncSocket *)sock
{
}
- (IBAction)clicked:(id)sender {
//創建客戶端代碼
self.clientSocket = [[AsyncSocket alloc]initWithDelegate:self];
[self.clientSocket connectToHost:self.hostTF.text onPort:8000 error:nil];
NSString *filePath = @"/Volumes/U/Networking_and_Multitasking/day01/光輝歲月.mp3";
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *headerString = [NSString stringWithFormat:@"file&&%@&&%lu",[filePath lastPathComponent],(unsigned long)fileData.length];
NSData *headerData = [headerString dataUsingEncoding:NSUTF8StringEncoding];
//創建一個100個字節的可變data把headerData裝進去
NSMutableData *allData = [NSMutableData dataWithLength:100];
[allData replaceBytesInRange:NSMakeRange(0, headerData.length) withBytes:headerData.bytes];
[allData appendData:fileData];
NSLog(@"%d",fileData.length);
[self.clientSocket writeData:allData withTimeout:-1 tag:0];
}
-(void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag{
NSLog(@"發送完成!!!");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end