PDA

View Full Version : Setting Up Socket Streams




sbandol
Jul 12, 2008, 01:38 PM
Hi all,

I have a basic(for most of you) question.
Im trying to make a very simple app that will have 1 button (for example) and when its pushed the app will create a socket connection with a host and will send it a message.

So , i have found this on ADC:

- (IBAction)searchForSite:(id)sender
{
NSString *urlStr = [sender stringValue];
if (![urlStr isEqualToString:@""]) {
[searchField setEnabled:NO];
NSURL *website = [NSURL URLWithString:urlStr];
if (!website) {
NSLog(@"%@ is not a valid URL");
return;
}
NSHost *host = [NSHost hostWithName:[website host]];
// iStream and oStream are instance variables
[NSStream getStreamsToHost:host port:80 inputStream:&iStream
outputStream:&oStream];
[iStream retain];
[oStream retain];
[iStream setDelegate:self];
[oStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[iStream open];
[oStream open];
}
}


but i dont realy understand where i have to put the ip of the host (192.168.1.10) and how can i define iStream and oStream.

Just to mention that 192.168.1.1 already have opened socket and are always listening a specified port.


All your help will be apreciated.



sbandol
Jul 12, 2008, 06:49 PM
So i made this :

- (IBAction)reset:(id)sender
{
[textField setStringValue:@"Testing Socket"];



NSString *urlStr = [sender stringValue];
if (![urlStr isEqualToString:@""]) {

NSURL *website = [NSURL URLWithString: @"http://192.168.1.1"];
if (!website) {
NSLog(@"%@ is not a valid URL");
return;
}
NSHost *host = [NSHost hostWithName:@"http://192.168.1.1"];
NSInputStream *iStream = [NSString stringWithFormat:@"ping"];
NSOutputStream *oStream = [NSString stringWithFormat:@"ping"];
[NSStream getStreamsToHost:host port:8000 inputStream:&iStream
outputStream:&oStream];
[iStream retain];
[oStream retain];
[iStream setDelegate:self];
[oStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[iStream open];
[oStream open];
}


}



@end


but my 192.168.1.1 still not recieve nothing...
where i'm wrong?

pccBryan
Oct 8, 2008, 02:41 PM
Did you ever get this figured out? I am trying to do the same thing. I have been looking for a full tutorial app for doing this and have not found anything yet. I need to hit a certain port on a server to retrieve user information, so I believe this is the route I need to be on. Thanks for any help you can provide.

beachdog
Oct 8, 2008, 03:11 PM
Here is some of my working code:

NSHost* host = [NSHost hostWithAddress:serverAddress];
if( host ) {
[NSStream getStreamsToHost:host port:server_port inputStream:&iStream outputStream:&oStream] ;

if( nil != iStream && nil != oStream ) {
[iStream retain];
[oStream retain];

[iStream setDelegate:self] ;
[oStream setDelegate:self] ;

[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[iStream open];
[oStream open];
bConnect = TRUE ;
}
NSLog(@"input stream %@", nil==iStream?@"was not created":@"was created");
NSLog(@"output stream %@", nil==oStream?@"was not created":@"was created");
}


and then...


- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {

NSLog(@"stream event %d", eventCode) ;
if( stream == iStream ) NSLog(@"on input stream");
else if( stream == oStream ) NSLog(@"on output stream");
else NSLog(@"on unknown stream identifier") ;

switch(eventCode) {
case NSStreamEventEndEncountered:
{
NSLog(@"stream ended; will be closed") ;
[stream close];
[stream removeFromRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[stream release];
stream = nil; // stream is ivar, so reinit it
break;
}
case NSStreamEventErrorOccurred:
NSLog(@"stream error") ;
break ;

case NSStreamEventHasBytesAvailable:
//TODO: read here
break ;

case NSStreamEventNone:
NSLog(@"stream null event") ;
break ;

case NSStreamEventOpenCompleted:
NSLog(@"stream is now open") ;
break ;

case NSStreamEventHasSpaceAvailable:
//write here
break ;
}

}