-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShareController.m
94 lines (67 loc) · 1.67 KB
/
ShareController.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
//
// ShareController.m
// test2
//
// Created by 冯子强 on 16/3/10.
// Copyright © 2016年 冯子强. All rights reserved.
//
#import "ShareController.h"
//ip地址及端口
#define HOST @"192.168.1.100"
#define PORT 1234
//连接超时
#define TIME_OUT 20
//设置读取超时 -1 表示不会使用超时
#define READ_TIME_OUT -1
//设置写入超时 -1 表示不会使用超时
#define WRITE_TIME_OUT -1
//每次最多读取多少
#define MAX_BUFFER 1024
@implementation ShareController
static ShareController *socketServe = nil;
#pragma mark public static methods
+ (ShareController *)shareSocketServe {
@synchronized(self) {
if (socketServe == nil) {
socketServe = [[[self class] alloc] init];
}
}
return socketServe;
}
+ (id) allocWithZone:(NSZone *)zone
{
@synchronized(self) {
if (socketServe == nil) {
socketServe = [super allocWithZone:zone];
return socketServe;
}
}
return nil;
}
- (void) startConnectSocket
{
if (![self SocketOpen:HOST port:PORT]) {
}
}
- (NSInteger) SocketOpen: (NSString *)addr port: (NSInteger)port
{
if (![self.socket isConnected]) {
NSError *error = nil;
[self.socket connectToHost:addr onPort:port error:&error];
}
return 0;
}
- (void) cutOffSocket
{
self.socket.userData = SocketOfflineByUser;
[self.socket disconnect];
}
- (void)sendMessage:(id)message
{
//像服务器发送数据
NSData *cmdData = [message dataUsingEncoding:NSUTF8StringEncoding];
[self.socket writeData:cmdData withTimeout:0 tag:1];
}
#pragma mark - Delegate
//发送消息成功之后回调
@end