Funcionamiento (Captura) : Henry Pérez García Aplicaciones Distribuidas Medio Chat en Netbeans (Java)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Henry Pérez García

Aplicaciones Distribuidas
Medio Chat en NetBeans (Java)
Funcionamiento (CAPTURA)

URL del proyecto: https://bitbucket.org/Jenruco/henryperez-mediochat/src/master/


APLICACIÓN CLIENTE
Clase Cliente
public class Client {

public static void main(String[] args) {


// TODO code application logic here
ClientInterface client = new ClientInterface("Chat - Sockets
Java");
client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Clase ClientInterface
public class ClientInterface extends JFrame {
private final JLabel lblTitle, lblClient, lblIp, lblMessage,
lblMessages;
private final JTextField txtMessage, txtClient, txtIp;
private final JTextArea txtMessages;
private final JButton btnSubmit, btnLimpiar;
private final JPanel panel;

private Socket socketClient;

public ClientInterface(String title){


lblTitle = new JLabel("Chat (Cliente)");
lblClient = new JLabel("Nombrte: ");
lblIp = new JLabel("Ip: ");
lblMessage = new JLabel ("Mensaje: ");
lblMessages = new JLabel ("Chat: ");

txtMessage = new JTextField (25);


txtMessages = new JTextArea (10, 25);
txtClient = new JTextField (25);
txtIp = new JTextField (25);

btnSubmit = new JButton("Enviar");


btnLimpiar = new JButton("Limpiar");

btnSubmit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
//System.out.println("Hola funciona");
socketClient = new Socket("192.168.42.6", 5555);
try (DataOutputStream outputStream = new
DataOutputStream(socketClient.getOutputStream())) {
outputStream.writeUTF(txtMessage.getText());
txtMessages.setText(txtMessages.getText() +
"\n" + txtMessage.getText());
outputStream.close();
}
socketClient.close();
} catch (IOException ex) {
txtMessages.setText(txtMessages.getText() + "\n" +
txtMessage.getText() + "Message not sent.");
System.out.println(ex.getMessage());
}

}
});

panel = new JPanel();

panel.add(lblTitle);
panel.add(lblIp); panel.add(txtIp);
panel.add(lblClient); panel.add(txtClient);
panel.add(lblMessages); panel.add(txtMessages);
panel.add(lblMessage); panel.add(txtMessage);
panel.add(btnSubmit); panel.add(btnLimpiar);

this.setBounds(800, 350, 320, 410);


this.add(panel);
this.setVisible(true);
this.setTitle(title);
}
}

APLICACION SERVIDOR
Clase Server
public class Server {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
ServerInterface interfaceServer =
new ServerInterface("Chat - SocketServer Java");

interfaceServer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Clase ServerInterface
public final class ServerInterface extends JFrame {
private final JLabel lblTitle, lblMessages; /*, lblMessage*/
//private final JTextField txtClient, txtIp; //txtMessage
public static JTextArea txtMessages;
private final JButton btnLimpiar; //btnSubmit,
private final JPanel panel;
//private final Thread myThread;
//private MyThread myThread;
Hilo hilo;

public JTextArea getMenssages(){


return txtMessages;
}

public ServerInterface(String title){


lblTitle = new JLabel("Chat (Servidor)");
lblMessages = new JLabel ("Chat: ");
txtMessages = new JTextArea (10, 25);
btnLimpiar = new JButton("Limpiar");
panel = new JPanel();

panel.add(lblTitle);
panel.add(lblMessages); panel.add(txtMessages);
panel.add(btnLimpiar);

this.setBounds(800, 350, 320, 410);


this.add(panel);
this.setVisible(true);
this.setTitle(title);

hilo = new Hilo();


}
}

Clase Hilo
public class Hilo implements Runnable{

public Hilo() {
Thread myThread = new Thread (this);
myThread.start();
}

@Override
public void run() {
ServerSocket serverSocket;
Socket server;

try {
serverSocket = new ServerSocket(5555);
//System.out.println("Ya creó el ServerSocket");
while (true)
{
try{
server = serverSocket.accept();
//System.out.println("Ya aceptó algo el
servidor");
DataInputStream inputStream = new
DataInputStream(server.getInputStream());
txtMessages.setText(txtMessages.getText() + "\n" +
inputStream.readUTF());
System.out.println(inputStream.readUTF());
server.close();
}
catch (IOException ex){
System.out.println (ex.getMessage());
}
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}

You might also like