Re: Setting Up Socket Streams
Re: Setting Up Socket Streams
- Subject: Re: Setting Up Socket Streams
- From: StaS Bandol <email@hidden>
- Date: Sun, 13 Jul 2008 22:46:34 +0200
Hi Chris,
Thank you for your response:
My 192.168.1.2 processor can understand standard TCP/IP protocol and
is located in my local network.
So , this 192.168.1.2 is a server in my communication and are always
listening the port 8000 , and my app will be the client which shall
open a socket and send him a command (a string "ping" for example).
After that the server will send a response to my client ("pong" will
be response to "ping")
"Note that @"http://192.168.1.2" is a URL, not a host name. Just
use the host name portion of the URL." - so i shall use
@"192.168.1.2" instead of @"http://192.168.1.2"?
Im not sure if i know how to send a predefined string ( "ping" in
this case) to my server , that why i have used
[NSInputStream *iStream = [NSString stringWithFormat:@"ping"];
If i will use
NSInputStream *iStream = nil;
then how can i asign a string("ping") to iStream .?
Thank you very much!
StaS
On 13 Jul, 2008, at 2:35 AM, Chris Hanson wrote:
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