Funcionamiento (Captura) : Henry Pérez García Aplicaciones Distribuidas Medio Chat en Netbeans (Java)
Funcionamiento (Captura) : Henry Pérez García Aplicaciones Distribuidas Medio Chat en Netbeans (Java)
Funcionamiento (Captura) : Henry Pérez García Aplicaciones Distribuidas Medio Chat en Netbeans (Java)
Aplicaciones Distribuidas
Medio Chat en NetBeans (Java)
Funcionamiento (CAPTURA)
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;
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.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);
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;
panel.add(lblTitle);
panel.add(lblMessages); panel.add(txtMessages);
panel.add(btnLimpiar);
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());
}
}
}