服务端测试代码

发布时间:2024年01月03日
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Servicer {

    private ServerSocket server;
    private ExecutorService exe;
    public Servicer() throws IOException{
        server = new ServerSocket(8008);
        exe = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*30);
    }
    public void service() throws IOException{
        String filename = "1.txt";
        OutputStream fileOut = new FileOutputStream(filename, true);
        PrintWriter pwFile = new PrintWriter(new OutputStreamWriter(fileOut, "GB2312"), true);
        while (true){
            try {
                Socket socket = server.accept();
                exe.execute(new Handler(socket, pwFile));
            }catch (IOException e){

            }
        }
    }
    public static void main(String args[]) throws IOException{
        new Servicer().service();
    }
    class Handler implements Runnable{
        private final Socket socket;
        private final PrintWriter pwFile;
        public Handler(Socket socket, PrintWriter pwFile){
            this.socket = socket;
            this.pwFile = pwFile;
        }

        @Override
        public void run() {
            BufferedReader br;
            PrintWriter pw = null;
            String yourNo="", yourName="", msg="";
            try {
                socket.setSoTimeout(60000);
                InputStream in = socket.getInputStream();
                br = new BufferedReader(new InputStreamReader(in, "GB2312"));
                OutputStream out = socket.getOutputStream();
                pw = new PrintWriter(new OutputStreamWriter(out, "utf-8"), true);

                pw.println("构造并发送一行包含你的学号和姓名并符合下列要求的字符串.");
                while ((msg=br.readLine()) != null){
                    if (msg.length() > 16 && msg.length() < 26){
                        int index1 = msg.indexOf("&&");
                        int index2 = msg.indexOf("##");
                        yourName = msg.substring(index1+2, index2);
                        yourNo = msg.substring(index2+2);
                        yourNo = reveral(yourNo);
                        pw.println("学号是:"+yourNo);
                        pw.println("姓名是:"+yourName);
                        pw.println("若返回的学号和姓名");
                        System.out.println(yourName);
                    }else {
                        if (msg.equals("OK")){
                            pw.println(socket.getInetAddress()+"->"+yourNo+" " + yourName+ ">>" + new Date());
                            break;
                        }
                        pw.println("fail");
                    }
                }
            }catch (IOException e){
                if (pw != null){
                    pw.println("超时");
                }
            }finally {
                try {
                    if (socket != null){
                        socket.close();
                    }
                }catch (IOException e){
                    System.out.println("close err");
                }
            }
        }
    }
    private String reveral(String yourNo){
        String newStr = "";
        int n = 10;
        while (n > -1){
            newStr = newStr + yourNo.substring(n,n+1);
            n -= 1;
        }
        return newStr;
    }
}
文章来源:https://blog.csdn.net/qq_37064135/article/details/135366273
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。