If I open a network connection with the code below do I need to do anything special to keep the program running? It is opening the streams, but then the program just exits. Do I need to fire up a thread or something?
All I am trying to do is connect to time-a.nist.gov on port 13. All that server does is print out the time and then disconnect. My stream:handleEvent: delegate isn't even getting called.
The output of my code (not all the code that produces this output is shown):
2009-01-08 00:25:00.878 StreamTester[25503:10b] Starting StreamTester...
2009-01-08 00:25:01.086 StreamTester[25503:10b] Hostname = <NSHost 0x1072e0> time-a.nist.gov (<CFArray 0x107220 [0xa041c1a0]>{type = mutable-small, count = 2, values = (
0 : <CFString 0x107b60 [0xa041c1a0]>{contents = "time-a.nist.gov"}
1 : <CFString 0x1076a0 [0xa041c1a0]>{contents = "time-a"}
)} <CFArray 0x107200 [0xa041c1a0]>{type = mutable-small, count = 1, values = (
0 : <CFString 0x1072b0 [0xa041c1a0]>{contents = "129.6.15.28"}
)})
2009-01-08 00:25:01.087 StreamTester[25503:10b] in connect to host!
2009-01-08 00:25:01.091 StreamTester[25503:10b] istream = <NSCFInputStream: 0x108050>
The Debugger has exited with status 0.
Have mercy on me I am a better than average Java programmer, but total newb for Objective-C and Cocoa
All I am trying to do is connect to time-a.nist.gov on port 13. All that server does is print out the time and then disconnect. My stream:handleEvent: delegate isn't even getting called.
Code:
-(void)connectToHost {
NSLog(@"in connect to host!");
[NSStream getStreamsToHost:host port:13 inputStream:&iStream outputStream:&oStream];
[iStream retain];
NSLog(@"istream = %@",iStream);
[oStream retain];
[iStream setDelegate:self];
[oStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[iStream open];
[oStream open];
}
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
NSLog(@"in handleEvent, eventcode = %i", eventCode);
switch(eventCode) {
case NSStreamEventHasBytesAvailable: {
NSLog(@"in has bytes");
if(!data) {
data = [[NSMutableData data] retain];
}
uint8_t buf[1024];
unsigned int len = 0;
len = [(NSInputStream *)stream read:buf maxLength:1024];
if(len) {
[data appendBytes:(const void *)buf length:len];
NSLog(@"len: %i", len);
//[bytesRead setIntValue:[bytesRead intValue]+len];
} else {
NSLog(@"no buffer!");
}
break;
}
}
}
The output of my code (not all the code that produces this output is shown):
2009-01-08 00:25:00.878 StreamTester[25503:10b] Starting StreamTester...
2009-01-08 00:25:01.086 StreamTester[25503:10b] Hostname = <NSHost 0x1072e0> time-a.nist.gov (<CFArray 0x107220 [0xa041c1a0]>{type = mutable-small, count = 2, values = (
0 : <CFString 0x107b60 [0xa041c1a0]>{contents = "time-a.nist.gov"}
1 : <CFString 0x1076a0 [0xa041c1a0]>{contents = "time-a"}
)} <CFArray 0x107200 [0xa041c1a0]>{type = mutable-small, count = 1, values = (
0 : <CFString 0x1072b0 [0xa041c1a0]>{contents = "129.6.15.28"}
)})
2009-01-08 00:25:01.087 StreamTester[25503:10b] in connect to host!
2009-01-08 00:25:01.091 StreamTester[25503:10b] istream = <NSCFInputStream: 0x108050>
The Debugger has exited with status 0.
Have mercy on me I am a better than average Java programmer, but total newb for Objective-C and Cocoa