-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShareViewController.m
196 lines (164 loc) · 6.65 KB
/
ShareViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// ShareViewController.m
// InCloud
//
// Created by Denis on 15/8/1.
// Copyright (c) 2015年 Denis. All rights reserved.
//
#import "ShareViewController.h"
#import "MyIPHelper.h"
#import "MBProgressHUD.h"
#import "service/ConfigService.h"
@interface ShareViewController()<MBProgressHUDDelegate> {
NSInteger isSrv;
MBProgressHUD *HUD;
NSMutableString *jsonString;
ConfigService *cs;
}
@end
@implementation ShareViewController
@synthesize curip,sharetitle,sendip,shareclient,sharesrv;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
cs=[[ConfigService alloc] init];
sharetitle.text=[[NSString alloc] initWithFormat:@"接收方按‘建立连接’,发送方按‘共享’"];
curip.text=[[NSString alloc] initWithFormat:@"当前WiFi-IP:%@",[MyIPHelper deviceIPAdress]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)createSrv{
jsonString=[[NSMutableString alloc] init];
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.dimBackground = YES;
HUD.delegate = self;
HUD.labelText = @"接收数据中....";
[HUD show:YES];
//创建一个后台队列 等待接收数据
dispatch_queue_t dQueue = dispatch_queue_create("rev socket queue", NULL); //第一个参数是该队列的名字
//1.实例化一个udp socket套接字对象
// udpServerSocket需要用来接收数据
udpServerSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dQueue socketQueue:nil];
[udpServerSocket setIPv4Enabled:YES];
[udpServerSocket setIPv6Enabled:NO];
//2.服务器端来监听端口12345(等待端口12345的数据)
[udpServerSocket bindToPort:18888 error:nil];
isSrv=1;
//3.接收一次消息(启动一个等待接收,且只接收一次)
[udpServerSocket receiveOnce:nil];
}
-(void)createClientUdpSocket{
if ((sendip.text.length>0)&&[self validateIp:sendip.text]) {
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.dimBackground = YES;
HUD.delegate = self;
HUD.labelText = @"共享数据中....";
[HUD show:YES];
dispatch_queue_t dQueue = dispatch_queue_create("client udp socket", NULL);
sendUdpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dQueue socketQueue:nil];
[sendUdpSocket setIPv4Enabled:YES];
[sendUdpSocket setIPv6Enabled:NO];
[sendUdpSocket bindToPort:19999 error:nil];
NSString *sendjson=[cs AtusToJsonList];
NSStringEncoding strEncode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSData *cmddata = [sendjson dataUsingEncoding:strEncode];
[sendUdpSocket sendData:cmddata toHost:sendip.text port:18888 withTimeout:60 tag:200];
isSrv=2;
[sendUdpSocket receiveOnce:nil];
}else{
UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"提示" message:@"文本框IP不能为空或IP格式不正确" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
return;
}
}
- (BOOL)validateIp:(NSString *)ipstr {
NSString *ipRegex = @"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$";
NSPredicate *ipTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", ipRegex];
return [ipTest evaluateWithObject:ipstr];
}
- (IBAction)sendPressed : (id)sender{
[self createClientUdpSocket];
}
- (IBAction)revPressed : (id)sender{
[self createSrv];
}
#pragma mark -GCDAsyncUdpSocketDelegate
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContex
{
//取得发送发的ip和端口
NSString *ip = [GCDAsyncUdpSocket hostFromAddress:address];
uint16_t port = [GCDAsyncUdpSocket portFromAddress:address];
//data就是接收的数据
NSStringEncoding strEncode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *s = [[NSString alloc] initWithData: data encoding:strEncode];
NSLog(@"[%@:%u]%@",ip, port,s);
if (isSrv==1) {
if ([s isEqualToString:@"end"]) {
dispatch_async(dispatch_get_main_queue(), ^{
[udpServerSocket close];
[HUD hide:YES];
});
NSLog(@"json str:%@",[jsonString copy]);
isSrv=-1;
NSMutableArray *atus=[cs JsonListToAtus:jsonString];
[cs SaveAtusData:atus];
}else{
dispatch_async(dispatch_get_main_queue(), ^{
[jsonString appendString:s];
});
NSData *cmddata = [@"ok" dataUsingEncoding:NSUTF8StringEncoding];
[udpServerSocket sendData:cmddata toHost:ip port:port withTimeout:60 tag:200];
[udpServerSocket receiveOnce:nil];
}
}else if(isSrv==2){
if ([s isEqualToString:@"ok"]) {
NSData *cmddata = [@"end" dataUsingEncoding:NSUTF8StringEncoding];
[sendUdpSocket sendData:cmddata toHost:ip port:port withTimeout:60 tag:200];
[sendUdpSocket receiveOnce:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[sendUdpSocket close];
[HUD hide:YES];
});
isSrv=-1;
}
}
}
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error{
NSLog(@"tag:%ld,%@",tag, error);
}
-(void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error {
NSLog(@"Socket closed, error: %@", error);
}
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag{
NSLog(@"Socket send, tag: %ld", tag);
}
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didNotConnect:(NSError *)error {
NSLog(@"Failed to connect to host, error: %@", error);
}
- (BOOL)slideNavigationControllerShouldDisplayLeftMenu
{
return YES;
}
- (BOOL)slideNavigationControllerShouldDisplayRightMenu
{
return NO;
}
#pragma mark - MBProgressHUDDelegate
- (void)hudWasHidden:(MBProgressHUD *)hud {
// Remove HUD from screen when the HUD was hidded
[HUD removeFromSuperview];
HUD = nil;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end