我試圖連接到一個telnet服務器(如果它很重要,運行NetLinx的AMX,如果它很重要),併發送一些命令,就好像我打開終端,並做了「telnet(地址) 「並開始輸入命令。我可以收到郵件,但使用此代碼,我從一個教程發現在http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server不能給他們:試圖發送telnet命令到服務器
- (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"
但是當我鍵入在終端命令,服務器沒有任何響應和它的工作,因爲它應該。
telnet協議在http://tools.ietf.org/html/rfc854中定義,如果有幫助的話。 –