Talk to a device using TCP / IP

I am trying to open a TCP stream to talk to a device with a cocoa application. I searched the Internet and found that there are some possibilities to do this, but I am a bit stuck.

I decided to use the NSStream method (because it refers to cocoa -touch, it will be useful if I want to port my application to the iPhone that I assume), here is my code:

@implementation AppDelegate

- (IBAction)connect:(id)sender {

    [NSStream getStreamsToHost:"192.168.1.4" port:23 inputStream:&inputStream outputStream:&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;
    }

}

So, when I click on the connect button, it should open a stream on my host and make my 2 objects (input stream and output stream)

The first step I would like to achieve is to have an input stream in NSTextView and know if the host has been reached or not ... but I'm still stuck :(

- , ! Stack Overflow, -, !:)


, , , :) telnet. "" texview. , , " ":

- (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]];

}

, - "user + ok", ... 2 : - FTP- SSH-, "" . telnet 90% : "ÿýÿýÿûÿû", ?

  • , , unrocognized handleEvent...
+3
1
0

All Articles