我在Swift中使用Socket TCP并在Swift项目中使用Objective c文件,之后我调用initNetworkCommunication()流未打开
任何人都知道为什么?
.h文件:
@interface SocketTCP : NSObject <NSStreamDelegate>
{
NSInputStream *inputStream;
NSOutputStream *outputStream;
NSString* CmdType,*IpAddress;
}
.m文件:
- (void) initNetworkCommunication:(NSString*) SERVER_IP :(int)SERVER_PORT {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)SERVER_IP, 5555, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
NSLog(@"She be opened, sir!");
}
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
switch (streamEvent) {
case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
case NSStreamEventHasSpaceAvailable:
NSLog(@"Stream has space available.");
case NSStreamEventHasBytesAvailable:
NSLog(@"Stream has byte available.");
case NSStreamEventErrorOccurred:
NSLog(@"Can not connect to the host!");
break;
case NSStreamEventEndEncountered:
default:
NSLog(@"Unknown event");
}
}