?? server.java
字號:
import java.io.DataInputStream;import java.io.PrintStream;import java.net.ServerSocket;/* * To change this template, choose Tools | Templates * and open the template in the editor. */import java.net.Socket;/** * * @author root */public class Server { public static void main(String[] args) throws Exception { new Listener(); }}interface Intf{ public String withdraw(int amt); public String deposit(int amt); public String balance();}class Listener extends Thread implements Intf{ public Bank B; public Listener() { B=new Bank(); B.login(12345,(short)5555); start(); } public String withdraw(int amt) { String s=""; if(B.lock){ while(B.lock);} B.lock=true; s=B.withdraw(amt); B.lock=false; return s; } public String balance() { return B.getBal(); } public String deposit(int amt) { String s=""; if(B.lock){ while(B.lock);} B.lock=true; s=B.deposit(amt); B.lock=false; return s; } public void run() { try{ ServerSocket ss=new ServerSocket(5555); while(true){ new Reader(this,ss.accept()); } }catch(Exception e){} }}class Reader extends Thread{ Socket S; Intf N; public Reader(Intf n,Socket s) { S=s; N=n; start(); } public void run() { try{ PrintStream out=new PrintStream(S.getOutputStream()); DataInputStream in=new DataInputStream(S.getInputStream()); while(true){ int opt=Integer.parseInt(in.readLine()); if(opt==1) { int amt=Integer.parseInt(in.readLine()); out.println(N.deposit(amt)); } else if(opt==2) { int amt=Integer.parseInt(in.readLine()); out.println(N.withdraw(amt)); } else if(opt==3) { out.println(N.balance()); } else break; } in.close(); out.close(); }catch(Exception e){} }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -