Http://blog.sina.com.cn/s/blog_7a2f0a830101ecv4.html
Clinetsocket 1, Viewcontroller.h@interface viewcontroller:uiviewcontroller<</span>gcdasyncsocketdelegate , uitextfielddelegate>{gcdasyncsocket *socket;} @property (Strong) Gcdasyncsocket *socket@property (Strong, nonatomic) Iboutlet Uitextfield *host; @property (Strong, nonatomic) Iboutlet Uitextfield *message; @property (Strong, nonatomic) Iboutlet Uitextfield *port; @property (Strong, nonatomic) Iboutlet Uitextview *status;-(ibaction) Connect: (ID) sender;-(ibaction) Send: (ID) sender;@ ENDVIEWCONTROLLER.M//Connection server-(ibaction) Connect: (ID) Sender {socket = [[Gcdasyncsocket alloc]initwithdelegate:self Dele Gatequeue:dispatch_get_main_queue ()]; Socket.delegate = self; Nserror *err = nil; BOOL connstate=[socket connectToHost:host.text onport:[port.text Intvalue] error:&err]; if (!connstate) {[Self addText:err.description]; }else {NSLog (@ "Connecting server:%@ success", Host.text); [Self addtext:@ "open port"]; }}-(void) Socket: (Gcdasyncsocket *) sock DidcoNnecttohost: (NSString *) host port: (uint16_t) port{[self addtext:[nsstring stringwithformat:@ "connected to:%@", host]]; [Socket readdatawithtimeout:-1 tag:0];} -(void) Socketdiddisconnect: (Gcdasyncsocket *) sock witherror: (Nserror *) err{NSLog (@ "Socket did is connect:%@", err);} Send data-(ibaction) Send: (ID) Sender {[Socket writedata:[message.text datausingencoding:nsutf8stringencoding] Withtimeout:-1 tag:0]; [Self addtext:[nsstring stringwithformat:@ "data sent:%@", Message.Text]]; [Message Resignfirstresponder]; [Socket readdatawithtimeout:-1 tag:0];} Read the data obtained by the server-(void) Socket: (Gcdasyncsocket *) sock didreaddata: (NSData *) data withtag: (long) tag{nsstring *newmessage = [[NSString Alloc] Initwithdata:data encoding:nsutf8stringencoding]; [Self addtext:[nsstring stringwithformat:@ "server:%@:%@", Sock.connectedhost,newmessage]]; [Socket readdatawithtimeout:-1 tag:0];} #pragma mark-view lifecycle-(void) AddText: (NSString *) str{status.text = [Status.text stringbyappendingformat:@"%@\n", str];} -(void) viewdidload{[Super Viewdidload]; Set the default server address and port Host.text = @ "192.168.2.107"; Port.text = @ "54321"; Do any additional setup after loading the view, typically from a nib.} 2, ServiceSocket1, appdelegate.h #import "GCDAsyncSocket.h" #import "GCDAsyncUdpSocket.h" @interface appdelegate: NSObject <</span>nsapplicationdelegate,gcdasyncsocketdelegate,gcdasyncudpsocketdelegate>{ Gcdasyncsocket *socket; Gcdasyncsocket *s;} @property (Strong) Gcdasyncsocket *socket;-(ibaction) listen: (ID) sender; @property (unsafe_unretained) Iboutlet Nstextview *status; @property (unsafe_unretained) iboutlet Nstextfield *port; @property (unsafe_unretained) IBOutlet Nstextfield *host, @property (assign) Iboutlet Nswindow *window;2, APPDELEGATE.M @implementation appdelegate@synthesize Status, @synthesize port, @synthesize host, @synthesize window = _window; @synthesize socket;-(void) Applicationdidfinishlaunching: (nsnotification *) anotification{//server port number Port.stRingvalue = @ "54321";} -(void) AddText: (NSString *) str{status.string = [status.string stringbyappendingformat:@ "%@\n", str];} Start listening-(Ibaction) listen: (ID) Sender {NSLog (@ "Begin listen"); socket = [[Gcdasyncsocket alloc] initwithdelegate:self delegatequeue:dispatch_get_main_queue ()]; Nserror *err = nil; if (![ Socket Acceptonport:[port IntegerValue] error:&err]) {[Self addText:err.description]; }else {[self addtext:[nsstring stringwithformat:@] started listening on%d ports. ", Port.integervalue]]; }}-(void) Socket: (Gcdasyncsocket *) sender Didacceptnewsocket: (Gcdasyncsocket *) newsocket{//The "sender" parameter is The listensocket we created. The "Newsocket" is a new instance of Gcdasyncsocket. It represents the accepted incoming client connection. Do server stuff with Newsocket ... [Self addtext:[nsstring stringwithformat:@ "establish connection with%@", Newsocket.connectedhost]]; s = newsocket; S.delegate = self; [s readdatawithtimeout:-1 tag:0];}Write data to client-(void) Socket: (Gcdasyncsocket *) sock didreaddata: (NSData *) data withtag: (long) tag{nsstring *receive = [[NSSt Ring alloc] Initwithdata:data encoding:nsutf8stringencoding]; [Self addtext:[nsstring stringwithformat:@ "%@:%@", Sock.connectedhost,receive]]; NSString *reply = [NSString stringwithformat:@ "Server Received:%@", receive]; [s writedata:[reply datausingencoding:nsutf8stringencoding] withtimeout:-1 tag:0]; [s readdatawithtimeout:-1 tag:0];} @end
iOS socket (Base demo)