Java网络编程socket,为何我的服务器打不开?-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

Java网络编程socket,为何我的服务器打不开?

2016-06-08 17:48:36 2480 1

初学Java网络编程socket,写了个服务器,但是打不开,求指导。代码如下


public class Test2_2Server extends Application{
    //创建显示文本域
    private TextArea area = new TextArea();

    public void start(Stage primaryStage){
        area.setEditable(false);

        Scene scene = new Scene(new ScrollPane(area), 450,300);
        primaryStage.setTitle("Test2_2Server");
        primaryStage.setScene(scene);
        primaryStage.show();

        try {
            //创建一个服务器套接字
            ServerSocket serverSocket = new ServerSocket(8800);
            Platform.runLater(() ->
                area.appendText("Server started at " + new Date() + '\n'));

            int clientNo = 1;
            while (true){
                //监听
                Socket socket = serverSocket.accept();

                area.appendText("Starting thread for client " + clientNo + " at " + new Date() + '\n');

                //获取客户端地址
                InetAddress inetAddress = socket.getInetAddress();
                area.appendText("Client " + clientNo + "'s host name is " + inetAddress.getHostName() + '\n');
                area.appendText("Client " + clientNo + "'s IP Address is " + inetAddress.getHostAddress() + '\n');

                //创建进程
                HandleAClient task = new HandleAClient(socket);

                //开始运行
                new Thread(task).start();

                //客户端数加一
                clientNo++;
            }
        }
        catch(IOException ex) {
            System.err.println(ex);
        }


    }

    public static void main(String[] args) {
        launch(args);
    }

    //服务器端的任务类
    class HandleAClient implements Runnable{
        private Socket socket;

        public HandleAClient(Socket socket1){
            this.socket = socket1;
        }

        public void run(){
            try {
                //创建输入输出流
                DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
                DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());

                while (true){
                    //获取客户端数据
                    double annualInterestRate = inputFromClient.readDouble();
                    int numberOfYears = inputFromClient.readInt();
                    double loanAmount = inputFromClient.readDouble();

                    //计算偿还额
                    double totalPayment = loanAmount * Math.pow(1 + annualInterestRate * 0.01, numberOfYears);
                    double monthlyPayment = totalPayment / numberOfYears / 12;
                    //将数据传送至客户端
                    outputToClient.writeDouble(monthlyPayment);
                    outputToClient.writeDouble(totalPayment);
                    //输出
                    Platform.runLater(() ->{
                        area.appendText("Annual Interest Rate:" + annualInterestRate + '\n');
                        area.appendText("Number Of Year:" + numberOfYears + '\n');
                        area.appendText("Loan Amount:" + loanAmount + '\n');

                        area.appendText("Monthly Payment:" + monthlyPayment + '\n');
                        area.appendText("Total Payment:" + totalPayment + '\n');
                    });
                }
            }
            catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
} 
取消 提交回答
全部回答(1)
  • 蛮大人123
    2019-07-17 19:32:28
    public class net extends Application{
        //创建显示文本域
        private TextArea area = new TextArea();
        public void start(Stage primaryStage){
            Scene scene = new Scene(new ScrollPane(area), 450,300);
            primaryStage.setTitle("Test2_2Server");
            primaryStage.setScene(scene);
            primaryStage.show();
        //此处做修改,为服务器分配线程
            new Thread(() ->{
                try {
                    //创建一个服务器套接字
                    ServerSocket serverSocket = new ServerSocket(8000);
                    area.appendText("Server started at " + new Date() + '\n');
                    while (true){
                        //监听
                        Socket socket = serverSocket.accept();
                        //创建进程
                        HandleAClient task = new HandleAClient(socket);
                        //开始运行
                        new Thread(task).start();
                    }
                }
                catch(IOException ex) {
                    System.err.println(ex);
                }
            }).start();
        }
        public static void main(String[] args) {
            launch(args);
        }
        //服务器端的任务类
        class HandleAClient implements Runnable{
            private Socket socket;
            public HandleAClient(Socket socket1){
                this.socket = socket1;
            }
            public void run(){
                try {
                    //创建输入输出流
                    DataInputStream input = new DataInputStream(socket.getInputStream());
                    DataOutputStream output = new DataOutputStream(socket.getOutputStream());
                    while (true){
                        //获取客户端数据
                        double annualInterestRate = input.readDouble();
                        int numberOfYears = input.readInt();
                        double loanAmount = input.readDouble();
                        //计算偿还额
                        double totalPayment = loanAmount * Math.pow(1 + annualInterestRate * 0.01, numberOfYears);
                        double monthlyPayment = totalPayment / numberOfYears / 12;
                        //将数据传送至客户端
                        output.writeDouble(monthlyPayment);
                        output.writeDouble(totalPayment);
                        //输出
                        Platform.runLater(() ->{
                            area.appendText("Annual Interest Rate:" + annualInterestRate + '\n');
                            area.appendText("Number Of Year:" + numberOfYears + '\n');
                            area.appendText("Loan Amount:" + loanAmount + '\n');
                            area.appendText("Monthly Payment:" + monthlyPayment + '\n');
                            area.appendText("Total Payment:" + totalPayment + '\n');
                        });
                    }
                }
                catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
    0 0
相关问答

1

回答

我java服务器开发曾经被一个android面试过。 热? 400 报错

2020-05-31 00:22:57 343浏览量 回答数 1

0

回答

请问java性能问题:如何优化向HashMap插入元素的速度?

2021-11-12 17:18:18 140浏览量 回答数 0

0

回答

请问java中使用BigDecimal计算操作性能最优的做法是什么?

2021-11-07 15:42:19 213浏览量 回答数 0

1

回答

java性能问题:有没有比较字符串是否相等的最快方法?

2021-11-06 16:21:40 171浏览量 回答数 1

0

回答

您好java性能问题:有没有比较字符串是否相等的最快方法?

2021-11-07 00:39:14 147浏览量 回答数 0

1

回答

java性能问题:如何优化向HashMap插入元素的速度?

2021-11-06 15:41:34 218浏览量 回答数 1

1

回答

java中常用的性能调优诊断工具都有哪些,请帮忙提供一下?

2021-11-06 10:24:39 163浏览量 回答数 1

0

回答

请问java中常用的性能调优诊断工具都有哪些,请帮忙提供一下?

2021-11-06 22:35:44 90浏览量 回答数 0

1

回答

java中使用BigDecimal计算操作性能最优的做法是什么?

2021-11-06 16:35:15 230浏览量 回答数 1

1

回答

java中ArrayBlockingQueue与DoubleBufferedQueue哪个性能更高?

2021-11-06 21:47:49 210浏览量 回答数 1
+关注
蛮大人123
我说我不帅他们就打我,还说我虚伪
0
文章
7733
问答
问答排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载