如何在客户端修复打印服务器端输出

时间:2019-03-24 15:20:51

标签: java

我有一个服务器代码,其中输出只打印一半,而并非全部打印在客户端上

服务器代码为:

public static void main(String args[]) throws IOException {
    int number;
    String temp = null;
    ServerSocket s1 = new ServerSocket(1306);
    Socket ss = s1.accept();
    Scanner sc = new Scanner(ss.getInputStream());
    number = sc.nextInt();
    //temp = number*5;
    switch (number) {
        case 2304: {
            temp = "RESEARCH METHODOLOGY "
            + "\n Madam Cecelia";
            PrintStream p = new PrintStream(ss.getOutputStream());
            p.println(temp); 
            break;
        }
        case 2404: {
            String temp = "PROJECT MANAGEMENT\n PATRICK Barack";
            PrintStream p = new PrintStream(ss.getOutputStream());
            p.println(temp);
            break;
        }
        case 2305: {
            String temp = "HUMAN COMPUTER INTERACTION\n Dr. HADULLO";
            PrintStream p = new PrintStream(ss.getOutputStream());                  
            p.println(temp);
            break;
        }
    }

客户端代码如下,当客户端发送单位代码,然后服务器通过客户端请求的输出进行响应时,客户端代码就会起作用。

Scanner sc1= new Scanner(System.in);
try {
    Socket so= new Socket("127.0.0.1", 1306);       
    System.out.println("Enter unit code");
    int number= sc1.nextInt();
    PrintStream p =  new PrintStream(so.getOutputStream());
    p.println(number);
    Scanner sc2= new Scanner(so.getInputStream());
    String temp= sc2.next();
    System.out.println(temp);

1 个答案:

答案 0 :(得分:0)

尝试flush()设置PrintStream:javadoc