多线程&socket

.编程题

1、编写一个计时器,每隔一秒钟,在控制台打印出最新时间。

知识点 Java中的线程状态

2、用Account类表示银行用户帐号,有账号(id)和余额(account)属性以及存款(deposit)取款(withdraw)和查询余额(checkBalance)操作。由于银行帐号要求安全性和可靠性较高,需要对存款与取款操作进行同步。设计该类。 

知识点:线程同步

3、编写一服务器端程序,实现读取客户端发送过来的一组整数,表现为一组数与数之间用空格隔开的字符串。对这组整数进行排序处理后,返回相应的字符串给客户端,如果数据格式不正确,则返回错误信息,以本机作为服务器。

知识点:基于TCPSocket编程

4、编写一个简单的socket通讯程序:

1)、客户机程序,从控制台输入字符串,发送到服务器端,并将服务器返回的信息显示出来。

2)、服务器端程序,从客户机接收数据并打印,同时将从标准输入获取的信息发送给客户机

3)、满足一个服务器可以服务多个客户

实现效果如图:

1:服务器端效果:

2:客户器端效果:

 

知识点:基于多线程的Socket编程

 

 

 答案:

.编程题

1、编写一个计时器,每隔一秒钟,在控制台打印出最新时间。

publicclass Main {

    publicstaticvoid main(String[] args) {

       while(true){

           SimpleDateFormat sdf =new SimpleDateFormat("HH:mm:ss");

           System.out.println(sdf.format(new Date()));

           try {

              Thread.sleep(1000);

           } catch (InterruptedException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

           }

       }

    }

}

 

2、用Account类表示银行用户帐号,有账号(id)和余额(account)属性以及存款(deposit)取款(withdraw)和查询余额(checkBalance)操作。由于银行帐号要求安全性和可靠性较高,需要对存款与取款操作进行同步。设计该类。 

publicclass Account {

    private String id;

    privatedoubleaccount;

    publicAccount(){

      

    }

    public Account(String id, double account) {

       super();

       this.id = id;

       this.account = account;

    }

    public String getId() {

       returnid;

    }

    publicvoid setId(String id) {

       this.id = id;

    }

    publicdouble getAccount() {

       returnaccount;

    }

    publicvoid setAccount(double account) {

       this.account = account;

    }

    publicsynchronizedvoid deposit(double a){

       account += a;

    }

    publicsynchronized  void withdraw(double a ){

       account -= a;

    }

    publicdouble  checkBalance(){

       returnaccount;

    }

   

}

 

3、编写一服务器端程序,实现读取客户端发送过来的一组整数,表现为一组数与数之间用空格隔开的字符串。对这组整数进行排序处理后,返回相应的字符串给客户端,如果数据格式不正确,则返回错误信息,以本机作为服务器。

 

publicclass Server {

    publicstaticvoid main(String[] args) {

       BufferedReader br = null;

        String clientStr="";

         try {

           //创建一个监听的端口

           ServerSocket ss = new ServerSocket(3838);

           Socket s = null;

          System.out.println("服务器启动...");

           //接受客户端通信请求,并建立专用通信端口

          s = ss.accept();

           System.out.println("有客户端发送请求");

           Send send = new Send(s);

           Thread t1 = new Thread(send);

           t1.start();

          while(true){

              

              //获取客户端通信的信息

               br = new BufferedReader(new InputStreamReader(s.getInputStream()));

               clientStr = br.readLine();

               System.out.println("客户端:"+clientStr);

          }

         } catch (IOException e) {

           e.printStackTrace();

         }

       }

}

publicclass Client {

   

    publicstaticvoid main(String[] args) {

       BufferedReader br = null;

        String clientStr="";

       try {

           //建立通信端口,并向服务器端发送通信请求

           Socket s = new Socket("127.0.0.1",3838);

           System.out.println("与服务器端建立连接");

           Send send = new Send(s);

           Thread t1 = new Thread(send);

           t1.start();

              while(true){

                 

                  //获取客户端通信的信息

                   br = new BufferedReader(new InputStreamReader(s.getInputStream()));

                   clientStr = br.readLine();

                  System.out.println("服务器端说:"+clientStr);

              }

          

       } catch (UnknownHostException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

 

    }

 

}

publicclass Send implements Runnable{

    Socket s ;

    Scanner in = new Scanner(System.in);

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public Send(Socket s){

       this.s = s;

    }

    publicvoid run() {

       // TODO Auto-generated method stub

       PrintWriter out = null;

       try {

           while(true){

              out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);

              String str = in.next();

                  //br.readLine();

               out.println(str);

   

           }

          

       } catch (Exception e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }finally{

           out.close();

       }

      

    }

   

}

 

4、编写一个简单的socket通讯程序:

1)、客户机程序,从控制台输入字符串,发送到服务器端,并将服务器返回的信息显示出来。

2)、服务器端程序,从客户机接收数据并打印,同时将从标准输入获取的信息发送给客户机

3)、满足一个服务器可以服务多个客户

实现效果如图:

1:服务器端效果:

2:客户器端效果:

 

publicclass Server {

    static List l = new ArrayList();

    publicstaticvoid main(String[] args) {

       try {

           ServerSocket so  =new ServerSocket(4001);

           System.out.println("服务器启动...");

           while(true){

              Socket s = so.accept();

              System.out.println(s.getInetAddress()+"发送请求");

              l.add(s);

              One o = new One();

              Thread t = new Thread(o);

              t.start();

           }

          

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

}

 

publicclass One implements Runnable {

 

    publicvoid run() {

       Socket s = Server.l.get(Server.l.size() - 1);

       String str = "";

       SendAll sendAll = new SendAll();

       Thread t1 = new Thread(sendAll);

       t1.start();

 

       try {

           while (true) {

               BufferedReader br = new BufferedReader(new InputStreamReader(s

                     .getInputStream()));

              str = br.readLine();

              System.out.println("客户端" + s.getInetAddress() + "说:" + str);

           }

       } catch (IOException e) {

           // TODO Auto-generated catch block

           System.out.println(s.getInetAddress() + "用户下线");

           Server.l.remove(s);

       }

 

    }

 

}

publicclass SendAll implements Runnable{

   

    Scanner in = new Scanner(System.in);

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

   

    publicvoid run(){

       PrintWriter out = null;

       try {

           while(true){

              String str =br.readLine();

              for(Socket s:Server.l){

              out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);

               out.println(str);

              }

           }

          

       } catch (Exception e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }finally{

           out.close();

       }

    }

}

 

publicclass Client {

   

    publicstaticvoid main(String[] args) {

       BufferedReader br = null;

        String clientStr="";

       try {

           //建立通信端口,并向服务器端发送通信请求

           Socket s = new Socket("127.0.0.1",4001);

           System.out.println("与服务器端建立连接");

           Send send = new Send(s);

           Thread t1 = new Thread(send);

           t1.start();

              while(true){

                 

                  //获取客户端通信的信息

                   br = new BufferedReader(new InputStreamReader(s.getInputStream()));

                   clientStr = br.readLine();

                  System.out.println("服务器端说:"+clientStr);

              }

          

       } catch (UnknownHostException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

 

    }

 

}

 

publicclass Send implements Runnable{

    Socket s ;

    Scanner in = new Scanner(System.in);

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public Send(Socket s){

       this.s = s;

    }

    publicvoid run() {

       // TODO Auto-generated method stub

       PrintWriter out = null;

       try {

           while(true){

              out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);

              String str =br.readLine();

               out.println(str);

   

           }

          

       } catch (Exception e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }finally{

           out.close();

       }

      

    }

   

}

 

 

 

你可能感兴趣的:(java)