//
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>
{
Creating sockets
Asyncudpsocket *sendsocket;
Asyncudpsocket *serversocket;
Teachers need a label, every 1 seconds, change the number
UILabel *numberlabel;
Student-side, need a imageview, show the data sent by the teacher.
Uiimageview *imageview;
Requires a macro definition to differentiate between student and teacher, one-click conversion
}
@property (Nonatomic,strong) Nsmutablearray *iparray;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
/**
* The program is divided into students and teachers, the teacher is responsible for all Access programs students send pictures, students receive data when the ImageView on the display
When the student program starts, it is necessary to send a data to the teacher, that is, check-in so that the teacher can get the student's IP address and transmit the data.
Need: The screenshot function is only valid for normal UI, but cannot get to the video screenshot, if you want to take a video screenshot, you need to use OpenGL to obtain the information of GL, and then render
*/
Self.iparray = [Nsmutablearray arraywithcapacity:0];
[Self createview];
[Self createsocket];
if (STU) {
If a student, the program starts sending a data to the teacher
[Sendsocket senddata:[@] Sign in! "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
}
Set the label's background color
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 = @ "Do you smoke?" Come with me. "
Numberlabel.font = [Uifont boldsystemfontofsize:40];
Capture Screen
The first parameter is the range of the captured image, and the second is the layer
UIImage *image = [Zcscreenshot beginImageContext:self.view.frame View:self.view];
Traverse forward
For (NSString *ip in Self.iparray) {
[Sendsocket senddata:uiimagejpegrepresentation (Image, 0.1) Tohost:ip port:5678 withtimeout:-1 tag:100];
}
}
-(void) createsocket{
Both students and teachers need to send and receive, the student sends a sign-in to the teacher, the teacher sends the picture to the student's side
Sendsocket = [[Asyncudpsocket alloc]initwithdelegate:self];
ServerSocket = [[Asyncudpsocket alloc]initwithdelegate:self];
Server-side bound port
[ServerSocket bindtoport:5678 Error:nil];
Continuous observation
[ServerSocket receivewithtimeout:-1 tag:100];
}
-(void) createview{
if (STU) {
Student-side
ImageView = [[Uiimageview alloc]initwithframe:self.view.frame];
[Self.view Addsubview:imageview];
}else{
Teacher-side
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{
Send complete
}
-(BOOL) Onudpsocket: (Asyncudpsocket *) sock didreceivedata: (NSData *) data withtag: (long) tag fromhost: (NSString *) host Port: (UInt16) port{
Accept Data
Teacher-Side accepts students ' IP
Student-accepted pictures of teachers
if (STU) {
if (ImageView) {
Imageview.image = [UIImage imagewithdata:data];
}
Imageview.image = [UIImage imagewithdata:data];
}else{
Record student IP, need an array, append new data, ignore old data
if (![ Self.iparray Containsobject:host]) {
[Self.iparray Addobject:host];
}
}
//
[sock receivewithtimeout:-1 tag:100];
return YES;
}
@end
Use of UDP in iOS