Use of UDP in iOS

Source: Internet
Author: User

//
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 to show the data sent by the teacher.
Uiimageview *imageview;
Need 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 student-and teacher-side. The teacher is responsible for the full access program students send pictures, students receive data when. Show on ImageView

When the student program starts, it needs to send a data to the teacher. This is the sign-in so that the teacher can get the student's IP address. For data forwarding

What is required is that the screenshot function is only valid for the normal UI. However, you cannot get a video screenshot, assuming you want to take a video screenshot. Need to use OpenGL to get the information of GL before rendering
*/
Self.iparray = [Nsmutablearray arraywithcapacity:0];
[Self createview];
[Self createsocket];

if (STU) {
If it is 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 extent of the captured image, and the second is the level of interception.
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{
Whether it's student-side. Or teacher-side, all need to send and receive, students send a check-in to the teacher, the teacher sends pictures to the student-side
Sendsocket = [[Asyncudpsocket alloc]initwithdelegate:self];
ServerSocket = [[Asyncudpsocket alloc]initwithdelegate:self];


Server-Side binding 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.