连接被拒绝,因为服务器未按预期处理连接。我想要的是服务器可能使用线程或池,以便它可以处理多个客户端。如果客户端断开连接,应该对服务器没有影响,也不会阻止其他客户端连接。
将此功能添加到服务器的成语或技巧是什么?
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:~$
服务器仍在运行,但客户端无法再连接。