以下是我的服务器和客户端身份验证程序。我想知道我是否以正确的方式进行操作,我希望消息按顺序发送和接收,这意味着服务器仅在接收到来自客户端的输入后才发送数据,在收到来自服务器的响应后,同样的事情会发送到客户端然后回复。
Client class:
try {
// Set up the socket
connection = new Socket("127.0.0.1", 55555);
PrintWriter out = new PrintWriter(connection.getOutputStream(),
true);
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
// Client --> Server 'A'
BigInteger A = client.Step1(username, password);
out.println(A);
// Client received s , B
byte[] s = new BigInteger(in.readLine()).toByteArray();
BigInteger B = new BigInteger(in.readLine());
// Client --> Server 'A'
BigInteger M1 = client.Step2(new BigInteger(s), B);
out.println(M1);
// Client received M2
BigInteger M2 = new BigInteger(in.readLine());
boolean clientVerify = client.clientVerify(M2);
if (clientVerify) {
System.out.println("Client OK");
} else {
System.out.println("NOT OK");
}
out.close();
in.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
Server class:
try {
socServer = new ServerSocket(55555);
System.out.println("Waiting Client....");
connection = socServer.accept();
System.out.println("Client Connected");
PrintWriter out = new PrintWriter(connection.getOutputStream(),
true);
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
// Server received A
BigInteger A = new BigInteger(in.readLine());
// Server --> Client 's' , 'B'
out.println(new BigInteger(s));
BigInteger B = server.step1(username, new BigInteger(s), v);
out.println(B);
server.step2(A);
// Server received 'M1'
BigInteger M1 = new BigInteger(in.readLine());
boolean serverVerify = server.serverVerify(M1);
if (serverVerify) {
// Server --> Client 'M2'
BigInteger M2 = server.step2(A, M1);
out.println(M2);
System.out.println("Server OK");
} else {
System.out.println("NOT OK");
connection.close();
}
out.close();
in.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
我可以通过 Wireshark 捕获数据吗?