Ver Mensaje Individual
  #4 (permalink)  
Antiguo 28/08/2008, 15:03
Avatar de HackmanC
HackmanC
 
Fecha de Ingreso: enero-2008
Ubicación: Guatemala
Mensajes: 1.817
Antigüedad: 16 años, 3 meses
Puntos: 260
Sonrisa Respuesta: Solicitud de archivo

Hola,

Supongo que básicamente está bien.
El puerto lo indicas constructor de java.net.ServerSocket(int port)

Saludos.

ps:

Un código algo antiguo (lo hice hace un par de años) posiblemente se pueda optimizar con el nuevo JDK :

Código:
public class JServer extends JThread {
    .... +code ....
    public void run() {
        running = true;
        
        try { server = new ServerSocket(port); }
        catch (IOException e) { running = false; }
        
        while(running) {
            try {
                Socket socket = server.accept();
                
                if(running)
                    try { handleConnection(socket); }
                    catch (IOException e) {
                        try { socket.close(); }
                        catch (IOException es) {}
                    }
            } catch (IOException e) {
                running = false;
            }
        }
        
        users.killAllUsers();
    }
    .... +code ....
    private synchronized void handleConnection(Socket socket) throws IOException {
        if(running) {
            String hostname = socket.getInetAddress().getHostName();
            String hostaddr = socket.getInetAddress().getHostAddress();
            
            if(!serverFull(socket))
                if(!overConnected(socket, hostname, hostaddr))
                    if(!bannedConnection(socket, hostname, hostaddr))
                        users.handleConnection(socket);
        }
    }
    .... +code ....
}

public class JSocket extends JObject {
    .... +code ....
    public void open() throws IOException {
        out = new PrintStream(socket.getOutputStream(), JConfig.SOCKET_AUTOFLUSH);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream(), JConfig.SOCKET_ENCODING));
    }
    .... +code ....
}
Otra parte importante de JServer

Código:
    public synchronized void stops() {
        if(running) {
            running = false;
            try { server.close(); }
            catch (IOException e) {}
        }
    }

Última edición por HackmanC; 28/08/2008 a las 15:23 Razón: close()