Ya se encuentra disponible la plataforma Git en producción. Se recomienda migrar sus proyectos ASAP. GitBETA será cerrado a partir del segundo semestre de 2020.

Para guías de cómo migrar sus proyectos, véase la documentación de Gitlab sobre Importar/Exportar proyectos

Ejercicio3 completo

parent 2970e968
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package excercise3;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JOptionPane;
/**
*
* @author Miguel Angel Egoavil Mathison Carne: B92695
*/
public class Client{
private DataOutputStream output;
private DataInputStream input;
private Socket client;
private final String HOST = "127.0.0.1";
private final int PORT = 1334;
public void runClient() {
try {
connectToServer();
getStream();
processConnection();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
closeConnection();
}
}
private void connectToServer() throws IOException {
System.out.println("Intentando conectar\n");
client = new Socket(HOST, PORT);
System.out.println("Conectado a: " + client.getInetAddress().getHostName());
}
private void getStream() throws IOException {
output = new DataOutputStream(client.getOutputStream());
output.flush();
input = new DataInputStream(client.getInputStream());
}
private void processConnection() throws IOException {
String message = "";
int answer = 0;
for (int i = 0; i < 5; i++) {
//Cliente lee
System.out.println("Esperando mensaje de servidor...");
message = input.readUTF();
System.out.println("Recibido");
//Cliente escribe
answer = Integer.parseInt(JOptionPane.showInputDialog(null, message));
output.writeInt(answer);
System.out.println("Enviado");
}
}
private void closeConnection() {
System.out.println("\nCerrando Conexión");
try {
output.close();
input.close();
client.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new Client().runClient();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package excercise3;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Miguel Angel Egoavil Mathison Carne: B92695
*/
public class Connection extends Thread {
private Socket connection1;
private Socket connection2;
private DataOutputStream output1;
private DataInputStream input1;
private DataOutputStream output2;
private DataInputStream input2;
private final static String C1 = "Cliente 1: ";
private final static String C2 = "Cliente 2: ";
public Connection(Socket connection1, Socket connection2) {
this.connection1 = connection1;
this.connection2 = connection2;
}
private void getStreams() throws IOException {
output1 = new DataOutputStream(connection1.getOutputStream());
output1.flush();
input1 = new DataInputStream(connection1.getInputStream());
output2 = new DataOutputStream(connection2.getOutputStream());
output2.flush();
input2 = new DataInputStream(connection2.getInputStream());
}
private void processConnection() throws IOException {
String message = "";
int result, n1, n2, answer, answer2;
result = n1 = n2 = answer = answer2 = 0;
boolean repeat = true;
while (repeat) {
//Definición de operación
n1 = (int) (Math.random() * 100 + 1);
n2 = (int) (Math.random() * 100 + 1);
result = n1 * n2;
message = n1 + " x " + n2;
//Simultaneo
//Turno cliente 1
output1.writeUTF(C1 + message);
System.out.println("Enviado");
output2.writeUTF(C2 + message);
System.out.println("Enviado");
System.out.println("Esperando respuesta de cliente 1...");
answer = input1.readInt();
System.out.println("Recibido");
//Turno cliente 2
System.out.println("Esperando respuesta de cliente 2...");
answer2 = input2.readInt();
System.out.println("Recibido");
/*
//Por turnos
//Turno cliente 1
output1.writeUTF(C1 + message);
System.out.println("Enviado");
System.out.println("Esperando respuesta de cliente 1...");
answer = input1.readInt();
System.out.println("Recibido");
//Turno cliente 2
output2.writeUTF(C2 + message);
System.out.println("Enviado");
System.out.println("Esperando respuesta de cliente 2...");
answer2 = input2.readInt();
System.out.println("Recibido");
*/
//Determinar ganador
if (answer == result || answer2 == result) {
if (answer != result) {
repeat = false;
JOptionPane.showMessageDialog(null, "Ha ganado el Cliente 2");
} else if (answer2 != result) {
repeat = false;
JOptionPane.showMessageDialog(null, "Ha ganado el Cliente 1");
}
}
}
}
private void closeConnection() {
System.out.println("\nTerminando Conexión");
try {
output1.close();
input1.close();
connection1.close();
output2.close();
input2.close();
connection2.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void run() {
try {
getStreams();
processConnection();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} finally {
closeConnection();
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package excercise3;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author Miguel Angel Egoavil Mathison Carne: B92695
*/
public class Server {
private ServerSocket server;
private Socket connection;
private final int PORT = 1334;
private DataOutputStream output;
private DataInputStream input;
private int numberConnection = 0;
public void runServer() {
try {
server = new ServerSocket(PORT);
waitForConnection();
Socket c1 = this.connection;
waitForConnection();
Socket c2 = this.connection;
new Connection(c1, c2).start();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
closeConnection();
}
}
private void waitForConnection() throws IOException {
System.out.println("Server esperando por conexión (cliente " + (++numberConnection) + ")...\n");
connection = server.accept();
System.out.println("\nConexión recibida de: " + connection.getInetAddress().getHostName());
}
private void closeConnection() {
System.out.println("\nTerminando Conexión");
try {
server.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new Server().runServer();
}
}
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