参考一下代码:用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. }
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. }
大侠们快帮帮我啊!
2. import java.awt.event.*;
3. import java.util.*;
4. import java.net.*;
5. import java.io.*;
6. import javax.swing.*;
7.
8. /**
9. This program shows how to use sockets to send plain text
10. mail messages.
11. */
12. public class MailTest
13. {
14. public static void main(String[] args)
15. {
16. JFrame frame = new MailTestFrame();
17. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
18. frame.show();
19. }
20. }
21.
22. /**
23. The frame for the mail GUI.
24. */
25. class MailTestFrame extends JFrame
26. {
27. public MailTestFrame()
28. {
29. setSize(WIDTH, HEIGHT);
30. setTitle("MailTest");
31.
32. getContentPane().setLayout(new GridBagLayout());
33.
34. GridBagConstraints gbc = new GridBagConstraints();
35. gbc.fill = GridBagConstraints.HORIZONTAL;
36. gbc.weightx = 0;
37. gbc.weighty = 0;
38.
39. gbc.weightx = 0;
40. add(new JLabel("From:"), gbc, 0, 0, 1, 1);
41. gbc.weightx = 100;
42. from = new JTextField(20);
43. add(from, gbc, 1, 0, 1, 1);
44.
45. gbc.weightx = 0;
46. add(new JLabel("To:"), gbc, 0, 1, 1, 1);
47. gbc.weightx = 100;
48. to = new JTextField(20);
49. add(to, gbc, 1, 1, 1, 1);
50.
51. gbc.weightx = 0;
52. add(new JLabel("SMTP server:"), gbc, 0, 2, 1, 1);
53. gbc.weightx = 100;
54. smtpServer = new JTextField(20);
55. add(smtpServer, gbc, 1, 2, 1, 1);
56.
57. gbc.fill = GridBagConstraints.BOTH;
58. gbc.weighty = 100;
59. message = new JTextArea();
60. add(new JScrollPane(message), gbc, 0, 3, 2, 1);
61.
62. communication = new JTextArea();
63. add(new JScrollPane(communication), gbc, 0, 4, 2, 1);
64.
65. gbc.weighty = 0;
66. JButton sendButton = new JButton("Send");
67. sendButton.addActionListener(new
68. ActionListener()
69. {
70. public void actionPerformed(ActionEvent evt)
71. {
72. new
73. Thread()
74. {
75. public void run()
76. {
77. sendMail();
78. }
79. }.start();
80. }
81. });
82. JPanel buttonPanel = new JPanel();
83. buttonPanel.add(sendButton);
84. add(buttonPanel, gbc, 0, 5, 2, 1);
85. }
86.
87. /**
88. Add a component to this frame.
89. @param c the component to add
90. @param gbc the grid bag constraints
91. @param x the grid bax column
92. @param y the grid bag row
93. @param w the number of grid bag columns spanned
94. @param h the number of grid bag rows spanned
95. */
96. private void add(Component c, GridBagConstraints gbc,
97. int x, int y, int w, int h)
98. {
99. gbc.gridx = x;
100. gbc.gridy = y;
101. gbc.gridwidth = w;
102. gbc.gridheight = h;
103. getContentPane().add(c, gbc);
104. }
105.
106. /**
107. Sends the mail message that has been authored in the GUI.
108. */
109. public void sendMail()
110. {
111. try
112. {
113. Socket s = new Socket(smtpServer.getText(), 25);
114.
115. out = new PrintWriter(s.getOutputStream());
116. in = new BufferedReader(new
117. InputStreamReader(s.getInputStream()));
118.
119. String hostName
120. = InetAddress.getLocalHost().getHostName();
121.
122. receive();
123. send("HELO " + hostName);
124. receive();
125. send("MAIL FROM: <" + from.getText() +">");
126. receive();
127. send("RCPT TO: <" + to.getText() +">");
128. receive();
129. send("DATA");
130. receive();
131. StringTokenizer tokenizer = new StringTokenizer(
132. message.getText(), "\n");
133. while (tokenizer.hasMoreTokens())
134. send(tokenizer.nextToken());
135. send(".");
136. receive();
137. s.close();
138. }
139. catch (IOException exception)
140. {
141. communication.append("Error: " + exception);
142. }
143. }
144.
145. /**
146. Sends a string to the socket and echoes it in the
147. communication text area.
148. @param s the string to send.
149. */
150. public void send(String s) throws IOException
151. {
152. communication.append(s);
153. communication.append("\n");
154. out.print(s);
155. out.print("\r\n");
156. out.flush();
157. }
158.
159. /**
160. Receives a string from the socket and displays it
161. in the communication text area.
162. */
163. public void receive() throws IOException
164. {
165. String line = in.readLine();
166. if (line != null)
167. {
168. communication.append(line);
169. communication.append("\n");
170. }
171. }
172.
173. private BufferedReader in;
174. private PrintWriter out;
175. private JTextField from;
176. private JTextField to;
177. private JTextField smtpServer;
178. private JTextArea message;
179. private JTextArea communication;
180.
181. public static final int WIDTH = 300;
182. public static final int HEIGHT = 300;
183. }
//此程序用于建立以实现与指定地址的端口进行标次输入输出操作的连接
//若被连接方是Telnet端口(23号)则时行协商
//协商时,拒绝服务器端的所有请求
//使用方法(1):java Telnet 服务器地址 端口号
//使用方法(2):java Telnet 服务器地址(2)时,假设端口号为23
//运行示例: java Telnet kiku.fuis.fukui-u.ac.jp 23
//要结束请按<Ctrl> + C键//导入库
import java.net.*;
import java.io.*;//Telnet类
//Telnet类用于实现网络连接管理
//利用StreamConnector类进行线程处理
//有2种构造器,分别对应使用方法(1)和(2)
public class Telnet {
Socket serverSocket; //连接用socket
public OutputStream serverOutput; //用于网络输出的流
public BufferedInputStream serverInput; //用于网络输入的流
String host; //连接服务器的地址
int port; //连接服务器的端口号
static final int DEFAULT_TELNET_PORT = 2323; //telnet的端口号(23号) //构造器(1):指定地址和端口号时用
public Telnet(String host, int port) {
this.host = host;
this.port = port;
} //构造器(2)只指定地址时用
public Telnet(String host) {
this(host, DEFAULT_TELNET_PORT); //假定为Telnet端口
} //openConnection 方法
//由地址和端口号虽构成Socket而开成流
public void openConnection() throws IOException, UnknownHostException {
serverSocket = new Socket(host, port);
serverOutput = serverSocket.getOutputStream();
serverInput = new BufferedInputStream(serverSocket.getInputStream());
//若连接的是Telnet端口则进行协商
if (port == DEFAULT_TELNET_PORT) {
negotiation(serverInput, serverOutput);
}
} //main_proc方法
//启动进行网络处理的线程
public void main_proc() throws IOException {
try {
//生成线程用streamConnector类的对象
StreamConnector stdin_to_socket = new StreamConnector(System.in,
serverOutput);
StreamConnector socket_to_stdout = new StreamConnector(serverInput,
System.out);
//生成线程
Thread input_thread = new Thread(stdin_to_socket);
Thread output_thread = new Thread(socket_to_stdout);
//启动线程
input_thread.start();
output_thread.start();
}
catch (Exception e) {
System.err.print(e);
System.exit(1);
}
} //定义用于协商的命令
static final byte IAC = (byte) 255;
static final byte DONT = (byte) 254;
static final byte DO = (byte) 253;
static final byte WONT = (byte) 252;
static final byte WILL = (byte) 251; //negotiation 方法
//利用NVT进行协商通信
static void negotiation(
BufferedInputStream in, OutputStream out) throws IOException {
byte[] buff = new byte[3]; //接收命令的数组
while (true) {
in.(buff.length);
if (in.available() >= buff.length) {
in.read(buff);
if (buff[0] != IAC) { //协商结束
in.reset();
return;
}
else if (buff[1] == DO) { //对于DO命令......
buff[1] = WONT; //用WONT作为应答
out.write(buff);
}
}
}
} //main方法
//建立TCP连接,开始处理
public static void main(String[] arg) {
try {
Telnet t = null;
//由参数个数决定调用哪个构造器
// switch (arg.length) {
// case 1: //只指定服务器地址
// t = new Telnet(arg[0]);
// break;
// case 2: //指定地址和端口
// t = new Telnet(arg[0], Integer.parseInt(arg[1]));
// break;
// default: //使用方法不正确时
// System.out.println(
// "usage:java Telnet<host name>{<port number>}");
// return;
// }
t = new Telnet("192.168.3.73", 2323);
t.openConnection();
t.main_proc();
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}//StreamConnector类
//接收流参数以二者结合实现数据传递
//StreamConnector类是用于构造线程序的类
class StreamConnector
implements Runnable {
InputStream src = null;
OutputStream dist = null; //构造器 接收输入输出流
public StreamConnector(InputStream in, OutputStream out) {
src = in;
dist = out;
} //执行处理的函数体
//无限循环进行流的读写
public void run() {
byte[] buff = new byte[1024];
while (true) {
try {
int n = src.read(buff);
if (n > 0) {
dist.write(buff, 0, n);
}
}
catch (Exception e) {
e.printStackTrace();
System.err.print(e);
System.exit(1);
}
}
}
}
//http://community.csdn.net/Expert/topic/3875/3875101.xml?temp=.1354029
import java.io.*;
import java.lang.*;public class SampleClient1 {
public static void main(String args[]) {
String host = "192.168.3.79";
int port = 2323;
Socket socket;
String s = "get sysLocation"; \\要执行的命令(取设备agent内的节点值)
InputStream Is;
OutputStream Os;
DataInputStream DIS;
PrintStream PS;
try { socket = new Socket(host, port); System.out.println("client ok");//获得对应socket的输入/输出流
Is = socket.getInputStream();
Os = socket.getOutputStream();
//建立数据流
DIS = new DataInputStream(Is);
PS = new PrintStream(Os);
//DataInputStream in = new DataInputStream(System.in); //s = in.readLine(); //读取用户输入的字符串
PS.println(s); //将读取得字符串传给server System.out.println("please wait server's message...");
s = DIS.readLine(); //从服务器获得字符串 s = new String(s.getBytes(),"gb2312");
//s = DIS.readChar();
System.out.println("server said:" + s); //打印字符串 //关闭连接
DIS.close(); //关闭数据输入流
PS.close(); //关闭数据输出流
Is.close(); //关闭输入流
Os.close(); //关闭输出流
socket.close(); //关闭socket
}
catch (Exception e) {
System.out.println("Error:" + e);
}
}
}
改成
int l=DIS.readLine(s);
System.out.println("server said:" + s); //打印字符串或
public static String read() {
byte[] buf=new byte[256];
byte b=-1,i=0;
try{
do{
b=(byte)clientis.read();
if (b==-1)return null;
buf[i]=b;
i++;
}while(b!='\n');//要是有结束符可以这样写
}catch(Exception E){
E.printStackTrace();return null;
}
return (new String(buf,0,i));
}