› Foros › PC › Software libre
package clienteudp;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import javax.imageio.ImageIO;
/**
*
* @author rocio
*/
public class Formulario extends javax.swing.JFrame implements Runnable{
/** Creates new form Formulario */
MulticastSocket s;
InetAddress group;
DatagramPacket dgp;
DatagramPacket paquete;
byte[] datos = null;
public Formulario() {
initComponents();
}
public void run()
{
try{
s = new MulticastSocket (5000);
//Configuramos el grupo (IP) a la que nos conectaremos:
group = InetAddress.getByName ("231.0.0.1");
//Nos unimos al grupo:
s.joinGroup (group);
String salida = new String();
byte [] buffer = new byte [256];
while(!salida.equals("salir"))
{
// Los paquetes enviados son de 256 bytes de maximo
//(es adaptable)
//Creamos el datagrama en el que recibiremos el paquete
//del socket:
dgp = new DatagramPacket (buffer, buffer.length);
// Recibimos el paquete del socket:
s.receive (dgp);
// Adaptamos la información al tamaño de lo que se envió
//(por si se envió menos de 256):
byte [] buffer2 = new byte [dgp.getLength ()];
// Copiamos los datos en el nuevo array de tamaño adecuado:
System.arraycopy (dgp.getData (),0,buffer2,0,dgp.getLength ());
//Vemos los datos recibidos por pantalla:
System.out.println(buffer);
if(ImageIO.read(new ByteArrayInputStream(buffer)) != null)
{
BufferedImage image = ImageIO.read(new ByteArrayInputStream(buffer2));
ImageIO.write(image, "png", new File("C:\\Users\\edu\\Pictures\\pruebaImagen.png"));
}
else
{
salida = new String (buffer2);
this.txtConversacion.append(salida+"\n");
}
}
}//try
catch(Exception ex)
{
System.err.println(ex.toString());
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
btnEnviar = new javax.swing.JButton();
txtTexto = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
txtConversacion = new javax.swing.JTextArea();
btnEnviarImagen = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Cliente UDP");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosed(java.awt.event.WindowEvent evt) {
formWindowClosed(evt);
}
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel1.setFont(new java.awt.Font("Bell MT", 0, 14)); // NOI18N
jLabel1.setText("Introduce el Texto a enviar");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 30, -1, -1));
btnEnviar.setFont(new java.awt.Font("Bell MT", 0, 14)); // NOI18N
btnEnviar.setText("Enviar");
btnEnviar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEnviarActionPerformed(evt);
}
});
getContentPane().add(btnEnviar, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 290, -1, -1));
txtTexto.setFont(new java.awt.Font("Bell MT", 0, 14)); // NOI18N
getContentPane().add(txtTexto, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 60, 420, -1));
txtConversacion.setColumns(20);
txtConversacion.setFont(new java.awt.Font("Monospaced", 0, 14)); // NOI18N
txtConversacion.setRows(5);
jScrollPane1.setViewportView(txtConversacion);
getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 90, 420, 190));
btnEnviarImagen.setText("Enviar imagen");
btnEnviarImagen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEnviarImagenActionPerformed(evt);
}
});
getContentPane().add(btnEnviarImagen, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 290, -1, -1));
pack();
}// </editor-fold>
private void formWindowOpened(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
}
private void formWindowClosed(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
try{
//Salimos del grupo multicast
s.leaveGroup (group);
// Cerramos el socket:
s.close ();
}
catch(Exception e)
{
}
}
private void btnEnviarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
String texto = this.txtTexto.getText();
datos = texto.getBytes();
System.out.println("Datos a enviar del texto "+datos);
paquete = new DatagramPacket(datos, datos.length, group,5000);
// Se envía el paquete con la fecha y la hora
s.send( paquete);
}
catch(Exception ex){
}
}
private void btnEnviarImagenActionPerformed(java.awt.event.ActionEvent evt) {
try {
BufferedImage bi = ImageIO.read(new File("C:\\Users\\edu\\Pictures\\taller.png"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "png", baos);
baos.flush();
datos = baos.toByteArray();
baos.close();
paquete = new DatagramPacket(datos, datos.length, group,5000);
s.send(paquete);
} catch (IOException ex) {
System.out.println("Error al enviar imagen "+ex.getMessage());
}
}
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JButton btnEnviar;
private javax.swing.JButton btnEnviarImagen;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea txtConversacion;
private javax.swing.JTextField txtTexto;