首页 > 代码库 > iOS中UDP的使用
iOS中UDP的使用
// ViewController.m
// UDPDemo
//
// Created by qianfeng01 on 15-8-13.
// Copyright (c) 2015年 zg. All rights reserved.
//
#import "ViewController.h"
#import "AsyncUdpSocket.h"
#import "ZCScreenShot.h"
#define STU 1
@interface ViewController ()<AsyncUdpSocketDelegate>
{
//建立socket
AsyncUdpSocket *sendSocket;
AsyncUdpSocket *serverSocket;
//教师端须要一个label,每隔1秒,改变一下数字
UILabel *numberLabel;
//学生端。须要一个imageView,显示教师端发送过来的数据
UIImageView *imageView;
//须要一个宏定义来区分学生端和教师端,一键转换
}
@property (nonatomic,strong) NSMutableArray *ipArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/**
* 程序分为学生端和教师端。教师端负责对全部接入程序的学生发送截图图片,学生端接受数据的时候。在imageview上显示
学生端程序启动的时候,须要向教师端发送一个数据。也就是签到,这样教师端能够获得学生端的IP地址。进行数据转发
须要的是:截屏函数仅仅对普通的ui有效。可是无法获取到视频截屏,假设要进行视频截屏。须要使用opengl来获取GL的信息,再进行渲染
*/
self.ipArray = [NSMutableArray arrayWithCapacity:0];
[self createView];
[self createSocket];
if (STU) {
//假设是学生,程序启动向教师端发送一个数据
[sendSocket sendData:[@"签到!
" dataUsingEncoding:NSUTF8StringEncoding] toHost:@"10.8.155.36" port:5678 withTimeout:-1 tag:100];
}else{
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerClick) userInfo:nil repeats:YES];
}
}
- (void)timerClick{
if (self.ipArray.count == 0) {
return;
}
//设置label的背景颜色
float r = arc4random()%256/255.0;
float g = arc4random()%256/255.0;
float b = arc4random()%256/255.0;
numberLabel.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1];
numberLabel.text = @"抽烟不?跟我一块儿吧";
numberLabel.font = [UIFont boldSystemFontOfSize:40];
//截取屏幕
//第一个參数是截取图片的范围,第二个參数是截取的那一层
UIImage *image = [ZCScreenShot beginImageContext:self.view.frame View:self.view];
//遍历转发
for (NSString *ip in self.ipArray) {
[sendSocket sendData:UIImageJPEGRepresentation(image, 0.1) toHost:ip port:5678 withTimeout:-1 tag:100];
}
}
- (void)createSocket{
//不论是学生端。还是教师端,都须要发送和接受,学生端发送一个签到给教师端,教师端发送图片给学生端
sendSocket = [[AsyncUdpSocket alloc]initWithDelegate:self];
serverSocket = [[AsyncUdpSocket alloc]initWithDelegate:self];
//服务端绑定port
[serverSocket bindToPort:5678 error:nil];
//持续观察
[serverSocket receiveWithTimeout:-1 tag:100];
}
- (void)createView{
if (STU) {
//学生端
imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
[self.view addSubview:imageView];
}else{
//教师端
numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];
numberLabel.center = self.view.center;
numberLabel.textAlignment =NSTextAlignmentCenter;
[self.view addSubview:numberLabel];
}
}
#pragma mark -- AsyncUdpSocketDelegate
-(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag{
//发送完毕
}
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{
//接受数据
//教师端接受学生的ip
//学生端接受教师的图片
if (STU) {
if (imageView) {
imageView.image = [UIImage imageWithData:data];
}
imageView.image = [UIImage imageWithData:data];
}else{
//记录学生IP,须要一个数组,对新的数据进行追加,对旧的数据忽略
if (![self.ipArray containsObject:host]) {
[self.ipArray addObject:host];
}
}
//
[sock receiveWithTimeout:-1 tag:100];
return YES;
}
@end
iOS中UDP的使用