首页 文章

尝试将telnet命令发送到服务器

提问于
浏览
2

我'm trying to connect to a telnet server (running NetLinx by AMX, in case it matters) and send it a few commands as if I opened Terminal and did 2993705 and started typing commands. I can receive messages but can' t使用我在http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server教程中找到的代码发送它们:

- (void)sendMessage: (NSString*) message{ //called when the user interacts with UISwitches, is supposed to send message to server
NSData *data = [[NSData alloc] initWithData:[message dataUsingEncoding:NSASCIIStringEncoding]]; //is ASCIIStringEncoding what I want?
[outputStream write:[data bytes] maxLength:[data length]];
NSLog(@"Sent message to server: %@", message);
}

- (void)initNetworkCommunication { //called in viewDidLoad of the view controller
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.1.90", 23, &readStream, &writeStream); //192.168.1.90 is the server address, 23 is standard telnet port
    inputStream = (__bridge NSInputStream *)readStream;
    outputStream = (__bridge NSOutputStream *)writeStream;

    [inputStream setDelegate:self];
    [outputStream setDelegate:self];

    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream open];
    [outputStream open];
}

它似乎向服务器发送某种消息,但可能格式错误 . 我的猜测是这是一个字符串格式问题 . ASCII是我想用于telnet的吗?如果我收到它在发送命令时打印的消息,并在telnet运行时粘贴到终端,服务器正常接收并处理命令 .

这是我要发送的示例命令:SEND_STRING dvJAND,“'#AUX2 = 0',$ 0D”

另一个谜团是,阅读输入流和打印,我发现以上内容时:

2013-08-20 00:20:23.791 [7548:907] server said: S
2013-08-20 00:20:23.793 [7548:907] server said: END_STRING dvJAND,"'#AUX
2013-08-20 00:20:23.795 [7548:907] server said: 2=0',$0D"

但是当我在终端中输入该命令时,服务器根本没有响应并按预期工作 .

1 回答

  • 1

    发现我唯一错误的是没有用\ r结束我的消息!我读到你需要以\ n结束,但\ r \ n是为我做的 .

相关问题