编程语言
首页 > 编程语言> > Java-实验六

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,incoming,java,System,实验,println,import,out
来源: https://blog.csdn.net/caoxiaobao1207/article/details/120384182