Setting Up Socket Streams

Discussion in 'iPhone/iPad Programming' started by sbandol, Jul 12, 2008.

  1. macrumors newbie

    Joined:
    Mar 3, 2008
    #1
    Hi all,

    I have a basic(for most of you) question.
    I´m 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:

    Code:
    - (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.
     
  2. thread starter macrumors newbie

    Joined:
    Mar 3, 2008
    #2
    continuation

    So i made this :

    Code:
    - (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?
     
  3. macrumors newbie

    Joined:
    Oct 8, 2008
    #3
    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.
     
  4. macrumors member

    Joined:
    Aug 10, 2008
    #4
    Here is some of my working code:
    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...

    Code:
    - (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 ;
    	}
    			
    }
    
    
     

Share This Page