Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 12, 2008, 02:38 PM   #1
sbandol
macrumors newbie
 
Join Date: Mar 2008
Setting Up Socket Streams

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:

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.
sbandol is offline   0 Reply With Quote
Old Jul 12, 2008, 07:49 PM   #2
sbandol
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
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?
sbandol is offline   0 Reply With Quote
Old Oct 8, 2008, 03:41 PM   #3
pccBryan
macrumors newbie
 
Join Date: Oct 2008
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.
pccBryan is offline   0 Reply With Quote
Old Oct 8, 2008, 04:11 PM   #4
beachdog
macrumors member
 
Join Date: Aug 2008
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 ;
	}
			
}
beachdog is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
download of streams.......???? johannestietze OS X 10.8 Mountain Lion 2 Mar 18, 2013 01:58 AM
download streams on ML johannestietze OS X 10.8 Mountain Lion 0 Aug 14, 2012 02:15 PM
headphone socket stephenxiii OS X 10.8 Mountain Lion 4 Aug 13, 2012 05:14 PM
C++ Socket programming tr!pf!3 Mac Programming 8 Aug 4, 2012 04:04 AM
Motherboard upgraded to the 2 socket qwarkas Mac Pro 2 Jun 13, 2012 07:13 PM

Forum Jump

All times are GMT -5. The time now is 08:59 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC