Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 12, 2008, 01: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, 06: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, 02: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, 03: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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Setting up wireless speaker system mooseface Apple TV and Home Theater 3 Nov 19, 2011 09:05 AM
Getting daughter iPhone, best iTunes set up? lowonthe456 iPhone 16 Nov 18, 2011 09:08 PM
Optimal set up to stream music? brayhite Apple TV and Home Theater 1 Oct 27, 2011 01:54 AM
How do I set up My AirPort Extreme + WD My Book To Make a Media Drive For my Network? polo3400 Mac OS X Server, Xserve, and Networking 2 Jul 12, 2011 12:30 PM
Resolved: Help!!!! New mba set up problem!!! Sugi209 MacBook Air 13 Feb 9, 2011 10:21 PM


All times are GMT -5. The time now is 02:35 PM.

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

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