为什么服务器向客户机发送东西时会有延迟?

x7rlezfr  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(155)

我仍然在学习java中的客户机和服务器,在我的课堂上的一个作业中,我遇到了一个问题:当你发送一个文本文件时,首先服务器会立即返回结果,但是在输入另一个文本文件后,你必须再次按enter键才能得到结果。这段代码的重点是我应该得到从服务器添加到一起的两个矩阵的结果。我在这段代码中遗漏了什么可以帮助解决这个问题吗?

import java.io.*;
import java.net.*;
import javax.swing.*;

public class ServerNClient {

    private static int[][] matrix;

    public static void main(String[] args) throws Exception {

        ServerSocket ss = new ServerSocket(2300);
        Socket stk = ss.accept();

        BufferedReader br  = new BufferedReader(new InputStreamReader(stk.getInputStream()));
        PrintStream ps = new PrintStream(stk.getOutputStream());
        MatrixThread mt = new MatrixThread();

        String message;
        String stringMatrix;
        System.out.println("Client succesfully connected");

        do {
            message = br.readLine();
            ps.println(message);
            if (message.endsWith(".txt")) {
                try {
                    matrix = mt.textReader(message);
                    int number = mt.getMatrixRows();
                    stringMatrix = stringAdder(matrix, number);
                    ps.println(stringMatrix);
                } catch (Exception e) {
                    System.out.println("Error: Something went wrong while trying to read the file. Please try again.");
                    ps.println("");
                }       
            } 
        } while (!message.equals("end"));
        System.out.println("Server shutting down...");
    }

    private static String stringAdder(int[][] matrix, int number) {
        String stringMatrix = "";

        stringMatrix += (number);
        stringMatrix += "\n";
        for (int i = 0; i < matrix.length; i++) {
            for (int k = 0; k < matrix[i].length; k++) {
                stringMatrix += (Integer.toString(matrix[i][k]) + " ");
            }
            stringMatrix += "\n";
        }

        return stringMatrix;
    }

}

class Client{

    private static Socket stk;

    public static void main(String[] args) throws Exception {
        stk = new Socket("localhost", 2300);

        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        BufferedReader br  = new BufferedReader(new InputStreamReader(stk.getInputStream()));
        PrintStream ps = new PrintStream(stk.getOutputStream());

        String message;

        System.out.println("Welcome. Press enter first, then put in a text file you wish to be read. Otherwise, disconnect by typing: end\n");
        do {
            message = input.readLine();
            ps.println(message);
            message = br.readLine();
            if (message.endsWith(".txt")) {
                message = "";
                String number = br.readLine();
                int numb = Integer.parseInt(number);
                int i = 0;
                String line;
                while ( i < numb )  {
                    line = br.readLine();
                    message += line;
                    message += "\n";
                    i++;
                }
                output.flush();
                System.out.println(message);
            }

        } while(!message.equals("end"));
        System.out.println("Client shutting down...");
        stk.close();
    }

}

我遇到的另一个问题是我第一次创业的时候 ServerNClient 以及 Client ,当它到达服务器时,我键入的第一件事就是在开头包含这些字符 ャ? . 为什么这些角色会突然出现,我该如何摆脱它们?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题