Re: Setting Up Socket Streams
Re: Setting Up Socket Streams
- Subject: Re: Setting Up Socket Streams
- From: Chris Hanson <email@hidden>
- Date: Sat, 12 Jul 2008 17:35:57 -0700
On Jul 12, 2008, at 5:22 PM, StaS Bandol wrote:
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(command).
To head this off at the pass, if you want to use HTTP there are easier
ways than writing your own raw HTTP support. You can use the NSURL...
classes to do all such communication, provided your server speaks
standard HTTP.
- (IBAction)reset:(id)sender
{
[textField setStringValue:@"Testing Socket"];
NSString *urlStr = [sender stringValue];
if (![urlStr isEqualToString:@""]) {
NSURL *website = [NSURL URLWithString: @"http://192.168.1.2"];
if (!website) {
NSLog(@"%@ is not a valid URL");
return;
}
NSHost *host = [NSHost hostWithName:@"http://192.168.1.2"];
Note that @"http://192.168.1.2" is a URL, not a host name. Just use
the host name portion of the URL.
NSInputStream *iStream = [NSString stringWithFormat:@"ping"];
NSOutputStream *oStream = [NSString stringWithFormat:@"ping"];
These lines are bogus as you're assigning an NSString instance to
variables that are claimed to point to NSInputStream and
NSOutputStream objects. If you want to set them to some initial
value, set them to nil.
[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?
You register your instance as a delegate for the input and output
streams, do you actually send any data to your host once the output
stream is opened? If you don't, then the host won't receive
anything. Your delegate object should be sent appropriate messages
when events occur on the streams, such as the streams opening or
closing or having data available.
Of course, all of this will be different if you just use the NSURL...
classes to handle the HTTP communication on your behalf, as I
recommend above. It will probably be a lot easier to get right, and
handle lots of the little details for you.
-- Chris
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden