Objective c tcp communication instance

Source: Internet
Author: User

TCP communication instance Communicator. h [cpp] # import <Foundation/Foundation. h> @ interface Communicator: NSObject <NSStreamDelegate >{@ public NSString * host; int port ;}- (void) setup;-(void) open;-(void) close; -(void) stream :( NSStream *) stream handleEvent :( NSStreamEvent) event;-(void) readIn :( NSString *) s;-(void) writeOut :( NSString *) s; @ end Communicator. m [cpp] # import "Communicator. h "CFReadStreamRef readStr Eam; CFWriteStreamRef writeStream; NSInputStream * inputStream; NSOutputStream * outputStream; @ implementation Communicator-(void) setup {NSURL * url = [NSURL URLWithString: host]; NSLog (@ "Setting up connection to % @: % I", [url absoluteString], port); CFStreamCreatePairWithSocketToHost (kCFAllocatorDefault, (CFStringRef) [url host], port, & readStream, & writeStream); if (! CFWriteStreamOpen (writeStream) {NSLog (@ "Error, writeStream not open"); return;} [self open]; NSLog (@ "Status of outputStream: % I ", [outputStream streamStatus]); return;}-(void) open {NSLog (@ "Opening streams. "); inputStream = (NSInputStream *) readStream; outputStream = (NSOutputStream *) writeStream; [inputStream retain]; [outputStream retain]; [inputStream setDelegate: self]; [outputStream setDelegate: self]; [inputStream scheduleInRunLoop: [nsunloop currentRunLoop] forMode: Unknown]; [outputStream restart: [nsunloop currentRunLoop] forMode: Unknown]; [inputStream open]; [outputStream open];} -(void) close {NSLog (@ "Closing streams. "); [inputStream close]; [outputStream close]; [inputStream closing: [nsunloop currentRunLoop] forMode: Unknown]; [outputStream removeFromRunLoop: [nsunloop currentRunLoop] forMode: Unknown]; [inputStream setDelegate: nil]; [outputStream setDelegate: nil]; [inputStream release]; [outputStream release]; inputStream = nil; outputStream = nil;}-(void) stream :( NSStream *) stream handleEvent :( NSStreamEvent) event {NSLog (@ "Stream triggered. "); switch (event) {case NSStreamEventHasSpaceAvailable: {if (stream = outputStream) {NSLog (@" outputStream is ready. ");} break;} case NSStreamEventHasBytesAvailable: {if (stream = inputStream) {NSLog (@" inputStream is ready. "); uint8_t buf [1024]; unsigned int len = 0; len = [inputStream read: buf maxLength: 1024]; if (len> 0) {NSMutableData * data = [[NSMutableData alloc] initWithLength: 0]; [data appendBytes: (const void *) buf length: len]; NSString * s = [[NSString alloc] initWithData: data encoding: NSASCIIStringEncoding]; [self readIn: s]; [data release] ;}} break;} default: {NSLog (@ "Stream is sending an Event: % I", event); break ;}}- (void) readIn :( NSString *) s {NSLog (@ "Reading in the following:"); NSLog (@ "% @", s) ;}- (void) writeOut :( NSString *) s {uint8_t * buf = (uint8_t *) [s UTF8String]; [outputStream write: buf maxLength: strlen (char *) buf)]; NSLog (@ "Writing out the following:"); NSLog (@ "% @", s) ;}@ end StreamExample. m [cpp] # import <Foundation/Foundation. h> # import "Communicator. h "int main (int argc, const char * argv []) {ngutoreleasepool * pool = [[ngutoreleasepool alloc] init]; Communicator * c = [Communicator alloc] init]; c-> host = @ "http: // 127.0.0.1"; c-> port = 6789; [c setup]; [c open]; [pool drain]; return 0 ;}

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.