📄 client.java
字号:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;
public class Client {
/**
* @param args
*/
private String ip = null;
private int portNum;
// client socket
private Socket s = null;
// client io communication with server
private DataInputStream dis = null;
private DataOutputStream dos = null;
// console input io stream
private BufferedReader sin = null;
private String input = null;
// data from server
private String clientReadData = null;
// shared secret key
private String sharedKey = null;
// for getting stuff from file, like pw hash value, secret key
private Properties pro = null;
// user name
private String userName = null;
// bye
private static final String BYE_BYE = "bye";
/*
* constructor
*/
public Client(String ip, int portNum) {
this.ip = ip;
this.portNum = portNum;
this.sin = new BufferedReader(new InputStreamReader(System.in));
pro = new Properties();
desApp = new DesApp();
}
/*
* initialize client socket with the assigned ip and associated port number,
* also the io stream between the server and itself
*/
private void intialize() {
try {
s = new Socket(ip, portNum);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* client write info to client
*/
private void clientWrite(String info) {
try {
dos.writeUTF(info);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* console promote input
*/
private String consoleInput() {
try {
input = sin.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return input;
}
/*
* read data from server
*/
private String clientRead() {
try {
clientReadData = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
}
return clientReadData;
}
/*
* get the user's shared key
*/
private String getSharedKey(String fileName, String userName) {
try {
pro.load(this.getClass().getResourceAsStream(fileName));
} catch (IOException e) {
e.printStackTrace();
}
sharedKey = pro.getProperty(userName);
return sharedKey;
// return pwHashInServer;
}
/*
* exit and close socket and all the io stream
*/
private void exit() {
try {
s.close();
dis.close();
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
}
// control while loop
private boolean go = true;
/*
* ifGo
*/
private void setGo(boolean ifGo) {
this.go = ifGo;
}
// encrypt msg from client
private String beEncMsgC = null;
// dencrypted msg by client
private String beDencMsgC = null;
// desApp
DesApp desApp = null;
//pw
private String passWord = null;
public void goClient() {
this.intialize();
/*
* promote username and pw input
*/
System.out.print("User name:");
userName = this.consoleInput();
this.clientWrite(userName);
InputMasking masking = new InputMasking();
String password = null;
try {
passWord = masking.getPassword("password: ");
} catch (IOException ex) {
ex.printStackTrace();
}
/*
* compute pw hash value
*/
this.clientWrite(String.valueOf(passWord.hashCode()));
beEncMsgC = this.clientRead();
// get the shared secret key
desApp.getKey(this.getSharedKey(userName + ".key", userName));
beDencMsgC = desApp.getDesString(beEncMsgC);
/*
* if access denied client exit
*/
if (this.beDencMsgC.equals("Access denied")) {
System.out.println(userName + " access has been denied");
this.exit();
} else if (this.beDencMsgC.equals("welcome " + userName + "!")) {
System.out.println(this.beDencMsgC);
// authentication successful, then start communicating with server
while (go) {
System.out.print(userName + " says:");
this.beEncMsgC = desApp.getEncString(this.consoleInput());
this.clientWrite(beEncMsgC);
// if client say "bye", client exit
if (desApp.getDesString(beEncMsgC).equals(BYE_BYE)) {
this.setGo(false);
System.out.println(userName + " disconnects with server");
break;
}
this.beDencMsgC = desApp.getDesString(this.clientRead());
System.out.println("Feedback from Server: " + this.beDencMsgC);
}
// jump out while loop, client disconnect with server finally
this.exit();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -