The connection is refused because the server hasn't handled the connection as intended. What I would like is for the server to use, perhaps, threads or pools, so that it can handle multiple clients. Should a client disconnect, as happens here, that should have no effect on server, nor prevent other clients from connecting.
What is the idiom or technique to add this functionality to the server?
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());
}
}
}
client code:
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());
}
}
}
Ultimately, the client will receive the record, update it, and send it back to server.
console output of the client:
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:~$
console output for the server:
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:~$
The server was still running, but the client could no longer connect.
I think the problem is that you try to start multiple ServerSockets, thus your server application starts throwing Exceptions, but you catch them. And don't exit.
A good place to start learning about socket programming is here, and multithreaded servers (with pools) are discussed here
Basicly it is better to keep a thread pool because it eliminates some of the overhead of strarting your Threads manually. here you can find a nice tutorial on ThreadPools. This should help you understand their pros and cons