📄 clientdemo.java
字号:
// 例 8.4.4 ClientDemo.java
import java.io.*;
import java.net.*;
class ClientDemo
{
public static void main (String [] args)
{
String host = "localhost";
//也可以通过命令行参数来获取服务器的地址
if (args.length == 1) host=args[0];
BufferedReader br = null;
PrintWriter pw = null;
Socket s = null;
System.out.println("Connecting to Server...");
try
{
s = new Socket (host, 10000);
System.out.println("Connection success");
System.out.println("you can send: DATE(date) or TIME(time) or BYE(bye)");
InputStreamReader isr;
isr = new InputStreamReader (s.getInputStream());
br = new BufferedReader (isr);
pw = new PrintWriter (s.getOutputStream(), true);
BufferedReader bu = new BufferedReader(
new InputStreamReader(System.in));
String str;
do
{
str = bu.readLine().toUpperCase();
pw.println(str);
if (str.startsWith("TIME")||str.startsWith("DATE"))
{
System.out.println(br.readLine());
}
}while(!str.equals("BYE"));
}catch (IOException e){
System.out.println("Connection failed");
System.out.println (e.toString ());
}finally{
try
{
if (br != null) br.close ();
if (pw != null) pw.close ();
if (s != null) s.close ();
}catch (IOException e){ e.printStackTrace();}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -