Commit 0124c569 authored by Administrator's avatar Administrator

Initial commit

Initial commit with the already existing examples code.
parents
#Eclipse
.settings
.project
.classpath
#Maven
target
#General
bak
This diff is collapsed.
# Ejemplos de Sockets
Ejemplos de Sockets incluidos en el Tema 2 de la asignatura DAI.
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>es.uvigo.esei.dai</groupId>
<artifactId>sockets</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Ejemplos de DAI - Sockets</name>
<inceptionYear>2014</inceptionYear>
<developers>
<developer>
<name>Miguel Reboiro-Jato</name>
<organization>Escola Superior de Enxeñaría Informática -
Universidade de Vigo</organization>
<organizationUrl>https://esei.uvigo.es/</organizationUrl>
<email>mrjato@uvigo.gal</email>
</developer>
</developers>
<licenses>
<license>
<name>GNU GENERAL PUBLIC LICENSE, Version 3</name>
<url>http://www.gnu.org/licenses/gpl.html</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Plugin versions -->
<license-maven-plugin.version>2.2.0</license-maven-plugin.version>
<maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
<!-- license-maven-plugin configuration -->
<license.licenseName>gpl_v3</license.licenseName>
<license.copyrightOwners>Miguel Reboiro Jato</license.copyrightOwners>
<license.organizationName>Universidade de Vigo</license.organizationName>
<license.addJavaLicenseAfterPackage>false</license.addJavaLicenseAfterPackage>
</properties>
<contributors>
<contributor>
<name>Miguel Reboiro Jato</name>
<email>mrjato@uvigo.gal</email>
<organization>Escola Superior de Enxeñaría Informática -
Universidade de Vigo</organization>
<organizationUrl>https://esei.uvigo.es/</organizationUrl>
<roles>
<role>author</role>
<role>professor</role>
</roles>
</contributor>
</contributors>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>${license-maven-plugin.version}</version>
<executions>
<execution>
<id>first</id>
<goals>
<goal>update-file-header</goal>
</goals>
<phase>process-sources</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
/*-
* #%L
* Ejemplos de DAI - Sockets
* %%
* Copyright (C) 2014 - 2023 Miguel Reboiro Jato
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.sockets.example1;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class HelloWorldClient {
public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 12345)) {
System.out.println("Local port: " + socket.getLocalPort());
final InputStream input = socket.getInputStream();
int read;
while ((read = input.read()) != -1) {
System.out.print((char) read);
}
System.out.println();
// El InputStream se cerrará cuando se cierre el Socket
} catch (final UnknownHostException e) {
System.out.println("Unknown host: localhost");
} catch (final IOException e) {
System.out.print("Connection error: ");
System.out.println(e.getMessage());
}
}
}
/*-
* #%L
* Ejemplos de DAI - Sockets
* %%
* Copyright (C) 2014 - 2023 Miguel Reboiro Jato
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.sockets.example1;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class HelloWorldServer {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(12345)) {
while (true) {
System.out.println("Antes de accept");
try (Socket clientSocket = serverSocket.accept()) {
System.out.println("Después de accept");
final OutputStream output = clientSocket.getOutputStream();
output.write("Hello world".getBytes());
output.flush();
}
}
} catch (final IOException e) {
System.err.println("Server socket could not be created");
}
}
}
/*-
* #%L
* Ejemplos de DAI - Sockets
* %%
* Copyright (C) 2014 - 2023 Miguel Reboiro Jato
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.sockets.example2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class HelloWorldReceiver {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket(1234)) {
final byte[] buffer = new byte[128];
final DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
final String message = new String(packet.getData(), 0, packet.getLength());
System.out.println("Mensaje recibido: " + message);
} catch (final IOException ioe) {
System.out.print("Connection error: ");
System.out.println(ioe.getMessage());
}
}
}
/*-
* #%L
* Ejemplos de DAI - Sockets
* %%
* Copyright (C) 2014 - 2023 Miguel Reboiro Jato
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.sockets.example2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class HelloWorldSender {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket()) {
final String message = "Hello World";
final DatagramPacket packet = new DatagramPacket(
message.getBytes(), message.length(), InetAddress.getLocalHost(), 1234
);
socket.send(packet);
System.out.println("Mensaje enviado");
} catch (final IOException ioe) {
System.out.print("Error de conexión: ");
System.out.println(ioe.getMessage());
}
}
}
/*-
* #%L
* Ejemplos de DAI - Sockets
* %%
* Copyright (C) 2014 - 2023 Miguel Reboiro Jato
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.sockets.example3;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
public class EchoClient {
public static void main(String[] args) {
// final Console console = System.console();
final BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
try (Socket socket = new Socket("localhost", 1234)) {
final DataInputStream input = new DataInputStream(socket.getInputStream());
final DataOutputStream output = new DataOutputStream(socket.getOutputStream());
String line;
while ((line = console.readLine()) != null) {
System.out.println("INPUT: " + line);
output.writeUTF(line);
// Fallará cuando se envíe un "quit"
System.out.println("ECHO: " + input.readUTF());
}
} catch (final UnknownHostException e) {
System.out.println("Unknown host: localhost");
} catch (final IOException e) {
System.out.println("Connection error: " + e.getMessage());
e.printStackTrace();
}
}
}
/*-
* #%L
* Ejemplos de DAI - Sockets
* %%
* Copyright (C) 2014 - 2023 Miguel Reboiro Jato
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.sockets.example3;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class EchoReceiver {
private static final int PACKET_LENGTH = 1024;
public static void main(String[] args) {
try (DatagramSocket receiverSocket = new DatagramSocket(1234)) {
while (true) {
final DatagramPacket packet = new DatagramPacket(new byte[PACKET_LENGTH], PACKET_LENGTH);
// Recepción del paquete
receiverSocket.receive(packet);
// Conversión a mayúsculas
final String messageUpperCase = new String(packet.getData(), 0, packet.getLength()).toUpperCase();
packet.setData(messageUpperCase.getBytes());
// Reenvío del paquete
receiverSocket.send(packet);
}
} catch (final IOException e) {
System.err.println("Server socket could not be created");
}
}
}
/*-
* #%L
* Ejemplos de DAI - Sockets
* %%
* Copyright (C) 2014 - 2023 Miguel Reboiro Jato
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.sockets.example3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
public class EchoSender {
public static void main(String[] args) {
// final Console console = System.console();
final BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
try (DatagramSocket socket = new DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), 4444))) {
String line;
while ((line = console.readLine()) != null) {
System.out.println("INPUT: " + line);
final byte[] lineData = line.getBytes();
final DatagramPacket packet = new DatagramPacket(lineData, lineData.length, InetAddress.getLocalHost(), 1234);
// El paquete se envía
socket.send(packet);
// Se recibe sobre el mismo paquete. Los datos se sobreescriben
socket.receive(packet);
System.out.println("ECHO: " + new String(packet.getData()));
}
} catch (final UnknownHostException e) {
System.out.println("Unknown host: localhost");
} catch (final IOException e) {
System.out.println("Connection error: " + e.getMessage());
}
}
}
/*-
* #%L
* Ejemplos de DAI - Sockets
* %%
* Copyright (C) 2014 - 2023 Miguel Reboiro Jato
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.sockets.example3;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServer {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(1234)) {
while (true) {
try (Socket clientSocket = serverSocket.accept()) {
final DataInputStream input = new DataInputStream(clientSocket.getInputStream());
final DataOutputStream output = new DataOutputStream(clientSocket.getOutputStream());
String message;
while (!(message = input.readUTF()).equalsIgnoreCase("quit")) {
output.writeUTF(message.toUpperCase());
output.flush();
}
}
}
} catch (final IOException e) {
System.err.println("Server socket could not be created");
}
}
}
/*-
* #%L
* Ejemplos de DAI - Sockets
* %%
* Copyright (C) 2014 - 2023 Miguel Reboiro Jato
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.sockets.example4;
import java.io.Serializable;
public class Message implements Serializable {
private static final long serialVersionUID = 1L;
private final String subject;
private final String message;
public Message(String subject, String message) {
this.subject = subject;
this.message = message;
}
public String getSubject() {
return subject;
}
public String getMessage() {
return message;
}
}
/*-
* #%L
* Ejemplos de DAI - Sockets
* %%
* Copyright (C) 2014 - 2023 Miguel Reboiro Jato
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.sockets.example4;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class MessageReceiver {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket(1234)) {
final byte[] buffer = new byte[1500];
while (true) {
final DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
try {
final ByteArrayInputStream input = new ByteArrayInputStream(buffer);
final ObjectInputStream dataInput = new ObjectInputStream(input);
final Message message = (Message) dataInput.readObject();
System.out.println("SUBJECT: " + message.getSubject());
System.out.println("MESSAGE: " + message.getMessage());
} catch (final ClassNotFoundException e) {
e.printStackTrace();
}
}
} catch (final IOException ioe) {
System.out.println("Connection error: ");
System.out.println(ioe.getMessage());
}
}
}
/*-
* #%L
* Ejemplos de DAI - Sockets
* %%
* Copyright (C) 2014 - 2023 Miguel Reboiro Jato
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.sockets.example4;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Date;
public class MessageSender {
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket()) {
final ByteArrayOutputStream output = new ByteArrayOutputStream(1500);
final ObjectOutputStream dataOutput = new ObjectOutputStream(output);
final Message message = new Message("Time", "Current time: " + new Date());
System.out.println("SUBJECT: " + message.getSubject());
System.out.println("MESSAGE: " + message.getMessage());
dataOutput.writeObject(message);
final DatagramPacket packet = new DatagramPacket(
output.toByteArray(), output.size(), InetAddress.getLocalHost(), 1234
);
socket.send(packet);
} catch (final IOException ioe) {
ioe.printStackTrace();
System.out.print("Connection error: ");
System.out.println(ioe.getMessage());
}
}
}
/*-
* #%L
* Ejemplos de DAI - Sockets
* %%
* Copyright (C) 2014 - 2023 Miguel Reboiro Jato
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.sockets.example5;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class ESEIHomeDownload {
public static void main(String[] args) {
try {
final URL url = new URL("https://www.esei.uvigo.es");
final URLConnection connection = url.openConnection();
System.out.println(connection.getClass());
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
} catch (final MalformedURLException e) {
e.printStackTrace();
} catch (final IOException e) {
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