0
嗨,大家好我已經在java中編寫了一個簡單的遠程登錄套接字客戶端,我試圖在windows 7專業版上連接到本地主機上的遠程登錄服務。該代碼執行正常,但無法打印輸出流和輸入流,代碼將刪除以下例外: 嘗試連接到端口1024上的主機localhost 012,無法獲得連接的I/O:本地主機java遠程登錄套接字客戶端
有什麼,我失蹤?代碼如下 在此先感謝。
import java.io.*;
import java.net.*;
import java.util.*;
public class telnetClients {
public static void main(String[] args) throws IOException {
String telnetServer = new String ("localhost");
int port = 1024;
if (args.length > 0)
telnetServer = args[0];
System.out.println ("Attemping to connect to host " +
telnetServer + " on port "
+ port);
Socket ClientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
ClientSocket = new Socket(telnetServer, port);
ClientSocket.setSoTimeout(20000);
// PrintStream com = new PrintStream(ClientSocket.getOutputStream());
// System.out.println(com);
// BufferedReader inCom = new BufferedReader(new InputStreamReader (ClientSocket.getInputStream()));
out = new PrintWriter(ClientSocket.getOutputStream(), true);
System.out.println(out);
in = new BufferedReader(new InputStreamReader(
ClientSocket.getInputStream()));
String command = in.readLine();
if(in != null);
System.out.println(in);
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + telnetServer);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: " + telnetServer);
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
System.out.println ("Type Message (\"bye\" to quit)");
while ((userInput = stdIn.readLine()) != null)
{
out.println(userInput);
// end loop
if (userInput.equals("bye"))
break;
System.out.println("echo: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
ClientSocket.close();
}
}
也許不是忽略例外的是什麼做到這一點,你應該打印('e.printStackTrace ()')。它會告訴你什麼是錯的。 –
您的計算機上是否啓用了該服務? –
「無法獲得連接的I/O」不是一個例外。這只是當你發現異常時你自己打印出來的東西。這是無用的。你應該打印的是*異常*本身。然後你就會知道發生了什麼問題。 – EJP