NSStreamEventHasBytesAvailable not getting thrown
NSStreamEventHasBytesAvailable not getting thrown
- Subject: NSStreamEventHasBytesAvailable not getting thrown
- From: Adam <email@hidden>
- Date: Tue, 27 Apr 2004 19:16:10 -0400
I have modified some code written by Prachi to create a socket stream
object. His code was originally written to interact with a GUI and I am
trying to adapt it so that other objects (main in this simple case) can
connect to the server and send and receive commands without the user
and any GUI.
His GUI version works great but my class is getting stuck somewhere.
Based on the debugging strings I output with NSLog(), I am successfully
making a connection, but the NSStreamEventHasBytesAvailable event is
not getting called. So, nothing is coming back after I make the
connection. I am a newbie coming over from the world of java to obj-c.
If anyone could be of help I would be grateful.
"Attached" is
main.m
CFSocketStream.m
CFSocketStream.h
-------------------------
main.m
-------------------------
#import <Cocoa/Cocoa.h>
#import "CFSocketStream.h"
int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CFSocketStream *FIBSSocket = [[CFSocketStream alloc] init];
NSLog(@"FIBSSocket 1: %@", FIBSSocket);
[FIBSSocket connect];
[FIBSSocket sendMessage:@"hi"];
[FIBSSocket disconnect];
[FIBSSocket release];
[pool release];
return 0;
// return NSApplicationMain(argc, argv);
}
-------------------------
CFSocketStream.h
-------------------------
#import <Foundation/Foundation.h>
//#include "FIBSCookieMonster.h"
@interface CFSocketStream : NSObject {
@private
NSInputStream *inputStream;
NSOutputStream *outputStream;
NSString *serverAddress;
int serverPort;
BOOL connected;
}
- (NSInputStream *)inputStream;
- (void)setInputStream:(NSInputStream *)newInputStream;
- (NSOutputStream *)outputStream;
- (void)setOutputStream:(NSOutputStream *)newOutputStream;
- (BOOL)isConnected;
- (void)setConnected:(BOOL)isConnected;
- (void)connect;
- (void)disconnect;
- (void)sendMessage:(NSString *)stringToSend;
@end
-------------------------
CFSocketStream.m
-------------------------
#import "CFSocketStream.h"
//#include "clip.h"
@implementation CFSocketStream
- (id)init
{
[super init];
serverAddress = @"fibs.com";
serverPort = 4321;
return self;
}
- (void)dealloc
{
NSLog(@"Deallocating %@", self);
[self setInputStream:nil];
[self setOutputStream:nil];
[super dealloc];
return;
}
- (NSInputStream *)inputStream
{
return [[inputStream retain] autorelease];
}
- (void)setInputStream:(NSInputStream *)newInputStream
{
if (inputStream != newInputStream) {
[inputStream release];
inputStream = [newInputStream retain];
}
return;
}
- (NSOutputStream *)outputStream
{
return [[outputStream retain] autorelease];
}
- (void)setOutputStream:(NSOutputStream *)newOutputStream {
if (outputStream != newOutputStream) {
[outputStream release];
outputStream = [newOutputStream retain];
}
}
- (BOOL)isConnected
{
return connected;
}
- (void)setConnected:(BOOL)isConnected
{
connected = isConnected;
}
- (void)connect
{
// Connect to the specified host
[NSStream getStreamsToHost:[NSHost hostWithName:serverAddress]
port:serverPort
inputStream:&inputStream
outputStream:&outputStream];
// Set ourself to to the delegate, schedule ourselves in the
runloop, and open the streams
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
//ResetFIBSCookieMonster();
NSLog(@"ConnectDone");
}
- (void)disconnect
{
// Disconnect, close and dealloc the streams, release the
FIBSCookieMonster
[self setConnected:NO];
[inputStream close];
[outputStream close];
[self setInputStream:nil];
[self setOutputStream:nil];
//ReleaseFIBSCookieMonster();
}
- (void)sendMessage:(NSString *)stringToSend
{
// Send the message stringToSend to the receiver. Append a \r\n to
the end
// for telnet like behavior
NSString *message = [stringToSend stringByAppendingString:@"\r\n"];
NSData *messageData = [message
dataUsingEncoding:NSUTF8StringEncoding];
[outputStream write:[messageData bytes] maxLength:[messageData
length]];
NSLog(stringToSend);
}
// This method handles all stream events (stream open, close, data
available, etc)
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)streamEvent
{
NSLog(@"StreamEventStart");
NSData *messageData = nil;
uint8_t buffer[1024];
int bytesRead = 0;
NSString *messageString = nil;
switch(streamEvent) {
case NSStreamEventNone:
NSLog(@"NSStreamEventNone");
break;
case NSStreamEventOpenCompleted:
// If the stream has completed opening, update the UI
NSLog(@"Connected\n");
[self setConnected:YES];
break;
case NSStreamEventHasBytesAvailable:
NSLog(@"NSStreamEventHasBytesAvailable");
NSString *loginString = @"login CocoaFIBSDev 1008 username password";
NSString *newLineChar = @"\n";
NSString *carriageReturnChar = @"\r";
NSString *replaceString = @"";
NSString *cookieString = nil;
NSString *messageStringTemp = nil;
NSString *messageStringFinal = nil;
NSString *oneStringFromArray = nil;
NSArray *stringArrayStripNL = nil;
NSData *cString = nil;
int count = 0;
int cookie = 0;
// If the stream has bytes available to read, read it.
bytesRead = [(NSInputStream *)stream read:buffer
maxLength:1024];
messageData = [NSData dataWithBytes:buffer
length:bytesRead];
messageString = [[NSString alloc] initWith
Data:messageData
encoding:NSUTF8StringEncoding];
stringArrayStripNL = [ messageString
componentsSeparatedByString:newLineChar];
count = [stringArrayStripNL count];
int i;
for (i=0; i < count; i++) {
oneStringFromArray = [stringArrayStripNL objectAtIndex:i];
NSArray *stringArrayStripCR = [ oneStringFromArray
componentsSeparatedByString:carriageReturnChar ];
messageStringTemp = [ stringArrayStripCR
componentsJoinedByString:replaceString ];
int aBufferSize = [messageStringTemp length];
char aBuffer[aBufferSize];
cString = [messageStringTemp dataUsingEncoding:[NSString
defaultCStringEncoding]];
[cString getBytes:aBuffer];
//cookie = FIBSCookie(aBuffer);
if (cookie == 21) {
[self sendMessageI:loginString];
NSLog(@"sent login: %s", loginString);
}
cookieString = [[NSString alloc] initWithFormat:@"(%d)", cookie];
messageStringFinal = [cookieString
stringByAppendingString:messageStringTemp];
NSLog(messageStringFinal);
}
[messageString release];
case NSStreamEventHasSpaceAvailable:
NSLog(@"NSStreamEventHasSpaceAvailable");
break;
case NSStreamEventErrorOccurred:
// If an error occurred, log it in the text view and
disconnect
NSLog(@"Error");
NSLog([[stream streamError] description]);
[self disconnect:self];
break;
case NSStreamEventEndEncountered:
// Log the disconnection and disconnect
NSLog(@"Disconected");
[self disconnect:self];
break;
}
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.