echotest.java
来自「java网络编程的一个实例」· Java 代码 · 共 36 行
JAVA
36 行
//客户端程序
import java.io.*;
import java.net.*;
public class EchoTest {
public static void main(String[] args) {
Socket echoSocket = null;
DataOutputStream os = null;
DataInputStream is = null;
DataInputStream stdIn = new DataInputStream(System.in);
try { echoSocket = new Socket("xyz-server", 4444);
os = new DataOutputStream(echoSocket.getOutputStream());
is = new DataInputStream(echoSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host: xyz-server");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: myHost");
}
if (echoSocket != null && os != null && is != null) {
try {
String userInput;
while ((userInput = stdIn.readLine()) != null) {
os.writeBytes(userInput);
os.writeByte('\n');
System.out.println("echo: " + is.readLine()); }
os.close();
is.close();
echoSocket.close();
} catch (IOException e) {
System.err.println("I/O failed on the connection to: myHost");
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?