做了第二个App,异步Sockets连接

/ 0评 / 0

学习了AsyncSocket开源库的基础用法,使用这个库建立iOS与Internet服务器的socket连接来实时获取数据。

//开启监听
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    NSLog(@"onSocket:%p didConnectToHost:%@ port:%hu", sock, host, port);
    [sock readDataWithTimeout:-1tag:0];
}

-(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    self.dataInfoStr = @"";
    //((ViewController *)self.viewWin).txtView.text = @"";
    NSString *str = [[NSStringalloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", str);
    [sock readDataWithTimeout:-1tag:0];
}

- (void)onSocket:(AsyncSocket *)sock didSecure:(BOOL)flag
{
    NSLog(@"onSocket:%p didSecure:YES", sock);
}
- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
    NSLog(@"onSocket:%p willDisconnectWithError:%@", sock, err);
}
- (void)onSocketDidDisconnect:(AsyncSocket *)sock
{
    //断开连接了
    NSLog(@"onSocketDidDisconnect:%p", sock);
}

通过代理事件(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag来读取获取到的信息。

发表评论

邮箱地址不会被公开。 必填项已用*标注