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

Commit 733773bc authored by TheMikox16's avatar TheMikox16

Finiquitado la mayoria del lab 3

parent 3569419c
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JDialogFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="2"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<Events>
<EventHandler event="windowClosed" listener="java.awt.event.WindowListener" parameters="java.awt.event.WindowEvent" handler="formWindowClosed"/>
</Events>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="19" max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" max="-2" attributes="0">
<Component id="send_bt" min="-2" max="-2" attributes="0"/>
<Component id="label" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="jScrollPane1" alignment="0" pref="305" max="32767" attributes="0"/>
<Component id="txt" alignment="0" max="32767" attributes="0"/>
</Group>
<EmptySpace pref="21" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="17" max="-2" attributes="0"/>
<Component id="label" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="jScrollPane1" min="-2" pref="265" max="-2" attributes="0"/>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Component id="txt" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="13" max="-2" attributes="0"/>
<Component id="send_bt" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="11" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="javax.swing.JScrollPane" name="jScrollPane1">
<AuxValues>
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
</AuxValues>
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JTextArea" name="mes_txf">
<Properties>
<Property name="editable" type="boolean" value="false"/>
<Property name="columns" type="int" value="20"/>
<Property name="rows" type="int" value="5"/>
</Properties>
</Component>
</SubComponents>
</Container>
<Component class="javax.swing.JLabel" name="label">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="SansSerif" size="14" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="Client"/>
</Properties>
</Component>
<Component class="javax.swing.JButton" name="send_bt">
<Properties>
<Property name="text" type="java.lang.String" value="Enviar"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="send_btActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JTextField" name="txt">
</Component>
</SubComponents>
</Form>
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JDialogFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="2"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<Events>
<EventHandler event="windowClosed" listener="java.awt.event.WindowListener" parameters="java.awt.event.WindowEvent" handler="formWindowClosed"/>
</Events>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="19" max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" max="-2" attributes="0">
<Component id="send_bt" min="-2" max="-2" attributes="0"/>
<Component id="label" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="jScrollPane1" alignment="0" pref="305" max="32767" attributes="0"/>
<Component id="txt" alignment="0" max="32767" attributes="0"/>
</Group>
<EmptySpace pref="21" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="17" max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Group type="102" attributes="0">
<Component id="label" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="jScrollPane1" min="-2" pref="265" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="43" max="-2" attributes="0"/>
</Group>
<Component id="txt" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="send_bt" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="11" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="javax.swing.JScrollPane" name="jScrollPane1">
<AuxValues>
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
</AuxValues>
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JTextArea" name="mes_txf">
<Properties>
<Property name="editable" type="boolean" value="false"/>
<Property name="columns" type="int" value="20"/>
<Property name="rows" type="int" value="5"/>
</Properties>
</Component>
</SubComponents>
</Container>
<Component class="javax.swing.JLabel" name="label">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="SansSerif" size="14" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="Server"/>
</Properties>
</Component>
<Component class="javax.swing.JButton" name="send_bt">
<Properties>
<Property name="text" type="java.lang.String" value="Enviar"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="send_btActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JTextField" name="txt">
</Component>
</SubComponents>
</Form>
/*
* 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 excercise2;
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 connection;
private DataOutputStream output;
private DataInputStream input;
public Connection(Socket connection) {
this.connection = connection;
}
private void getStreams() throws IOException {
output = new DataOutputStream(connection.getOutputStream());
output.flush();
input = new DataInputStream(connection.getInputStream());
}
private void processConnection() throws IOException {
String message = "";
int correct, incorrect, result, n1, n2, answer;
correct = incorrect = result = n1 = n2 = answer = 0;
for (int i = 0; i < 5; i++) {
//Servidor Escribe
n1 = (int) (Math.random() * 100 + 1);
n2 = (int) (Math.random() * 100 + 1);
result = n1 * n2;
message = n1 + " x " + n2;
output.writeUTF(message);
System.out.println("Enviado");
//Servidor Lee
System.out.println("Esperando respuesta de cliente...");
answer = input.readInt();
System.out.println("Recibido");
if(answer == result){
correct++;
}else{
incorrect++;
}
}
JOptionPane.showMessageDialog(null, "Correctas: " + correct
+ "\nIncorrectas: " + incorrect);
}
private void closeConnection(){
System.out.println("\nTerminando Conexión");
try{
output.close();
input.close();
connection.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();
}
}
}
......@@ -10,36 +10,28 @@ import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
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 Server extends Thread{
public class Server {
private ServerSocket server;
private Socket connection;
private final int PORT = 1334;
private DataOutputStream output;
private DataInputStream input;
private static final int N = 3;
public Server(Socket connection) {
this.connection = connection;
}
public Server() {
}
public void runServer() {
try {
System.out.println("Numero máximo de conexiones: 3");
System.out.println("Numero máximo de conexiones: " + N);
server = new ServerSocket(PORT);
for(int i = 0; i < 3; i++){
for(int i = 0; i < N; i++){
waitForConnection();
new Server(connection).start();
new Connection(connection).start();
}
} catch (IOException ex) {
ex.printStackTrace();
......@@ -54,47 +46,9 @@ public class Server extends Thread{
System.out.println("\nConexión recibida de: " + connection.getInetAddress().getHostName());
}
private void getStreams() throws IOException {
output = new DataOutputStream(connection.getOutputStream());
output.flush();
input = new DataInputStream(connection.getInputStream());
}
private void processConnection() throws IOException {
String message = "";
int correct, incorrect, result, n1, n2, answer;
correct = incorrect = result = n1 = n2 = answer = 0;
for (int i = 0; i < 5; i++) {
//Servidor Escribe
n1 = (int) (Math.random() * 100 + 1);
n2 = (int) (Math.random() * 100 + 1);
result = n1 * n2;
message = n1 + " x " + n2;
output.writeUTF(message);
System.out.println("Enviado");
//Servidor Lee
System.out.println("Esperando respuesta de cliente...");
answer = input.readInt();
System.out.println("Recibido");
if(answer == result){
correct++;
}else{
incorrect++;
}
}
JOptionPane.showMessageDialog(null, "Correctas: " + correct
+ "\nIncorrectas: " + incorrect);
}
private void closeConnection(){
private void closeConnection(){
System.out.println("\nTerminando Conexión");
try{
output.close();
input.close();
connection.close();
server.close();
}catch (IOException ex){
ex.printStackTrace();
......@@ -105,13 +59,4 @@ private void closeConnection(){
new Server().runServer();
}
public void run(){
try {
getStreams();
processConnection();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/*
* 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) {