You are on page 1of 6

import java.io.*; import java.net.*; //////////////////////////////////////////////////////////////////// import java.awt.Color; import java.awt.Font; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.

ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.ScrollPaneConstants; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; import javax.swing.border.TitledBorder; import org.jvnet.substance.SubstanceLookAndFeel; import org.jvnet.substance.watermark.SubstanceImageWatermark; class Sockets_Receptor extends JFrame { private private private private private private private private private private private private JButton b1; JButton b2; JButton b3; JButton b4; JLabel l1; JTextField t1; JTextArea a1; JScrollPane scroll; JTextArea textarea1; JTextField textfield1; JPanel panel; JFrame frm;

//////////////envi del doc///////////////////////////////////////////////////////////////// String doc; int tamao; Socket skCliente; File archivo = null; FileReader fr = null; BufferedReader br = null;

// ///////////////conexion socket////////////////////////////////// static final String HOST = "192.168.1.10"; static final int PUERTO = 5000; /////////////////////////////////////////////////////////////////// public Sockets_Receptor(String titulo, int x, int y, int a, int b) { super(titulo); this.getContentPane().setLayout(null); this.setBounds(x, y, a, b); this.setVisible(true); l1 = new JLabel(""); l1.setText("SOCKET RECEPTOR"); l1.setBounds(120, 10, 520, 40); l1.setForeground(Color.GREEN); l1.setFont(new Font("Constantia", Font.BOLD, 38)); this.add(l1);

t1 = new JTextField(""); t1.setBounds(40, 60, 220, 30); t1.setFont(new Font("Constantia", Font.BOLD, 13)); this.add(t1); // this.add(a1); this.setLayout(null); a1 = new JTextArea(); scroll=new JScrollPane(a1); scroll.setBounds(new Rectangle(275, 60, 300, 390)); this.add(scroll); this.show(); a1.setLineWrap(true); b1 = new JButton("RECIBIR"); b1.setBounds(40, 180, 220, 50); b1.setFont(new Font("RECIBIR", Font.BOLD, 18)); b1.setEnabled(false); this.add(b1); b2 = new JButton("CONECTAR"); b2.setBounds(40, 120, 220, 50); b2.setFont(new Font("CONECTAR", Font.BOLD, 18)); this.add(b2);

b3 = new JButton("SALIR"); b3.setBounds(40, 240, 220, 50); b3.setFont(new Font("SALIR", Font.BOLD, 18)); this.add(b3); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //////////////leer el txt/////////// try { archivo = new File(t1.getText()); fr = new FileReader(archivo); br = new BufferedReader(fr); // Lectura del fichero String linea; while ((linea = br.readLine()) != null) a1.append(linea + "\n"); } catch (Exception e1) { //e1.printStackTrace(); } finally { // En el finally cerramos el fichero, para asegurarnos // que se cierra tanto si todo va bien como si salta // una excepcion. try { if (null != fr) { fr.close(); } } catch (Exception e2) { e2.printStackTrace(); } } } }); // ///////////////////////////////////////////// b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Recibir(); b2.setEnabled(false); b1.setEnabled(true); }

}); // ///////////////////////////////////////////// b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { conectar(); b2.setEnabled(false); b1.setEnabled(true); l1.setText("CONEXION ESTABLECIDA"); l1.setFont(new Font("", Font.BOLD, 30)); l1.setBackground(Color.LIGHT_GRAY); l1.setBounds(100, 20, 400, 40); } }); // ///////////////////////////////////////////// b3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(1); } }); /////////////////////////////////////////////////// } public void Recibir() { try { // //////////////////////////////////////////////////////////////// // InputStream aux = skCliente.getInputStream(); // DataInputStream entrada = new DataInputStream(aux ); // ///////////////////////////////////////77 DataInputStream entrada = new DataInputStream( skCliente.getInputStream()); doc = entrada.readUTF().toString(); tamao = entrada.readInt();

System.out.println("Cargando archivo " + doc); t1.setText("C:\\Users\\vick\\Desktop\\tele\\" + doc); FileOutputStream salida = new FileOutputStream("C:\\Users\\vick\\Desktop\\tele\\" + doc); BufferedOutputStream out = new BufferedOutputStream(salida); BufferedInputStream in = new BufferedInputStream( skCliente.getInputStream()); byte[] buffer = new byte[tamao]; for (int i = 0; i < buffer.length; i++) { buffer[i] = (byte) in.read(); } out.write(buffer); out.flush(); in.close(); out.close(); skCliente.close(); l1.setText(" DOCUMENTO RECIBIDO "); l1.setFont(new Font("", Font.BOLD, 32)); l1.setBackground(Color.cyan); l1.setBounds(120, 20, 400, 40); System.out.println("Documento Recibido Correctamente " + doc); } catch (IOException e) { } } public void conectar(){ try { skCliente = new Socket(HOST, PUERTO); // skCliente.setSoTimeout(2000); skCliente.setKeepAlive(true); System.out.println("CONEXION EXITOSA"); } catch (Exception e) { // System.out.println( e.getMessage() ); System.out.println("Recibiendo " + e.toString()); } } public static void main(String[] arg) {

JFrame.setDefaultLookAndFeelDecorated(true); SubstanceLookAndFeel.setSkin("org.jvnet.substance.skin.FindingNemoSkin"); SubstanceLookAndFeel.setCurrentTheme( "org.jvnet.substance.skin.EmeraldDu skSkin" ); SubstanceLookAndFeel.setCurrentWatermark(new SubstanceImageWatermark("1.jpg")); new Sockets_Receptor(null, 190, 10, 610, 500).show();

} }

You might also like