Gcdaynscsocket Simple to use-service side

Source: Internet
Author: User

From the last time I wrote "Gcdaynscsocket Simple use-service side" almost one months, now the service side of the introduction to fill up.

Service side of the introduction is relatively simple, because the server and the client to receive and send data is the same way, the difference is that the server is to open a service passive waiting for the client access. So just describe how to turn the service on and off.

1. Open service

Gcdaynscsocket encapsulation is very good, in most cases we just have to invoke its interface.

To open a service callable method:

/**

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

* When a connection is accepted, a new instance of Gcdasyncsocket would be spawned to handle it,

* and the Socket:didAcceptNewSocket:delegate method would be invoked.

*

* The socket would listen on all available interfaces (e.g. WiFi, Ethernet, etc)

**/

-(BOOL) Acceptonport: (uint16_t) port error: (NSERROR * *) errptr;

This method binds a port number port, Errptr is an error variable that can be used to determine whether the service is open successfully

Defines a flag bit that determines whether the current service is open

BOOL isrunning;

/**

* @brief Open Service

*/

-(void) StartServer

{

if (!isrunning)

{

[Self initserver];

[_serversocket readdatawithtimeout:-1 tag:0];

IsRunning = YES;

}

}

/**

* @brief Service-side initialization

*/

-(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-to-%@", error);

}

Else

{

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

}

}

2. Close the service

Stop Service direct call-(void) Disconnect method to

/**

* @brief Stop Service

*/

-(void) stopserver

{

if (isrunning)

{

[_serversocket disconnect];

IsRunning = NO;

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

}

}

3. Delegation method

The following three delegate methods are called when there is a client-access server:

/**

* This method was called immediately prior to Socket:didacceptnewsocket:.

* It Optionally allows a listening socket to specify, the Socketqueue for a new accepted socket.

* If This method was not implemented, or returns NULL, the new accepted socket would create its own default queue.

*

* Since You cannot autorelease a dispatch_queue,

* This method uses the ' new ' prefix in it name to specify, the returned queue has been retained.

*

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

* Return Dispatch_queue_create ("Myqueue", NULL);

*

* If You is placing multiple sockets on the same queue,

* Then care should being 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.

*

* Must retain the newsocket if you wish to handle the connection.

* Otherwise The Newsocket instance would be released and the spawned connection would be closed.

*

* By default the new socket would have the same delegate and Delegatequeue.

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

**/

-(void) Socket: (Gcdasyncsocket *) sock didacceptnewsocket: (Gcdasyncsocket *) newsocket

/**

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

* The host parameter would be a IP address and not a DNS name.

**/

-(void) Socket: (Gcdasyncsocket *) sock didconnecttohost: (NSString *) host port: (uint16_t) port;

Client and server code is relatively simple, the demo after the modification I will upload, PS: There is no place to upload

Gcdaynscsocket Simple to use-service side

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.