📄 atmserver.java
字号:
import java.io.*;
import java.net.*;
import javagently.*;
class ATMServer {
/* A simple server program by J M Bishop December 1996
* =======================
* Java 1.1 revised January 1998
* Java 1.2 B Worrall August 2000
* Allows multiple simultaneous connections.
* Illustrates sockets and networking.
*/
static final String magicPIN = "5678";
static int port = 8190;
InetAddress serverAddress = null;
private boolean done = false;
public static void main(String[] args) {
// Set up the port address from the command line,
// or default to 8190
if (args.length > 0)
port =Integer.parseInt(args[0]);
new ATMServer (port);
}
ATMServer (int port) {
try {
serverAddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {}
// Initial printing on Server side only
// using System.out
System.out.println("****** SAVANNA BANK ********");
System.out.println("Simulate an ATM session by "
+ "telnetting in to ");
System.out.println(serverAddress.getHostName() +
" on port "+port);
System.out.println("from any number of different computers or");
System.out.println("from different active Windows.");
// Set up the server socket
try {
ServerSocket listener = new ServerSocket(port);
int c = 0;
while (!done) {
// This is where the program waits for new clients
Socket client = listener.accept( );
c ++;
System.out.println("Card inserted on " +
client.getInetAddress().getHostName());
System.out.println("Starting a new client, numbered "
+c);
new handler(client, c).start();
}
listener.close();
}
catch (IOException e) {
System.out.println("Port "+port+
" may be busy. Try another.");
}
}
void closeDown () {
done = true;
}
class handler extends Thread {
private Socket toClient;
private int id;
handler(Socket s, int i) {
// Remember the client socket number and client id number
toClient = s;
id = i;
}
public void run() {
try {
Stream conin = new Stream (toClient.getInputStream());
PrintWriter conout = new
PrintWriter(toClient.getOutputStream(),true);
conout.println( "Welcome to Savanna Bank");
for (int tries = 0; tries < 3; tries++) {
conout.println("Please type in your PIN "+
"number or type CANCEL");
String s = conin.readLine();
System.out.println("Client "+id+":"+s);
if (s.equals("SHUTDOWN")) closeDown();
else if (s.equals("CANCEL")) {
conout.println("Transactions halted. Goodbye.");
break;
}
else if (s.equals(magicPIN)) {
conout.println("Please start your transactions");
break;
}
else
conout.println("Incorrect PIN. Try again.");
}
System.out.println("Simulation complete. Thanks.");
}
catch (IOException e) {System.out.println("IO Error");}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -