Created
February 9, 2018 18:05
-
-
Save parzibyte/db22bb6783f3c3cf5b34756faa349dd3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.*; | |
import java.io.*; | |
import java.util.Scanner; | |
public class Servidor { | |
private Socket socket; | |
private ServerSocket serverSocket; | |
private DataInputStream bufferDeEntrada = null; | |
private DataOutputStream bufferDeSalida = null; | |
Scanner escaner = new Scanner(System.in); | |
final String COMANDO_TERMINACION = "salir()"; | |
public void levantarConexion(int puerto) { | |
try { | |
serverSocket = new ServerSocket(puerto); | |
mostrarTexto("Esperando conexión entrante en el puerto " + String.valueOf(puerto) + "..."); | |
socket = serverSocket.accept(); | |
mostrarTexto("Conexión establecida con: " + socket.getInetAddress().getHostName() + "\n\n\n"); | |
} catch (Exception e) { | |
mostrarTexto("Error en levantarConexion(): " + e.getMessage()); | |
System.exit(0); | |
} | |
} | |
public void flujos() { | |
try { | |
bufferDeEntrada = new DataInputStream(socket.getInputStream()); | |
bufferDeSalida = new DataOutputStream(socket.getOutputStream()); | |
bufferDeSalida.flush(); | |
} catch (IOException e) { | |
mostrarTexto("Error en la apertura de flujos"); | |
} | |
} | |
public void recibirDatos() { | |
String st = ""; | |
try { | |
do { | |
st = (String) bufferDeEntrada.readUTF(); | |
mostrarTexto("\n[Cliente] => " + st); | |
System.out.print("\n[Usted] => "); | |
} while (!st.equals(COMANDO_TERMINACION)); | |
} catch (IOException e) { | |
cerrarConexion(); | |
} | |
} | |
public void enviar(String s) { | |
try { | |
bufferDeSalida.writeUTF(s); | |
bufferDeSalida.flush(); | |
} catch (IOException e) { | |
mostrarTexto("Error en enviar(): " + e.getMessage()); | |
} | |
} | |
public static void mostrarTexto(String s) { | |
System.out.print(s); | |
} | |
public void escribirDatos() { | |
while (true) { | |
System.out.print("[Usted] => "); | |
enviar(escaner.nextLine()); | |
} | |
} | |
public void cerrarConexion() { | |
try { | |
bufferDeEntrada.close(); | |
bufferDeSalida.close(); | |
socket.close(); | |
} catch (IOException e) { | |
mostrarTexto("Excepción en cerrarConexion(): " + e.getMessage()); | |
} finally { | |
mostrarTexto("Conversación finalizada...."); | |
System.exit(0); | |
} | |
} | |
public void ejecutarConexion(int puerto) { | |
Thread hilo = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
while (true) { | |
try { | |
levantarConexion(puerto); | |
flujos(); | |
recibirDatos(); | |
} finally { | |
cerrarConexion(); | |
} | |
} | |
} | |
}); | |
hilo.start(); | |
} | |
public static void main(String[] args) throws IOException { | |
Servidor s = new Servidor(); | |
Scanner sc = new Scanner(System.in); | |
mostrarTexto("Ingresa el puerto [5050 por defecto]: "); | |
String puerto = sc.nextLine(); | |
if (puerto.length() <= 0) puerto = "5050"; | |
s.ejecutarConexion(Integer.parseInt(puerto)); | |
s.escribirDatos(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment