Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.logging.*;
- public class TCPServidor extends Thread {
- private ObjectOutputStream salida;
- private ObjectInputStream entrada;
- private ServerSocket servidor;
- private Socket conexion;
- int Port = 6789;
- public TCPServidor()
- {
- }
- public void setServidor() throws IOException
- {
- // configurar servidor para que reciba conexiones; procesar las conexiones
- try {
- // Paso 1: crear un objeto ServerSocket.
- servidor = new ServerSocket(Port);
- while ( true ) {
- try {
- waitConexion(); // Paso 2: esperar una conexión.
- flujosES(); // Paso 3: obtener flujos de entrada y salida.
- getConexion(); // Paso 4: procesar la conexión.
- }
- // procesar excepción IOXException cuando el cliente cierre la conexión
- catch ( IOException e ) {
- System.err.println( "El servidor terminó la conexión" );
- }
- finally {
- logout(); // Paso 5: cerrar la conexión.
- }
- } // fin de instrucción while
- } // fin del bloque try
- // procesar problemas con E/S
- catch ( IOException e ) {
- printMensaje( "\n Problemas con los flujos" );
- }
- }
- private void waitConexion()
- {
- printMensaje( "Esperando una conexión\n" );
- conexion = servidor.accept(); // permitir al servidor aceptar la conexión
- printMensaje( "Conexión recibida de: " + conexion.getInetAddress().getHostName() );
- }
- private void getConexion() throws IOException
- {
- // enviar mensaje de conexión exitosa al cliente
- String mensaje = "Conexión establecida";
- sentMensaje( mensaje );
- do { // procesar los mensajes enviados por el cliente
- // leer el mensaje y mostrarlo en pantalla
- try {
- mensaje = ( String ) entrada.readUTF();
- printMensaje( "\n" + mensaje );
- }
- // atrapar problemas que pueden ocurrir al tratar de leer del cliente
- catch ( IOException e ) {
- printMensaje( "\nError al enviar" );
- }
- } while ( !mensaje.equals( "CLIENTE>>> TERMINAR" ) );
- }
- private void sentMensaje(String msj) throws IOException
- {
- // enviar objeto al cliente
- try {
- salida.writeObject( "SERVIDOR>>> " + msj );
- salida.flush();
- printMensaje( "\nSERVIDOR>>> " + msj );
- }
- // procesar problemas que pueden ocurrir al enviar el mensaje
- catch ( IOException e ) {
- printMensaje( "\nError al enviar" );
- }
- }
- private void printMensaje(final String msj)
- {
- System.out.println(msj);
- }
- private void logout() throws IOException
- {
- System.out.println("El usuario llamado" + conexion.getInetAddress().getHostName() + " se ha desconectado.");
- try{
- conexion.close();
- salida.close();
- entrada.close();
- }
- catch (IOException e) {
- Logger.getLogger(TCPServidor.class.getName()).log(Level.SEVERE, null, e);
- }
- }
- private void flujosES()
- {
- salida = new ObjectOutputStream( conexion.getOutputStream() );
- salida.flush();
- entrada = new ObjectInputStream( conexion.getInputStream() );
- printMensaje( "\nSe recibieron los flujos de E/S\n" );
- }
- public static void main(String args[]) throws IOException
- {
- TCPServidor chat=new TCPServidor();
- chat.setServidor();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement