如何用java实现telnet的登录及实现命令

如题所述

第1个回答  2015-01-29
参考一下代码:

用telnet是这样:telnet time-A.timefreq.bldrdoc.gov 13

用socket是这样:

1. import java.io.*;
2. import java.net.*;
3.
4. /**
5. This program makes a socket connection to the atomic clock
6. in Boulder, Colorado, and prints the time that the
7. server sends.
8. */
9. public class SocketTest
10. {
11. public static void main(String[] args)
12. {
13. try
14. {
15. Socket s = new Socket("time-A.timefreq.bldrdoc.gov",
16. 13);
17.
18. BufferedReader in = new BufferedReader
19. (new InputStreamReader(s.getInputStream()));
20. boolean more = true;
21. while (more)
22. {
23. String line = in.readLine();
24. if (line == null)
25. more = false;
26. else
27. System.out.println(line);
28. }
29.
30. }
31. catch (IOException e)
32. {
33. e.printStackTrace();
34. }
35. }
36. }本回答被提问者和网友采纳
相似回答