Note that there are some explanatory texts on larger screens.

plurals
  1. POSpeak to a device using TCP/IP
    text
    copied!<p>I try to open a TCP stream to speak to a device with a cocoa app. I searched the web and found that there is some possibilities to do that but i'm a little bit stuck.</p> <p>I decided to use the NSStream way (because it's referenced in cocoa-touch, will be usefull if i want to port my app to iPhone i presume), so here is my code:</p> <pre><code>@implementation AppDelegate - (IBAction)connect:(id)sender { [NSStream getStreamsToHost:"192.168.1.4" port:23 inputStream:&amp;inputStream outputStream:&amp;outputStream]; [inputStream setDelegate:self]; [outputStream setDelegate:self]; [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [inputStream open]; [outputStream open]; } // Both streams call this when events happen - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode { if (aStream == inputStream) { [self handleInputStreamEvent:eventCode]; } else if (aStream == outputStream) { [self handleOutputStreamEvent:eventCode]; } } - (void)handleInputStreamEvent:(NSStreamEvent)eventCode { switch (eventCode) { case NSStreamEventHasBytesAvailable:[self readBytes]; break; case NSStreamEventOpenCompleted: // Do Something break; default: case NSStreamEventErrorOccurred: NSLog(@"An error occurred on the input stream."); break; } } </code></pre> <p>So, when I click on my connect button, it is supposed to open the stream to my host and make my 2 objects (inputstream and outputstream)</p> <p>The first step I would like to reach is to have the inputStream in a NSTextView and know if the host has been reached or not... but i'm still stuck :(</p> <p>If someone can light my way, it would be nice! I'm new on Stack Overflow and I'll be glad to help the community on somethings that i know much! :)</p> <hr> <p>I updated my code and it seems that the light is coming, slowly but it's coming :) I made a stream to a telnet server. I got the "hello" in a texview. Now, I would like to send the user &amp; password to be able to send commands to the server, but here is my "send user &amp; pass" button code:</p> <pre><code>- (IBAction)sendusername:(id)sender { NSString * usernameMsg = [NSString stringWithFormat:@"user @", [usernameField stringValue]]; NSData * usertosend = [[NSData alloc] initWithData:[usernameMsg dataUsingEncoding:NSUTF8StringEncoding]]; [outputStream write:[usertosend bytes] maxLength:[usertosend length]]; } </code></pre> <p>Follow my searchs, the server should respond me a thing like "user +ok" but nothing... 2 stranges things: - If I open the socket to a FTP server of SSH server, I've always the "hello" response without problem. But in telnet, 90% of connections respond me a strange hello like this: "ÿýÿýÿûÿû", why?</p> <ul> <li>When I send the user, nothing happen, only an unrocognized event from the handleEvent...</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload