如何添加线程或池以避免ConnectException:连接被拒绝?

时间:2014-07-01 15:53:04

标签: java sockets client-server connection-pooling serversocket

连接被拒绝,因为服务器未按预期处理连接。我想要的是服务器可能使用线程或池,以便它可以处理多个客户端。如果客户端断开连接,应该对服务器没有影响,也不会阻止其他客户端连接。

将此功能添加到服务器的成语或技巧是什么?

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:~$ 

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

1 个答案:

答案 0 :(得分:2)

我认为问题是你尝试启动多个ServerSockets,因此你的服务器应用程序开始抛出异常,但你抓住了它们。并且不要退出。

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

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