Multithreading 使用executor服务时发生java.net.ConnectException
我在下面收到这个错误。如果我取出代码的executor服务部分,我的代码就会工作。因此,这使它下降到大约几行代码。但我将包括必要的客户端和服务器代码Multithreading 使用executor服务时发生java.net.ConnectException,multithreading,jakarta-ee,executorservice,Multithreading,Jakarta Ee,Executorservice,我在下面收到这个错误。如果我取出代码的executor服务部分,我的代码就会工作。因此,这使它下降到大约几行代码。但我将包括必要的客户端和服务器代码 Error in connecting to server: 20999 java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualSt
Error in connecting to server: 20999
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at client.Client_Controller.connect(Client_Controller.java:32)
at client.Client_Controller.<init>(Client_Controller.java:23)
at client.Client_UI.<init>(Client_UI.java:29)
at client.Client_UI$1.run(Client_UI.java:169)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at client.Client_Controller.outputStream(Client_Controller.java:45)
at client.Client_Controller.<init>(Client_Controller.java:24)
at client.Client_UI.<init>(Client_UI.java:29)
at client.Client_UI$1.run(Client_UI.java:169)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
}
以下是服务器的主要方法:
public static void main(String... args) throws Exception {
ExecutorService executor = Executors.newCachedThreadPool();
System.out.println("Server has started");
try (Server server = new Server(20999, new ClientName_Handler.Factory(), executor))
{
server.start();
}
}
}
Client_Controller() {
this.connect(hostName, port);
this.outputStream();
this.inputStream();
}
private void connect(String hostName, int port) {
try {
socket = new Socket(hostName, port);
} catch (UnknownHostException ex) {
System.out.println("Host was not found: " + hostName);
} catch (IOException ex) {
System.out.println("Error in connecting to server: " + port);
ex.printStackTrace();
}
}
private void outputStream() {
try {
out = new DataOutputStream(socket.getOutputStream());
outObj = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException ex) {
System.out.println("Output stream failure");
}
}
private void inputStream() {
try {
in = new DataInputStream(socket.getInputStream());
inObj = new ObjectInputStream(socket.getInputStream());
} catch (IOException ex) {
System.out.println("Input stream failure");
}
}
public void closeConnection() throws IOException {
try {
this.sendMessage("Client_Close");
}
finally {
try {
out.close();
}
finally {
try {
in.close();
}
finally {
socket.close();
}
}
}
}
public void sendMessage(String message) throws IOException {
try {
out.writeUTF(message);
}
finally {
out.flush();
}
}
public String receiveMessage() {
String input = null;
try {
input = in.readUTF();
} catch (IOException ex) {
System.out.println("Error receving the message from the server");
}
return input;
}
public Object receiveObject(String message) {
Object input = null;
try {
sendMessage(message);
input = inObj.readObject();
System.out.println(input);
} catch (IOException | ClassNotFoundException ex) {
System.out.println("Error receving the message from the server");
}
return input;
}
以下是与服务器连接的客户端代码:
public static void main(String... args) throws Exception {
ExecutorService executor = Executors.newCachedThreadPool();
System.out.println("Server has started");
try (Server server = new Server(20999, new ClientName_Handler.Factory(), executor))
{
server.start();
}
}
}
Client_Controller() {
this.connect(hostName, port);
this.outputStream();
this.inputStream();
}
private void connect(String hostName, int port) {
try {
socket = new Socket(hostName, port);
} catch (UnknownHostException ex) {
System.out.println("Host was not found: " + hostName);
} catch (IOException ex) {
System.out.println("Error in connecting to server: " + port);
ex.printStackTrace();
}
}
private void outputStream() {
try {
out = new DataOutputStream(socket.getOutputStream());
outObj = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException ex) {
System.out.println("Output stream failure");
}
}
private void inputStream() {
try {
in = new DataInputStream(socket.getInputStream());
inObj = new ObjectInputStream(socket.getInputStream());
} catch (IOException ex) {
System.out.println("Input stream failure");
}
}
public void closeConnection() throws IOException {
try {
this.sendMessage("Client_Close");
}
finally {
try {
out.close();
}
finally {
try {
in.close();
}
finally {
socket.close();
}
}
}
}
public void sendMessage(String message) throws IOException {
try {
out.writeUTF(message);
}
finally {
out.flush();
}
}
public String receiveMessage() {
String input = null;
try {
input = in.readUTF();
} catch (IOException ex) {
System.out.println("Error receving the message from the server");
}
return input;
}
public Object receiveObject(String message) {
Object input = null;
try {
sendMessage(message);
input = inObj.readObject();
System.out.println(input);
} catch (IOException | ClassNotFoundException ex) {
System.out.println("Error receving the message from the server");
}
return input;
}
}
下面是代码行,如果我删除这些代码,将允许客户端连接。这些代码行可以在服务器类的方法start和listen下找到。我们将不胜感激
executor.submit(new Runnable() {
@Override
public void run() {
try {
listen();
}
catch (IOException ex) {
System.out.println("An exception occurred while the server was listening");
ex.printStackTrace();
}
}
});
executor.submit(new Runnable() {
public void run() {
delegateToHandler(socket);
}
});
当您通过ExecutorService运行listen方法时,它允许start返回,这会导致您的try with resources块退出并关闭服务器套接字,因此您的客户机无法连接