当前位置:编程学习 > C/C++ >>

Objective C TCP 通讯实例

TCP通讯实例
Communicator.h 
[cpp] 
#import <Foundation/Foundation.h>  
   
@inte易做图ce 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 readStream;  
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:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];  
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];  
      
    [inputStream open];  
    [outputStream open];  
}  
   
- (void)close {  
    NSLog(@"Closing streams.");  
      
    [inputStream close];  
    [outputStream close];  
      
    [inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];  
    [outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];  
      
    [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
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,