try{
// 產生一個Socket
,通過指定的端口與主機通信。
s = new Socket(args[0], port);
// 產生用于發出和接收的文本字符流
DataInputStream sin = new DataInputStream(s.getInputStream());
PrintStream sout = new DataInputStream(s.getInputStream());
// 從控制臺讀入字符流
DataInputStream in = new DataInputStream(System.in);
// 返回連接的地址和端口
ystem.out.println("Connected to" s.get.netAddress() ":"
s.getPort());
String line;
For(;;)
{
// 顯示提示符
System.out.print(" >");
System.out.flush();
// 讀入控制臺輸入的一行字符
line = in.readline();
if (line == null) break;
// 將接收的文本行送至服務器
sout.println(line);
// 從服務器接收一行字符
line = sin.readline();
// Check if connection is closed(i.e. for
EOF)
if(line == null)
{
System.out.println("Connection
closed by server.");
Break;
}
// 在控制臺上顯示接收的字符
System.out.println(line);
}
延伸閱讀
文章來源于領測軟件測試網 http://www.kjueaiud.com/