Creación de una aplicación con uso de sockets
Enviado por Carlos Coronel • 21 de Julio de 2020 • Ensayo • 2.129 Palabras (9 Páginas) • 143 Visitas
Creación de una aplicación con uso de sockets
servidor
1 | import java.net.*; |
2 | import java.io.*; |
3 | import java.util.Scanner; |
4 5 | |
6 7 | |
8 | public class Servidor { |
9 10 | |
11 | |
12 | private Socket socket; |
13 | private ServerSocket serverSocket; |
14 | private DataInputStream bufferDeEntrada = null; |
15 | private DataOutputStream bufferDeSalida = null; |
16 | Scanner escaner = new Scanner(System.in); |
17 | final String COMANDO_TERMINACION = "salir()"; |
18 19 | |
20 | public void levantarConexion(int puerto) { |
21 | try { |
22 | serverSocket = new ServerSocket(puerto); |
23 | mostrarTexto("Esperando conexión entrante en el puerto " + String.valueOf(puerto) + "..."); |
24 | socket = serverSocket.accept(); |
25 | mostrarTexto("Conexión establecida con: " + socket.getInetAddress().getHostName() + "\n\n\n"); |
26 | } catch (Exception e) { |
27 | mostrarTexto("Error en levantarConexion(): " + e.getMessage()); |
28 | System.exit(0); |
29 | } |
30 | } |
31 | public void flujos() { |
32 | try { |
33 | bufferDeEntrada = new DataInputStream(socket.getInputStream()); |
34 | bufferDeSalida = new DataOutputStream(socket.getOutputStream()); |
35 | bufferDeSalida.flush(); |
36 | } catch (IOException e) { |
37 | mostrarTexto("Error en la apertura de flujos"); |
38 | } |
39 | } |
40 41 | |
42 | public void recibirDatos() { |
43 | String st = ""; |
44 | try { |
45 | do { |
46 | st = (String) bufferDeEntrada.readUTF(); |
47 | mostrarTexto("\n[Cliente] => " + st); |
48 | System.out.print("\n[Usted] => "); |
49 | } while (!st.equals(COMANDO_TERMINACION)); |
50 | } catch (IOException e) { |
51 | cerrarConexion(); |
52 | } |
53 | } |
54 | public void enviar(String s) { |
55 | try { |
56 | bufferDeSalida.writeUTF(s); |
57 | bufferDeSalida.flush(); |
58 | } catch (IOException e) { |
59 | mostrarTexto("Error en enviar(): " + e.getMessage()); |
60 | } |
61 | } |
62 | public static void mostrarTexto(String s) { |
63 | System.out.print(s); |
64 | } |
65 | public void escribirDatos() { |
66 | while (true) { |
67 | System.out.print("[Usted] => "); |
68 | enviar(escaner.nextLine()); |
69 | } |
70 | } |
71 72 | |
73 | public void cerrarConexion() { |
74 | try { |
75 | bufferDeEntrada.close(); |
76 | bufferDeSalida.close(); |
77 | socket.close(); |
78 | } catch (IOException e) { |
79 | mostrarTexto("Excepción en cerrarConexion(): " + e.getMessage()); |
80 | } finally { |
81 | mostrarTexto("Conversación finalizada...."); |
82 | System.exit(0); |
82 83 | |
84 | } |
85 | } |
86 87 | |
88 | public void ejecutarConexion(int puerto) { |
89 | Thread hilo = new Thread(new Runnable() { |
90 | @Override |
91 | public void run() { |
92 | while (true) { |
93 | try { |
94 | levantarConexion(puerto); |
95 | flujos(); |
96 | recibirDatos(); |
97 | } finally { |
98 | cerrarConexion(); |
99 | } |
100 | } |
101 | } |
102 | }); |
103 | hilo.start(); |
104 | } |
105 106 | |
107 | public static void main(String[] args) throws IOException { |
108 | Servidor s = new Servidor(); |
109 | Scanner sc = new Scanner(System.in); |
110 111 | |
112 | mostrarTexto("Ingresa el puerto [5050 por defecto]: "); |
113 | String puerto = sc.nextLine(); |
114 | if (puerto.length() <= 0) puerto = "5050"; |
115 | s.ejecutarConexion(Integer.parseInt(puerto)); |
116 | s.escribirDatos(); |
117 | } |
118 | } |
Cliente
1 | import java.net.*; |
2 | import java.io.*; |
3 | import java.util.Scanner; |
4 | public class Cliente { |
5 | private Socket socket; |
6 | private DataInputStream bufferDeEntrada = null; |
7 | private DataOutputStream bufferDeSalida = null; |
8 | Scanner teclado = new Scanner(System.in); |
9 | final String COMANDO_TERMINACION = "salir()"; |
10 11 | |
12 | public void levantarConexion(String ip, int puerto) { |
13 | try { |
14 | socket = new Socket(ip, puerto); |
15 | mostrarTexto("Conectado a :" + socket.getInetAddress().getHostName()); |
16 | } catch (Exception e) { |
17 | mostrarTexto("Excepción al levantar conexión: " + e.getMessage()); |
18 | System.exit(0); |
19 | } |
20 | } |
21 22 | |
23 | public static void mostrarTexto(String s) { |
24 | System.out.println(s); |
25 | } |
26 27 | |
28 | public void abrirFlujos() { |
29 | try { |
30 | bufferDeEntrada = new DataInputStream(socket.getInputStream()); |
31 | bufferDeSalida = new DataOutputStream(socket.getOutputStream()); |
32 | bufferDeSalida.flush(); |
33 | } catch (IOException e) { |
34 | mostrarTexto("Error en la apertura de flujos"); |
35 | } |
36 | } |
37 38 | |
39 | public void enviar(String s) { |
40 | try { |
41 | bufferDeSalida.writeUTF(s); |
42 | bufferDeSalida.flush(); |
43 | } catch (IOException e) { |
44 | mostrarTexto("IOException on enviar"); |
45 | } |
46 | } |
47 48 | |
49 | public void cerrarConexion() { |
50 | try { |
51 | bufferDeEntrada.close(); |
52 | bufferDeSalida.close(); |
53 | socket.close(); |
54 | mostrarTexto("Conexión terminada"); |
55 | } catch (IOException e) { |
56 | mostrarTexto("IOException on cerrarConexion()"); |
57 | }finally{ |
58 | System.exit(0); |
59 | } |
60 | } |
61 62 | |
63 | public void ejecutarConexion(String ip, int puerto) { |
64 | Thread hilo = new Thread(new Runnable() { |
65 | @Override |
66 | public void run() { |
67 | try { |
68 | levantarConexion(ip, puerto); |
69 | abrirFlujos(); |
70 | recibirDatos(); |
71 | } finally { |
72 | cerrarConexion(); |
73 | } |
74 | } |
75 | }); |
76 | hilo.start(); |
77 | } |
78 79 | |
80 | public void recibirDatos() { |
81 | String st = ""; |
82 | try { |
83 | do { |
84 | st = (String) bufferDeEntrada.readUTF(); |
85 | mostrarTexto("\n[Servidor] => " + st); |
86 | System.out.print("\n[Usted] => "); |
87 | } while (!st.equals(COMANDO_TERMINACION)); |
88 | } catch (IOException e) {} |
89 | } |
90 91 | |
92 | public void escribirDatos() { |
93 | String entrada = ""; |
94 | while (true) { |
95 | System.out.print("[Usted] => "); |
96 | entrada = teclado.nextLine(); |
97 | if(entrada.length() > 0) |
98 | enviar(entrada); |
99 | } |
100 | } |
101 102 | |
103 | public static void main(String[] argumentos) { |
104 | Cliente cliente = new Cliente(); |
105 | Scanner escaner = new Scanner(System.in); |
106 | mostrarTexto("Ingresa la IP: [localhost por defecto] "); |
107 | String ip = escaner.nextLine(); |
108 | if (ip.length() <= 0) ip = "localhost"; |
109 110 | |
112 | mostrarTexto("Puerto: [5050 por defecto] "); |
113 | String puerto = escaner.nextLine(); |
114 | if (puerto.length() <= 0) puerto = "5050"; |
115 | cliente.ejecutarConexion(ip, Integer.parseInt(puerto)); |
116 | cliente.escribirDatos(); |
117 | } |
118 | } |
...