iOS socket manager 你应该保持不同美女之间的联系
你只需要N个美女的微信 或者说IP
你只需要会撩美女的裙子 或者说端口
可连接一切的原生,接下来直接展示
#import "SocketManager.h"
@interface SocketManager ()<NSStreamDelegate>
{
NSInputStream *inputStream;
NSOutputStream *outputStream;
}
@end
@implementation SocketManager
+ (instancetype)sharedSocketManager {
static SocketManager *socketManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
socketManager = [[SocketManager alloc] init];
});
return socketManager;
}
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{
NSLog(@"eventCode:%lu",(unsigned long)eventCode);
switch (eventCode) {
case NSStreamEventOpenCompleted:
NSLog(@"输入输出流打开完成");
break;
case NSStreamEventHasBytesAvailable:
NSLog(@"有字节可读");
[self readData];
break;
case NSStreamEventHasSpaceAvailable:
NSLog(@"可以发送字节");
break;
case NSStreamEventErrorOccurred:
NSLog(@" 连接出现错误");
break;
case NSStreamEventEndEncountered:
NSLog(@"连接结束");
// 关闭输入输出流
[inputStream close];
[outputStream close];
// 从主运行循环移除
[inputStream removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
break;
default:
break;
}
}
- (void)connect{
NSString *host = IP地址;
int port = 端口号;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)host, port, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)(readStream);
outputStream = (__bridge NSOutputStream *)(writeStream);
inputStream.delegate = self;
outputStream.delegate = self;
[inputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
- (void)login{
写入的命令数据,此处省略
[outputStream write:data.bytes maxLength:data.length];
}
- (void)readData{
uint8_t buf[1024];
NSInteger len = [inputStream read:buf maxLength:sizeof(buf)];
NSData *data = [NSData dataWithBytes:buf length:len];
NSString *recStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",recStr);
// [self cleanUpStream:inputStream];
}
- (void)cleanUpStream:(NSStream *)stream
{
[stream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[stream close];
stream = nil;
}
@end
来自于 C 的 socket manager
#import "SocketManager1.h"
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <syslog.h>
#include <errno.h>
#include <stdlib.h>
@implementation SocketManager1
- (void)socketConnect:(NSString *)host Port:(int)port{
const char *ip = IP地址;
self.socketFD = socket(AF_INET, SOCK_STREAM, 0);
NSData *dataLogin = [self login];
NSInteger bytes = [dataLogin length];
NSLog(@"socketFD:%d",self.socketFD);
struct sockaddr_in nativeAdd;
nativeAdd.sin_len = sizeof(struct sockaddr);
nativeAdd.sin_family = AF_INET;
nativeAdd.sin_port = htons(port);
nativeAdd.sin_addr.s_addr = inet_addr(ip);
self.status = connect(self.socketFD, (struct sockaddr *) &nativeAdd, sizeof(nativeAdd));
if (-1 == self.status) {
close(self.socketFD);
}
if (self.status <0) {
NSLog(@"连接失败:%d",errno);
}else{
NSLog(@"连接成功:%d",self.status);
self.status = write(self.socketFD, [dataLogin bytes], bytes);
if (self.status <0) {
NSLog(@"写入失败:%d",errno);
}else{
NSLog(@"登陆成功:%d",self.status);
[self read];
}
}
}
- (void)read{
[NSThread detachNewThreadSelector:@selector(thread:) toTarget:self withObject:@"thread1"];
}
- (void)thread:(NSString *)sender{
NSThread *thread = [NSThread currentThread];
NSLog(@"%@",thread);
NSMutableData *data = [[NSMutableData alloc]init];
BOOL waitingForData = YES;
int i = 0;
do {
const char *buffer[10000];
int length = sizeof(buffer);
int result = read(self.socketFD, &buffer, length);
NSLog(@"result:%d",result);
if (result >0) {
[data appendBytes:buffer length:result];
NSLog(@"data:%@",data);
}else{
NSString *string = [[NSString alloc]initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"string:%@",string);
waitingForData = NO;
}
++i;
NSLog(@"i:%d",i);
NSLog(@"waitingForData:%d",waitingForData);
} while (waitingForData == YES);
}
- (NSData *)login{
写入的命令数据,此处省略;
return data;
}
有必要提到在一个完整的请求和响应过程中,控制信息起到非常关键的作用
-
syn 就是同步的缩写,客户端会首先发送 syn 数据包,请求服务端建立连接
-
ack 就是响应的意思,它是对发送 syn 数据包的响应
-
fin 是终止的意思,它表示客户端/服务端想要终止连接
由于网络环境的复杂多变,经常存在数据丢包的情况,所以双方通讯时需要相互确认对方的数据包是否已经到达,判断的标准就是ack的值。
网友评论