GCDAsyncSocket didReadData

时间:2017-07-05 12:49:19

标签: ios objective-c sockets gcdasyncsocket

这是我的GCDAsyncSocket代码

+ (instancetype)shared
{
    static id shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shared = [[self alloc] init];
    });
    return shared;
}

- (id)init
{
    self = [super init];

    if (self)
    {
        _socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    }

    return self;
}

- (void)setUpSocket:(SocketTCP *)socketTCP
{
    _socketTCP = socketTCP;
}

- (void)connect
{
    NSError *err = nil;
    if (![_socket connectToHost:_socketTCP.host onPort:_socketTCP.port error:&err])
    {
        NSLog(@"Fail");
    }
    else
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:CONNECT_FROM_SOCET_NOTIFICATION object:nil];
        NSLog(@"Connect");
    }
}


- (void)disconnect
{
    [_socket disconnect];
}

- (void)sendPakege:(Packege *)package
{
    NSLog(@"Send packege %@",package.description);
    [_socket writeData:package.data withTimeout:-1 tag:0];
}

#pragma mark - GCDAsyncSocketDelegate

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
    NSLog(@"Connected to host: %@ with Port: %i",host, port);
}


- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    [sock readDataWithTimeout:-1 tag:tag];
    [PackegeManager parseData:data];
}

- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
    [sock readDataWithTimeout:-1 tag:tag];
}

当我写数据套接字时,必须阅读超过100个数据包。但它只读10-15。包装没有丢失(我在android上检查过)。谁可以帮助如何回收许多包装?

0 个答案:

没有答案