第三方框架直接podfile导入GCDAsyncSocket。
#import <GCDAsyncSocket.h>
遵循<GCDAsyncSocketDelegate>这个第三方框架代理协议
// 定义客户端socket
@property (strong, nonatomic) GCDAsyncSocket *clientSocket;
// 1.初始化GCDAsyncSocket
self.clientSocket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
//终端打开IP和端口 nc -lk xxxx xxxx为端口号
//第一参数,IP地址
//第二参数,端口号
// 2.链接服务器
// withTimeout -1 : 无穷大,一直等
[self.clientSocket connectToHost:self.addressTF.text onPort:self.portTF.text.integerValue viaInterface:nil withTimeout:-1 error:nil];
//3.发送消息给服务器
NSMutableArray *arrayMsg = [[NSMutableArray alloc]init];
// ==msg head id==
[arrayMsg addObject:@(0x21)];
[arrayMsg addObject:@(0x00)];
[arrayMsg addObject:@(0x00)];
[arrayMsg addObject:@(0x00)];
// ==msg head ack==
[arrayMsg addObject:@(0x00)];
// ==msg head dir==
[arrayMsg addObject:@(0x01)];
[arrayMsg addObject:@(0x00)];
[arrayMsg addObject:@(0x00)];
[arrayMsg addObject:@(0x00)];
// ==cmd_Id==
[arrayMsg addObject:@(0x07)];
[arrayMsg addObject:@(0x00)];
[arrayMsg addObject:@(0x00)];
[arrayMsg addObject:@(0x00)];
[arrayMsg addObject:@(0x0E)];
[arrayMsg addObject:@(0x00)];
// ==cmd_Datalen==
[arrayMsg addObject:@(0x01)];
[arrayMsg addObject:@(0x00)];
// ==cmd_DataSum==
[arrayMsg addObject:@(0x01)];
[arrayMsg addObject:@(0x00)];
// ==cmd_Data==
[arrayMsg addObject:@(0x01)];
Byte socketShake[20];
for (int i = 0; i<[arrayMsg count]; i++) {
socketShake[i] = (Byte)[[arrayMsg objectAtIndex:i] intValue];
}
NSData *shakeData = [NSData dataWithBytes:socketShake length:[arrayMsg count]];
// withTimeout -1 : 无穷大,一直等
// tag : 消息标记
[self.clientSocket writeData:shakeData withTimeout:-1 tag:0];
#pragma mark - GCDAsyncSocketDelegate
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port {
[self showMessageWithStr:@"链接成功"];
NSLog(@"链接成功");
[self showMessageWithStr:[NSString stringWithFormat:@"服务器IP: %@", host]];
[self.clientSocket readDataWithTimeout:-1 tag:0];
}
// 收到消息
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
NSLog(@"收到消息");
NSString *str = [self HexStringWithData:data];
[self showMessageWithStr:[NSString stringWithFormat:@"收到消息:%@",str]];
NSLog(@"收到消息:str = %@",str);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
// 信息展示
- (void)showMessageWithStr:(NSString *)str {
self.showMessageTF.text = [self.showMessageTF.text stringByAppendingFormat:@"%@\n", str];
}
// 链接失败
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
NSLog(@"链接失败 \t,err = %@,%ld",err.domain,(long)err.code);
}
//data转为十六进制字符串
-(NSString *)HexStringWithData:(NSData *)data{
Byte *bytes = (Byte *)[data bytes];
NSString *hexStr=@"";
for(int i=0;i<[data length];i++) {
NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
if([newHexStr length]==1){
hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
}
else{
hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
}
}
hexStr = [hexStr uppercaseString];
return hexStr;
}
网友评论