GCDAynscSocket简单使用-服务端

距离上次写《GCDAynscSocket简单使用-服务端》差不多一个月了,现在把服务端的介绍给补上。

服务端的介绍比较简单,因为服务端和客户端的接收和发送数据的方法是一样的,不同的地方在于服务端是开启一个服务被动的等待客户端的接入。所以只介绍一下如何开启和关闭服务。

1、开启服务

GCDAynscSocket封装的非常好了,大部分情况下我们只要调用它的接口就可以了。

开启服务可调用的方法:

/**

 * Tells the socket to begin listening and accepting connections on the given port.

 * When a connection is accepted, a new instance of GCDAsyncSocket will be spawned to handle it,

 * and the socket:didAcceptNewSocket: delegate method will be invoked.

 * 

 * The socket will listen on all available interfaces (e.g. wifi, ethernet, etc)

**/

- (BOOL)acceptOnPort:(uint16_t)port error:(NSError **)errPtr;

此方法绑定一个端口号port,  errPtr是错误变量,可通过该变量判断服务开启是否成功

// 定义一个标志位,用于判断当前服务是否开启

BOOL     isRunning;

/**

 *  @brief   开启服务

 */

- (void)startServer

{

    if (!isRunning)

    {

        [self initServer];

        [_serverSocket readDataWithTimeout:-1 tag:0];

        isRunning = YES;

    }

}

/**

 *  @brief   服务端初始化

 */

- (void)initServer

{

    allClientArray = [NSMutableArray array];    

    dispatch_queue_t socketQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    

    _serverSocket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:socketQueue];    

    NSError *error = nil;    

    [_serverSocket acceptOnPort:_socketPort error:&error];    

    if (error != nil)

    {

        NSLog(@"error --> %@", error);

    }

    else

    {

        NSLog(@"server start...");

    }

}

2、关闭服务

停止服务直接调用- (void)disconnect方法即可

/**

 *  @brief   停止服务

 */

- (void)stopServer

{

    if (isRunning)

    {

        [_serverSocket disconnect];      

        isRunning = NO;

        NSLog(@"server stop...");

    }

}

3、委托方法

在有客户端接入的服务器时,会调用下面三个委托方法:

/**

 * This method is called immediately prior to socket:didAcceptNewSocket:.

 * It optionally allows a listening socket to specify the socketQueue for a new accepted socket.

 * If this method is not implemented, or returns NULL, the new accepted socket will create its own default queue.

 * 

 * Since you cannot autorelease a dispatch_queue,

 * this method uses the "new" prefix in its name to specify that the returned queue has been retained.

 * 

 * Thus you could do something like this in the implementation:

 * return dispatch_queue_create("MyQueue", NULL);

 * 

 * If you are placing multiple sockets on the same queue,

 * then care should be taken to increment the retain count each time this method is invoked.

 * 

 * For example, your implementation might look something like this:

 * dispatch_retain(myExistingQueue);

 * return myExistingQueue;

**/

- (dispatch_queue_t)newSocketQueueForConnectionFromAddress:(NSData *)address onSocket:(GCDAsyncSocket *)sock

/**

 * Called when a socket accepts a connection.

 * Another socket is automatically spawned to handle it.

 * 

 * You must retain the newSocket if you wish to handle the connection.

 * Otherwise the newSocket instance will be released and the spawned connection will be closed.

 * 

 * By default the new socket will have the same delegate and delegateQueue.

 * You may, of course, change this at any time.

**/

- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket

/**

 * Called when a socket connects and is ready for reading and writing.

 * The host parameter will be an IP address, not a DNS name.

**/

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

客户端和服务端的代码比较简单,demo修改后我会上传,PS:目前还没发现可以上传的地方

原文地址:https://www.cnblogs.com/songshu-yilia/p/4613819.html