高级UIKit-08(TCPSocket)

【day1001_MoviePlayer】:视频播放

需要导入MediaPlayer.framework

- (void)viewDidLoad



{



    [super viewDidLoad];



    NSURL *url = [NSURL fileURLWithPath:@"/Users/tarena/yz/李明杰/OC视频/ARC/28-ARC04-循环引用.mp4"];



   



    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];



    self.moviePlayer.view.frame = CGRectMake(0, 100, 320, 200);



//    [self.moviePlayer setControlStyle:MPMovieControlStyleNone]; // 隐藏视频操作



    [self.moviePlayer play];



    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playFinishedAction) name:MPMoviePlayerPlaybackDidFinishNotification object:Nil];



    [self.view addSubview:self.moviePlayer.view];



}



-(void)playFinishedAction{



    NSLog(@"播放完成");



}



- (IBAction)clicked:(UIButton *)sender {



//    [self.moviePlayer pause];// 暂停



    // 准备播放 把数据加载到内容



//    [self.moviePlayer prepareToPlay];



    UIImage *image = [self.moviePlayer thumbnailImageAtTime:self.moviePlayer.currentPlaybackTime timeOption:MPMovieTimeOptionExact];



    self.imageView.image = image;



   



}

 

【day1002_netWork】:tcpSocket

要导入一个类AsyncSocket,在ARC模式下需要将该类的.m文件上附加-fno-objc-arc(不使用arc模式)

- (void)viewDidLoad



{



    [super viewDidLoad];



   



    self.fileData = [NSMutableData data];



    // 创建服务端对象



    self.socketServer = [[AsyncSocket alloc] initWithDelegate:self];



    // 设置端口号



    [self.socketServer acceptOnPort:8000 error:Nil];



}



// 接收新的服务端 接收到客户端发送的socket 相当于是客户端和服务端之间的通道



-(void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{



    NSLog(@"接收到客户端发送的连接");



    self.socketNew = newSocket;



}



// 连接到客户端



-(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{



    NSLog(@"连接到客户端:%@",host);



    [self.socketNew readDataWithTimeout:-1 tag:0]; // 读取数据



}



// 接收到客户端数据  注意:当文件大时,此方法只会调用一次读取一次数据,不会自动调用



-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{



    self.fileSize += data.length;



    [self.fileData appendData:data];



    NSLog(@"接收到数据长度:%d,%d",data.length,self.fileSize);



    if (self.fileData.length == 4186188) {



        [self.fileData writeToFile:@"/Users/tarena/yz/第三阶段(高级UI)/day10/a.mp3" atomically:YES];



    }



    [self.socketNew readDataWithTimeout:-1 tag:0];



}



// 断开连接



-(void)onSocketDidDisconnect:(AsyncSocket *)sock{



   



}



 



// 客户端



- (IBAction)buttonAction:(UIButton *)sender {



    // 创建客户端



    self.socketClient = [[AsyncSocket alloc] initWithDelegate:self];



    // 发送连接



    // 连接服务端



    [self.socketClient connectToHost:@"localhost" onPort:8000 error:Nil];



    NSLog(@"发送连接");



    // 发送数据



    NSData *data = [NSData dataWithContentsOfFile:@"/Users/tarena/yz/第三阶段(高级UI)/day10/musics/泡沫.mp3"];



    [self.socketClient writeData:data withTimeout:-1 tag:0];



   



}



-(void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag{



    NSLog(@"发送完成");



}

 

 

属性不能以new开头

 

你可能感兴趣的:(socket)