gpt4 book ai didi

java - 如何添加线程或池以避免 ConnectException : Connection refused?

转载 作者:行者123 更新时间:2023-12-02 05:41:06 25 4
gpt4 key购买 nike

连接被拒绝,因为服务器未按预期处理连接。我希望服务器使用线程或池,以便它可以处理多个客户端。如果客户端断开连接,就像这里发生的那样,这不会对服务器产生影响,也不会阻止其他客户端连接。

将此功能添加到服务器的习惯用法或技术是什么?

package net.bounceme.dur.driver;

import java.net.*;
import java.io.*;
import java.util.Properties;
import java.util.logging.Logger;

public class Client {

private static final Logger log = Logger.getLogger(Client.class.getName());

private void put(String server, int portNumber) throws IOException, ClassNotFoundException {
Socket socket = new Socket(server, portNumber);
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream())) {
MyRecord recordFromServer = (MyRecord) objectInputStream.readObject();
log.info(recordFromServer.toString());
MyRecord record = new MyRecord(1, "from client");
objectOutputStream.writeObject(record);
}
}

public static void main(String args[]) throws IOException, ClassNotFoundException, InterruptedException {
Properties props = PropertiesReader.getProps();
int portNumber = Integer.parseInt(props.getProperty("port"));
String server = (props.getProperty("server"));
try {
new Client().put(server, portNumber);
} catch (IOException | ClassNotFoundException e) {
log.warning(e.toString());
}
}

}

客户端代码:

package net.bounceme.dur.driver;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Server {

private static final Logger log = Logger.getLogger(Server.class.getName());
private static final RecordQueue recordsQueue = new RecordQueue();

public static void main(String[] args) {
Properties props = PropertiesReader.getProps();
int portNumber = Integer.parseInt(props.getProperty("port"));
recordsQueue.populate();
while (true) {
try {
new Server().inOut(portNumber);
} catch (SocketException se) {
Logger.getLogger(Server.class.getName()).log(Level.FINE, "spammy", se);
} catch (IOException ioe) {
Logger.getLogger(Server.class.getName()).log(Level.WARNING, null, ioe);
} catch (ClassNotFoundException cnf) {
Logger.getLogger(Server.class.getName()).log(Level.INFO, null, cnf);
} catch (NoSuchElementException nse) {
Logger.getLogger(Server.class.getName()).log(Level.FINER, "no more records?", nse);
}
}
}

public void inOut(int portNumber) throws NoSuchElementException, IOException, ClassNotFoundException, java.net.SocketException {
MyRecord recordFromServer = recordsQueue.pop();
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket socket = serverSocket.accept();
MyRecord recordFromClient = null;
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream())) {
objectOutputStream.writeObject(recordFromServer);
recordFromClient = (MyRecord) objectInputStream.readObject();
log.info(recordFromClient.toString());
}
}
}

最终,客户端将接收记录,更新它,并将其发送回服务器。

客户端的控制台输出:

thufir@dur:~$ 
thufir@dur:~$ java -jar NetBeansProjects/Client/dist/Client.jar
Jul 01, 2014 8:43:18 AM net.bounceme.dur.driver.Client put
INFO: value=0, id=from server
Jul 01, 2014 8:43:18 AM net.bounceme.dur.driver.MyRecord <init>
INFO: trying..
Jul 01, 2014 8:43:18 AM net.bounceme.dur.driver.MyRecord <init>
INFO: value=1, id=from client
thufir@dur:~$
thufir@dur:~$
thufir@dur:~$ java -jar NetBeansProjects/Client/dist/Client.jar
Jul 01, 2014 8:43:24 AM net.bounceme.dur.driver.Client main
WARNING: java.net.ConnectException: Connection refused
thufir@dur:~$
thufir@dur:~$ java -jar NetBeansProjects/Client/dist/Client.jar
Jul 01, 2014 8:43:30 AM net.bounceme.dur.driver.Client main
WARNING: java.net.ConnectException: Connection refused
thufir@dur:~$
thufir@dur:~$

服务器的控制台输出:

thufir@dur:~$ 
thufir@dur:~$ java -jar NetBeansProjects/Server/dist/Server.jar
Jul 01, 2014 8:43:18 AM net.bounceme.dur.driver.Server inOut
INFO: value=1, id=from client
^Cthufir@dur:~$
thufir@dur:~$
thufir@dur:~$

服务器仍在运行,但客户端无法再连接。

最佳答案

我认为问题在于您尝试启动多个 ServerSocket,因此您的服务器应用程序开始抛出异常,但您捕获了它们。并且不要退出。

开始学习套接字编程的一个好地方是 here ,并讨论了多线程服务器(带池)here

基本上,最好保留一个线程池,因为它消除了手动启动线程的一些开销。 here你可以找到一个关于 ThreadPools 的很好的教程。这应该可以帮助您了解它们的优点和缺点

关于java - 如何添加线程或池以避免 ConnectException : Connection refused?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24514575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com