Java教程

Java-实验六

本文主要是介绍Java-实验六,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

实验六

      • 1、第一版(不成熟,第一次试探性进攻,走的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
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

这篇关于Java-实验六的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!