Java-实验六
实验六
1、第一题第一版(不成熟,第一次试探性进攻,走的java核心技术卷二的例题的样式)
代码
package Ex6.Secondquestion;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Scanner;
/**
* @author 15328
*/
public class Main {
public static void main(String[] args) throws IOException {
try(ServerSocket s = new ServerSocket(8189)){
try(Socket incoming = s.accept()){
InputStream inputStream = incoming.getInputStream();
OutputStream outputStream = incoming.getOutputStream();
Scanner in = new Scanner(inputStream,"UTF-8");
PrintWriter out = new PrintWriter(outputStream,true);
System.out.println("incoming.Localport: "+incoming.getLocalPort() + " incoming.Address: " + incoming.getInetAddress());
out.println("s.port: "+s.getLocalPort() + " s.Address: " + s.getInetAddress());
String str = "Verifying Server!";
out.println("Consume: " + str);
System.out.println("Server: " + str);
int count = 0;
while(count < 3) {
out.println("Input Password:");
String line = in.nextLine();
System.out.println("the test_password :" + line);
if(Objects.equals(line,"195779")){
String s3 = "Registration Successful!";
System.out.println("Server: " + s3);
out.println("Consumer: "+ s3);
break;
}
else{
String s2 = "PassWord Wrong!";
System.out.println("Server: "+ s2);
out.println("Consumer: " + s2 );
}
count++;
}
if(count >= 3){
String s1 = "Illegal User!";
System.out.println("Server: " + s1);
out.println("Consumer: "+ s1);
incoming.close();
s.close();
}
}
}
}
}
使用telnet
1、第一题最终版(暂定)
服务器端类
package Ex6.Secondquestion;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Objects;
import java.util.Scanner;
/**
* @author 15328
*/
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8189);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
PrintWriter out = new PrintWriter(outputStream,true);
out.println("Verifying Server!");
System.out.println("ready to receieve the password from the Client : ");
int count = 0;
while(true) {
count++;
String x = in.readLine();
if(count > 3){
out.println("Illegal User!");
System.out.println("count > 3 : Illegal User!");
break;
}
if(Objects.equals(x, "195779")){
out.println("Password Right! AND Registration Successful!");
System.out.println("Password Right! AND Registration Successful!");
break;
}
else{
out.println("Password Wrong!");
System.out.println("Password Wrong!");
}
}
in.close();
out.close();
socket.close();
serverSocket.close();
}
}
客户端类
package Ex6.Secondquestion;
import java.io.*;
import java.net.Socket;
import java.util.Objects;
import java.util.Scanner;
public class SocketOf{
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost",8189);
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
PrintWriter out = new PrintWriter(outputStream,true);
String x = in.readLine();
Scanner input = new Scanner(System.in);
if(Objects.equals(x, "Verifying Server!")) {
System.out.println("has received the first message : " + x);
while(true) {
//输入口令
System.out.println("Password:");
x = input.nextLine();
out.println(x);
//接收反馈
x = in.readLine();
if(Objects.equals(x, "Password Wrong!")){
System.out.println("has received :Password Wrong!");
}
else{
if(Objects.equals(x, "Password Right! AND Registration Successful!")){
System.out.println("has received : Password Right! AND Registration Successful!");
break;
}
if(Objects.equals(x, "Illegal User!")){
System.out.println("has received : Illegal User!");
break;
}
}
}
}
else{
System.out.println("Server Wrong!");
}
in.close();
out.close();
socket.close();
}
}
运行截图:
三次输入口令都错误的时候:
客户端:
服务器端:
口令正确的时候:
客户端:
服务器端:
第二题第一问,单个服务器端与单个客户端聊天通信(不太聪明的样子)
互相通话,客户端要求结束通话
客户端窗口
服务端窗口
互相通话,服务端要求结束通话
服务端窗口
客户端窗口
代码
package Ex6.Firstquestion;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Objects;
import java.util.Scanner;
/**
* @author 15328
*/
public class Main {
public static void main(String[] args) {
}
}
class Server{
public static void main(String[] args) throws IOException, InterruptedException {
ServerSocket serverSocket = new ServerSocket(8189);
Socket socket = serverSocket.accept();
//获取输入输出流
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
PrintWriter out = new PrintWriter(outputStream,true);
//输出流,文本格式输出
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
//输入流,处理文本输入
Scanner input = new Scanner(System.in);
//确定连接的通信
out.println("Server is alive !");
String x = in.readLine();
String y;
boolean symbol = false;
if(Objects.equals(x, "SocketOfClient is alive !")){
System.out.println(x);
symbol = true;
//能够互相发送消息
}
while(symbol) {
System.out.print("Me: ");
y=input.nextLine();
out.println(y);
if(Objects.equals(x, "bye")){
socket.close();
}
if(socket.isClosed()){
System.out.println("客户端要求关闭,已经获得服务端许可,客户端已经关闭,服务端也即将关闭");
break;
}
if(Objects.equals(x, "OK")){
System.out.println("服务端要求关闭,已经获得客户端许可,服务端和客户端都将关闭");
break;
}
x = in.readLine();
if(Objects.equals(x, "bye")) {
System.out.println("has received: " + x);
out.println("OK");
//告诉客户端允许关闭
}
System.out.print("Client: *");
System.out.println(x + "*");
}
in.close();
out.close();
socket.close();
serverSocket.close();
}
}
class SocketOf{
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 8189);
//获取输入输出流
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
PrintWriter out = new PrintWriter(outputStream, true);
//输出流,文本格式输出
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
//输入流,处理文本输入
String x = in.readLine();
String y = "";
boolean symbol = false;
Scanner input = new Scanner(System.in);
if(Objects.equals(x, "Server is alive !")) {
out.println("SocketOfClient is alive !");
System.out.println(x);
symbol = true;
}
while(symbol) {
System.out.print("Me: ");
y=input.nextLine();
out.println(y);
if(Objects.equals(x, "bye")) {
break;
}
x = in.readLine();
if(Objects.equals(x, "bye")) {
System.out.println("has received: " + x);
out.println("OK");
}
System.out.print("Server: *");
System.out.println(x + "*");
if(Objects.equals(x, "OK")) {
break;
}
}
in.close();
out.close();
socket.close();
}
}