📄 client.java
字号:
import java.io.*;
import java.net.*;
public class Client{
public static void main(String[] args) {
boolean goon = true;
HERE: while (goon) {
System.out
.println("Please give me the server name you want to login in: ");
BufferedReader br = null;
String hostName = null;
String accountNumber = null;
String clientAmount = null;
Socket client = null;
DataInputStream clientIn = null;
DataOutputStream clientOut = null;
// ask the client to input the name of the host
br = new BufferedReader(new InputStreamReader(System.in));
try {
hostName = br.readLine();
} catch (IOException e1) {
e1.printStackTrace();
}
// connect to the host
try {
client = new Socket(hostName, 111);
} catch (UnknownHostException e) {
System.err.println("Host Unknown!");
System.exit(1);
} catch (IOException e) {
System.err.println("Host Unknown!");
System.exit(1);
}
try {
clientIn = new DataInputStream(client.getInputStream());
clientOut = new DataOutputStream(client.getOutputStream());
clientOut.writeUTF("Hello!");
clientOut.flush();
System.out.println(clientIn.readUTF());
// give the account number to the server
br = new BufferedReader(new InputStreamReader(System.in));
accountNumber = br.readLine();
clientOut.writeUTF(accountNumber);
clientOut.flush();
// deal with login incorrect!!!
if (clientIn.readBoolean()) {
System.out
.println("Login incorrect or time gap between two transaction is less than 1 minute!!!"
+ "\nPlease try again!");
continue HERE;
}
// when the time gap is too short, less than 1 minute
if (clientIn.readBoolean()) {
System.out
.println("Login incorrect or time gap between two transaction is less than 1 minute!!!"
+ "\nPlease try again!");
continue HERE;
}
System.out.println(clientIn.readUTF());
System.out.println(clientIn.readUTF());
// give the server the amount of the money, deposite or withdraw
br = new BufferedReader(new InputStreamReader(System.in));
clientAmount = br.readLine();
clientOut.writeUTF(clientAmount);
clientOut.flush();
System.out.println(clientIn.readUTF());
System.out.println(clientIn.readUTF());
// ask the client if he wants to make another transaction
int again;
br = new BufferedReader(new InputStreamReader(System.in));
again = Integer.valueOf(br.readLine());
clientOut.write(again);
clientOut.flush();
if (again == 1) {
goon = true;
continue HERE; // loop again if he wants to make another transaction
} else {
goon = false; // if not, terminate
clientIn.close();
clientOut.close();
br.close();
client.close();
}
} catch (IOException e) {
System.err.println("Connection Lost, Please Try again!");
System.exit(1);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -