zoukankan      html  css  js  c++  java
  • CocoaAsyncSocket使用笔记

    先去github的站点下载最新的包,然后先看看介绍。

    写的比較具体了

    https://github.com/robbiehanson/CocoaAsyncSocket/wiki/Intro_GCDAsyncSocket

    网上非常多都是老版本号的帖子。官方已经推出了GCDAsyncSocket来取代曾经老的AsyncSocket。

    我的项目是no-ARC的,这个框架仅仅有arc的版本号。所以引入GCDAsyncSocket的.h和.m文件后。改动xcode中项目的属性。

    1)targets中“build settings”中找到Compiler for c/c++/Objective-c的选项。

    改为Apple LLVM compiler 3.0 仅仅要是3.0或以上就能够

    2)在“build phases”中“compile sources”中找到GCDAsyncSocket.m,添加參数-fobj-arc

    3)引入GCDAsyncSocket所须要的框架,CFNetwork和security这两个


    这样就能够在我的no-arc的项目中来使用GCDAsyncSocket的库了。

    以下是我写的连接echoServer的client

    - (void) initNetwork{

        socket= [[GCDAsyncSocketalloc] initWithDelegate:selfdelegateQueue:dispatch_get_main_queue()];

        NSError*error = nil;

        if(![socketconnectToHost:@"serverIP"onPort:serverport error:&error]) {

            NSLog(@"is aready connected");

        }

    }


    - (void) sendData{

        NSLog(@"send Message to server");

        NSData*datas = [@"client send msg"dataUsingEncoding:NSUTF8StringEncoding];

        [socket writeData:datas withTimeout:-1 tag:1];//这里tag没用到

        [socket readDataWithTimeout:-1 tag:1];//这句必须加。否则收不到server返回数据

    }


    - (void) socket:(GCDAsyncSocket*)sock didConnectToHost:(NSString*)host port:(uint16_t)port{

        NSLog(@"connected to the server");

    }


    - (void) socket:(GCDAsyncSocket*)sock didReadData:(NSData*)data withTag:(long)tag{

        NSString*msg = [[NSStringalloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"return message=%@",msg);

    }


    - (void) socketDidDisconnect:(GCDAsyncSocket*)sock withError:(NSError*)err{

        NSLog(@"close connected");

    }



  • 相关阅读:
    UVA
    UVALive
    找一
    买书最低价格
    NABCD模型分析
    二维数组--首尾
    结对开发---环
    结对开发---二维数组
    电梯设计需求调研报告
    数组求和(2)
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6790034.html
Copyright © 2011-2022 走看看