1

I've made a NSStream socket to connect to a telnet server. Actually, it can connect fine to the server; I got an inputStream with the "first words" of the server, but I don't understand it. I'm looking for some explanations about the telnet IAC commands.

Here is my code to receive from the server:

 case NSStreamEventHasBytesAvailable:

            if (theStream == inputStream) {

                uint8_t buffer[1024];
                int len;

                while ([inputStream hasBytesAvailable]) {
                    len = [inputStream read:buffer maxLength:sizeof(buffer)];
                    if (len > 0) {

                        NSString * serverSaid = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

                        if (nil != serverSaid) {
                            NSLog(@"The server said: %@", serverSaid);
                            [connectLog insertText:serverSaid];
                            [connectLog insertText:@"\r"];
                        }
                    }
                }
            }

            break;

It is based on the EventHasBytesAvailable. It is working fine (got the hello from server with the login prompt).

Now, to send to the server, I do this:

NSString * theMsg  = [NSString stringWithFormat:@"root"];
NSData * msgToSend = [[NSData alloc] initWithData:[theMsg dataUsingEncoding:NSUTF8StringEncoding]];
[outputStream write:[msgToSend bytes] maxLength:[msgToSend length]];

I scripted the output on a button, to see what happen when I use the outputstream: EventHasBytesAvailable catch my output has input... The server is telling me what I told him!

Can someone explain to me the IAC commands and/or how to to proceed to login on the server and send commands?

Gareth McCaughan
  • 19,888
  • 1
  • 41
  • 62
Cindy Broutin
  • 187
  • 1
  • 3
  • 10
  • Could you try to clarify the paragraph that begins "I scripted the output on a button"? I'm having trouble working out what it means. (Does it mean: what you sent to the server is being received back by your client in its `EventHasBytesAvailable` handler, because the server is sending back a copy of everything you send to it?) – Gareth McCaughan May 01 '12 at 00:10
  • I scripted the data which I want to send to the server on a button. When I press it, the EventHasBytesAvalaible which catch what the server say sends me a copy of what I sent. This is why I'm sure that the server receives my data. Now I have to send commands which the server understand (user/pw for the login prompt) and then commands (which is the thing that is interesting me ^^)... – Cindy Broutin May 01 '12 at 00:18
  • 1
    Thanks. I don't know anything much about Telnet myself, but maybe someone will be along soon who does. The official specification of how to talk to a Telnet server is RFC 854 (http://www.apps.ietf.org/rfc/rfc854.html) together with RFC 855 (http://www.apps.ietf.org/rfc/rfc855.html), if that helps. – Gareth McCaughan May 01 '12 at 00:22
  • 2
    You can just do `NSData* msgToSend = [theMsg dataUsingEncoding:NSUTF8StringEncoding];`, you don't need to create yet another `NSData` object. If you want to keep it around just call `retain` on it. – Rob Keniger May 01 '12 at 01:25
  • I try a lot of different ways to be understood by the server, but nothing to do, I can't login and can't send commands... I don't know how to send the IAC commands... If an expert can help me It would be great! Thanks for your help! – Cindy Broutin May 01 '12 at 08:14
  • So, in the hello of the server, we can see "ÿýÿýÿûÿû" which is ASCII converted IAC from the server, which means: 255 253 1 255 253 31 255 251 1 255 251 3 IAC DO ECHO IAC DO WindowSize IAC WILL ECHO IAC WILL SH. But what am I supposed to do? I read the telnet manual but when I try to respond something, I convert it in ASCII and send it, I'm automaticaly disconnected (only if I send this type of command, otherwise I'm not disconnected). – Cindy Broutin May 01 '12 at 08:37
  • Nobody can help me? Still in the black... :( – Cindy Broutin May 02 '12 at 09:26
  • I'm also looking into this because my server is responding with chopped up copies of whatever I send it. – sudo Aug 20 '13 at 13:38

0 Answers0