为什么会出现服务器异常:套接字已关闭?

时间:2019-02-28 08:06:13

标签: multithreading server serversocket java-threads

对于我的班级,我必须有一堆与服务器通信的客户端。您在开始时通过提示输入客户端数量。我知道我应该使用线程来运行每个客户端,但是对于我的代码出了什么问题,我感到困惑。每次我运行它时,都会显示服务器异常:套接字已关闭。当我检查服务器端时,它对每个线程说以下内容:

New client Connected.Exception in thread "Thread-0" 
java.lang.NumberFormatException: null

at java.base/java.lang.Integer.parseInt(Integer.java:614)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at ServerThread.run(Server.java:40)

有人可以向我解释我在做错什么,为什么会给我这个错误?这是我的客户端和服务器的代码:

客户:

import java.util.*;
import java.net.*;
import java.io.*;

public class Client {
  public static void main(String[] args) {
    if (args.length < 2) {
      System.out.print("Error, less than 2 arguments. Argumens must contain 
      the server hostname and the port number.");
      System.exit(0);
    }
    String hostName = args[0];
    int portNumber = Integer.parseInt(args[1]);
    boolean clientFlag = true;
    int numberOfClients = 0;
    Scanner clientInput = new Scanner(System.in);

    while (clientFlag) {
      System.out.print("Please enter the number of clients desired: ");
      try {
        numberOfClients = clientInput.nextInt();
        clientFlag = false;
      }    
      catch(InputMismatchException ex) {
        System.out.println("Error, must enter an integer. Please try 
        again.");
        clientInput.next();
      }
    }

    Thread[] threadArray = new Thread[numberOfClients];
    for (int i = 0; i < numberOfClients; i++) {
      try (Socket socket = new Socket(hostName, portNumber)) {
        threadArray[i] = new ClientThread(socket);
      }
      catch (UnknownHostException ex) {
        System.out.println("Server not found: " + ex.getMessage());
      }
      catch (IOException ex) {
        System.out.println("I/O error: " + ex.getMessage());
      }
    }

    for (int i = 0; i < numberOfClients; i++) {
      threadArray[i].start()
    }    
  }
}

class ClientThread extends Thread {
  private Socket socket;

  public ClientThread(Socket socket) {
    this.socket = socket;
  }

  public void run() {
    try {
      OutputStream output = socket.getOutputStream();
      PrintWriter writer = new PrintWriter(output, true);

      Scanner input2 = new Scanner(System.in);
      boolean flag = true;
      String option;
      String serverValue;
      do {
        System.out.println("\n1.   Host current Date and Time.");
        System.out.println("2.   Host Uptime");
        System.out.println("3.   Host memory use");
        System.out.println("4.   Host Netstat");
        System.out.println("5.   Host current users");
        System.out.println("6.   Host running processes");
        System.out.println("7.   Quit");
        System.out.print("\nPlease type the number of the option you wish to 
        select: ");

        option = input2.nextLine();
        writer.println(option);
        if (!(option.equals("1") || option.equals("2") || option.equals("3") 
        || option.equals("4") || option.equals("5") || option.equals("6") || 
        option.equals("7")))
          System.out.println("Invalid input, please try again.");
        if (option.equals("7")) {
          System.out.println("Connection to server terminated...");
        flag = false;
        }

        InputStream input = socket.getInputStream();
        BufferedReader reader = new BufferedReader(new 
        InputStreamReader(input));
        serverValue = reader.readLine();
        System.out.println(serverValue);
      } while (flag);
      socket.close();
    }
    catch(IOException ex) {
      System.out.println("Server exception: " + ex.getMessage());
    }
  }
}

服务器:

import java.util.*;
import java.net.*;
import java.io.*;

public class Server {
  public static void main(String[] args) {
    if (args.length < 1) {
      System.out.print("Error, no arguments inputted. Argument must contain 
      the server port number.");
      System.exit(0);
    }

    int port = Integer.parseInt(args[0]);
    try(ServerSocket serverSocket = new ServerSocket(port)) {
      System.out.println("Server is now listening on port " + port);

      while(true) {
      Socket socket = serverSocket.accept();
      System.out.println("New client Connected.");

      new ServerThread(socket).start();
      }
    }
    catch (IOException ex) {
      System.out.println("IO Exceptin in server: " + ex.getMessage());
    }
  }
}

class ServerThread extends Thread {
  private Socket socket;

  public ServerThread(Socket socket) {
    this.socket = socket;
  }

  public void run() {
    try {
      InputStream input = socket.getInputStream();
      BufferedReader reader = new BufferedReader(new 
      InputStreamReader(input));
      int clientInput = Integer.parseInt(reader.readLine());
      OutputStream output = socket.getOutputStream();
      PrintWriter writer = new PrintWriter(output, true);
      while (clientInput != 7) { 
        switch (clientInput) {
          case 1: writer.println("You selected option 1"); break;
          case 2: writer.println("You selected option 2"); break;
          case 3: writer.println("You selected option 3"); break;
          case 4: writer.println("You selected option 4"); break;
          case 5: writer.println("You selected option 5"); break;
          case 6: writer.println("You selected option 6"); break;
        }
      }
    }
    catch(IOException ex) {
      System.out.println("Server exception: " + ex.getMessage());
    }
  }
}

0 个答案:

没有答案