Commit ae910eda authored by Administrator's avatar Administrator

Cleans the code

Code has been reviewed and cleaned.
parent 0124c569
...@@ -28,7 +28,7 @@ import java.net.UnknownHostException; ...@@ -28,7 +28,7 @@ import java.net.UnknownHostException;
public class HelloWorldClient { public class HelloWorldClient {
public static void main(String[] args) { public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 12345)) { try (Socket socket = new Socket("localhost", 50000)) {
System.out.println("Local port: " + socket.getLocalPort()); System.out.println("Local port: " + socket.getLocalPort());
final InputStream input = socket.getInputStream(); final InputStream input = socket.getInputStream();
...@@ -41,9 +41,11 @@ public class HelloWorldClient { ...@@ -41,9 +41,11 @@ public class HelloWorldClient {
// El InputStream se cerrará cuando se cierre el Socket // El InputStream se cerrará cuando se cierre el Socket
} catch (final UnknownHostException e) { } catch (final UnknownHostException e) {
System.out.println("Unknown host: localhost"); System.out.println("Unknown host: localhost");
e.printStackTrace();
} catch (final IOException e) { } catch (final IOException e) {
System.out.print("Connection error: "); System.out.print("Connection error: ");
System.out.println(e.getMessage()); System.out.println(e.getMessage());
e.printStackTrace();
} }
} }
} }
...@@ -28,7 +28,7 @@ import java.net.Socket; ...@@ -28,7 +28,7 @@ import java.net.Socket;
public class HelloWorldServer { public class HelloWorldServer {
public static void main(String[] args) { public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(12345)) { try (ServerSocket serverSocket = new ServerSocket(50000)) {
while (true) { while (true) {
System.out.println("Antes de accept"); System.out.println("Antes de accept");
...@@ -43,6 +43,7 @@ public class HelloWorldServer { ...@@ -43,6 +43,7 @@ public class HelloWorldServer {
} }
} catch (final IOException e) { } catch (final IOException e) {
System.err.println("Server socket could not be created"); System.err.println("Server socket could not be created");
e.printStackTrace();
} }
} }
} }
...@@ -27,19 +27,21 @@ import java.net.DatagramSocket; ...@@ -27,19 +27,21 @@ import java.net.DatagramSocket;
public class HelloWorldReceiver { public class HelloWorldReceiver {
public static void main(String[] args) { public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket(1234)) { try (DatagramSocket socket = new DatagramSocket(60000)) {
final byte[] buffer = new byte[128]; final byte[] buffer = new byte[128];
final DatagramPacket packet = new DatagramPacket(buffer, buffer.length); final DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
System.out.println("Antes de recibir");
socket.receive(packet); socket.receive(packet);
final String message = new String(packet.getData(), 0, packet.getLength()); final String message = new String(packet.getData(), 0, packet.getLength());
System.out.println("Mensaje recibido: " + message); System.out.println("Mensaje recibido: " + message);
} catch (final IOException ioe) { } catch (final IOException e) {
System.out.print("Connection error: "); System.out.print("Connection error: ");
System.out.println(ioe.getMessage()); System.out.println(e.getMessage());
e.printStackTrace();
} }
} }
} }
...@@ -32,15 +32,17 @@ public class HelloWorldSender { ...@@ -32,15 +32,17 @@ public class HelloWorldSender {
final String message = "Hello World"; final String message = "Hello World";
final DatagramPacket packet = new DatagramPacket( final DatagramPacket packet = new DatagramPacket(
message.getBytes(), message.length(), InetAddress.getLocalHost(), 1234 message.getBytes(), message.length(), InetAddress.getLocalHost(), 60000
); );
System.out.println("Antes de enviar");
socket.send(packet); socket.send(packet);
System.out.println("Mensaje enviado"); System.out.println("Mensaje enviado");
} catch (final IOException ioe) { } catch (final IOException e) {
System.out.print("Error de conexión: "); System.out.print("Error de conexión: ");
System.out.println(ioe.getMessage()); System.out.println(e.getMessage());
e.printStackTrace();
} }
} }
} }
...@@ -34,7 +34,7 @@ public class EchoClient { ...@@ -34,7 +34,7 @@ public class EchoClient {
// final Console console = System.console(); // final Console console = System.console();
final BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); final BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
try (Socket socket = new Socket("localhost", 1234)) { try (Socket socket = new Socket("localhost", 50000)) {
final DataInputStream input = new DataInputStream(socket.getInputStream()); final DataInputStream input = new DataInputStream(socket.getInputStream());
final DataOutputStream output = new DataOutputStream(socket.getOutputStream()); final DataOutputStream output = new DataOutputStream(socket.getOutputStream());
...@@ -49,6 +49,7 @@ public class EchoClient { ...@@ -49,6 +49,7 @@ public class EchoClient {
} }
} catch (final UnknownHostException e) { } catch (final UnknownHostException e) {
System.out.println("Unknown host: localhost"); System.out.println("Unknown host: localhost");
e.printStackTrace();
} catch (final IOException e) { } catch (final IOException e) {
System.out.println("Connection error: " + e.getMessage()); System.out.println("Connection error: " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
......
...@@ -29,7 +29,7 @@ public class EchoReceiver { ...@@ -29,7 +29,7 @@ public class EchoReceiver {
private static final int PACKET_LENGTH = 1024; private static final int PACKET_LENGTH = 1024;
public static void main(String[] args) { public static void main(String[] args) {
try (DatagramSocket receiverSocket = new DatagramSocket(1234)) { try (DatagramSocket receiverSocket = new DatagramSocket(60000)) {
while (true) { while (true) {
final DatagramPacket packet = new DatagramPacket(new byte[PACKET_LENGTH], PACKET_LENGTH); final DatagramPacket packet = new DatagramPacket(new byte[PACKET_LENGTH], PACKET_LENGTH);
...@@ -44,7 +44,8 @@ public class EchoReceiver { ...@@ -44,7 +44,8 @@ public class EchoReceiver {
receiverSocket.send(packet); receiverSocket.send(packet);
} }
} catch (final IOException e) { } catch (final IOException e) {
System.err.println("Server socket could not be created"); System.out.println("Server socket could not be created");
e.printStackTrace();
} }
} }
} }
...@@ -35,13 +35,13 @@ public class EchoSender { ...@@ -35,13 +35,13 @@ public class EchoSender {
// final Console console = System.console(); // final Console console = System.console();
final BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); final BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
try (DatagramSocket socket = new DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), 4444))) { try (DatagramSocket socket = new DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), 55555))) {
String line; String line;
while ((line = console.readLine()) != null) { while ((line = console.readLine()) != null) {
System.out.println("INPUT: " + line); System.out.println("INPUT: " + line);
final byte[] lineData = line.getBytes(); final byte[] lineData = line.getBytes();
final DatagramPacket packet = new DatagramPacket(lineData, lineData.length, InetAddress.getLocalHost(), 1234); final DatagramPacket packet = new DatagramPacket(lineData, lineData.length, InetAddress.getLocalHost(), 60000);
// El paquete se envía // El paquete se envía
socket.send(packet); socket.send(packet);
...@@ -53,8 +53,10 @@ public class EchoSender { ...@@ -53,8 +53,10 @@ public class EchoSender {
} }
} catch (final UnknownHostException e) { } catch (final UnknownHostException e) {
System.out.println("Unknown host: localhost"); System.out.println("Unknown host: localhost");
e.printStackTrace();
} catch (final IOException e) { } catch (final IOException e) {
System.out.println("Connection error: " + e.getMessage()); System.out.println("Connection error: " + e.getMessage());
e.printStackTrace();
} }
} }
} }
...@@ -29,7 +29,7 @@ import java.net.Socket; ...@@ -29,7 +29,7 @@ import java.net.Socket;
public class EchoServer { public class EchoServer {
public static void main(String[] args) { public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(1234)) { try (ServerSocket serverSocket = new ServerSocket(50000)) {
while (true) { while (true) {
try (Socket clientSocket = serverSocket.accept()) { try (Socket clientSocket = serverSocket.accept()) {
final DataInputStream input = new DataInputStream(clientSocket.getInputStream()); final DataInputStream input = new DataInputStream(clientSocket.getInputStream());
...@@ -44,6 +44,7 @@ public class EchoServer { ...@@ -44,6 +44,7 @@ public class EchoServer {
} }
} catch (final IOException e) { } catch (final IOException e) {
System.err.println("Server socket could not be created"); System.err.println("Server socket could not be created");
e.printStackTrace();
} }
} }
} }
...@@ -29,7 +29,7 @@ import java.net.DatagramSocket; ...@@ -29,7 +29,7 @@ import java.net.DatagramSocket;
public class MessageReceiver { public class MessageReceiver {
public static void main(String[] args) { public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket(1234)) { try (DatagramSocket socket = new DatagramSocket(60000)) {
final byte[] buffer = new byte[1500]; final byte[] buffer = new byte[1500];
while (true) { while (true) {
...@@ -49,9 +49,10 @@ public class MessageReceiver { ...@@ -49,9 +49,10 @@ public class MessageReceiver {
e.printStackTrace(); e.printStackTrace();
} }
} }
} catch (final IOException ioe) { } catch (final IOException e) {
System.out.println("Connection error: "); System.out.println("Connection error: ");
System.out.println(ioe.getMessage()); System.out.println(e.getMessage());
e.printStackTrace();
} }
} }
} }
...@@ -32,7 +32,7 @@ import java.util.Date; ...@@ -32,7 +32,7 @@ import java.util.Date;
public class MessageSender { public class MessageSender {
public static void main(String[] args) { public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket()) { try (DatagramSocket socket = new DatagramSocket()) {
final ByteArrayOutputStream output = new ByteArrayOutputStream(1500); final ByteArrayOutputStream output = new ByteArrayOutputStream(60000);
final ObjectOutputStream dataOutput = new ObjectOutputStream(output); final ObjectOutputStream dataOutput = new ObjectOutputStream(output);
final Message message = new Message("Time", "Current time: " + new Date()); final Message message = new Message("Time", "Current time: " + new Date());
...@@ -45,10 +45,10 @@ public class MessageSender { ...@@ -45,10 +45,10 @@ public class MessageSender {
); );
socket.send(packet); socket.send(packet);
} catch (final IOException ioe) { } catch (final IOException e) {
ioe.printStackTrace();
System.out.print("Connection error: "); System.out.print("Connection error: ");
System.out.println(ioe.getMessage()); System.out.println(e.getMessage());
e.printStackTrace();
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment