-
Good for I/O base?
Hey all,
I'm creating a server from scratch using the standard I/O. I'm just doing it for experience, and I don't except to create an awesome stable server. I just need some comments if this is good? Just was wondering if the s.isConnected() would be a good idea.
Thanks,
William.D
Code:
package com.williaminc.server;
import java.io.IOException;
import com.williaminc.server.util.Logger;
public class Server {
public static java.net.ServerSocket clientListener = null;
private static Logger logger = Logger.getInstance();
public Server() {
logger.log("Engine of "+Configuration.SERVER_NAME+" started");
listen(Configuration.SERVER_PORT);
}
public boolean listen(int port) {
try {
/*
* STARTING THE SOCKET
*/
logger.log("Listening on "+port, true);
clientListener = new java.net.ServerSocket(port, 1);
while(true) {
java.net.Socket s = clientListener.accept();
logger.log("New connection from: "+s.getInetAddress().getHostName(), true);
if (s.isConnected()) {
/*
* CREATE NEW PLAYER
*/
} else {
/*
* CLOSE CONNECTION?
*/
}
}
} catch (IOException e) {
logger.log("Could not listen on "+port);
logger.logError(e);
}
return true;
}
}
-
Well the only way while loops work is that if there in their own runnable environment meaning you cannot call the listen in a constructor. it has to be called in the main method or in a run() {.
Also the listen boolean method will never return until the while loop finished.
EDIT: Looking at the code again I see that you have a try catch block outside of the while loop. In a way that is bad because if a connection error's whilst connecting then the server cannot listen anymore..