Ver Mensaje Individual
  #1 (permalink)  
Antiguo 22/12/2008, 20:07
Canito07
 
Fecha de Ingreso: septiembre-2008
Mensajes: 32
Antigüedad: 15 años, 9 meses
Puntos: 0
ChatClient.java ChatServer.java Connection.java Hosts.txt

Muchachos, yo tengo estos 3 o 4 códigos.... Yo soy un principiante pero un amigo me los pasó estos códigos...... compilan y todo,,,,pero no entiendo bien cómo podría hacer para que me pueda mandar mensajes entre dos computadoras....

Primero, lo pienso probar entre dos computadoras que tengo acá en casa, pero no entiendo cómo hacerlo, alguien que me ayude!!

Tengo wireless, no sé si tiene algo que ver...


Saludos Genios!



Muchas gracias...

Les dejo los.....

Códigos......... unos entran acá, los otros los pongo en otro comentario, No es doble-post, NO ME ENTRAN!!!

GRACIAS!!
-------------------------------------------------------------------------------------------------------
ChatClient.java

Código:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class ChatClient {

  private Socket connection = null;
  private BufferedReader serverIn = null;
  private PrintStream serverOut = null;

  private TextArea output;
  private TextField input;
  private Button sendButton;
  private Button quitButton;
  private Frame frame;
  private Choice usernames;
  private Dialog aboutDialog;

  public ChatClient() {
    output = new TextArea(10,50);
    input = new TextField(50);
    sendButton = new Button("Send");
    quitButton = new Button("Quit");
    usernames = new Choice();
    usernames.add("Bryan Basham");
    usernames.add("Grand Poobah");
    usernames.add("Java Geek");
  }

  public void launchFrame() {
    frame = new Frame("Chat Room");

    // Use the Border Layout for the frame
    frame.setLayout(new BorderLayout());

    frame.add(output, BorderLayout.WEST);
    frame.add(input, BorderLayout.SOUTH);

    // Create the button panel
    Panel p1 = new Panel(); 
    p1.setLayout(new GridLayout(3,1));
    p1.add(sendButton);
    p1.add(quitButton);
    p1.add(usernames);

    // Add the button panel to the center
    frame.add(p1, BorderLayout.CENTER);

    // Create menu bar and File menu
    MenuBar mb = new MenuBar();
    Menu file = new Menu("File");
    MenuItem quitMenuItem = new MenuItem("Quit");
    quitMenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        exit();
      }
    });
    file.add(quitMenuItem);
    mb.add(file);
    frame.setMenuBar(mb);

    // Add Help menu to menu bar
    Menu help = new Menu("Help");
    MenuItem aboutMenuItem = new MenuItem("About");
    aboutMenuItem.addActionListener(new AboutHandler());
    help.add(aboutMenuItem);
    mb.setHelpMenu(help);

    // Attach listener to the appropriate components
    sendButton.addActionListener(new SendHandler());
    input.addActionListener(new SendHandler());
    frame.addWindowListener(new CloseHandler());
    quitButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          System.exit(0);
        }
    });

    frame.pack();
    frame.setVisible(true);

    doConnect();
  }

  private void doConnect() {
    // Initialize server IP and port information
    String serverIP = System.getProperty("serverIP", "127.0.0.1");
    String serverPort = System.getProperty("serverPort", "2000");

    try {
      // Create the connection to the chat server
      connection = new Socket(serverIP, Integer.parseInt(serverPort));

      // Prepare the input stream and store it in an instance variable
      InputStream is = connection.getInputStream();
      InputStreamReader isr = new InputStreamReader(is);
      serverIn = new BufferedReader(isr);

      // Prepare the output stream and store it in an instance variable
      serverOut = new PrintStream(connection.getOutputStream());

      // Launch the reader thread
      Thread t = new Thread(new RemoteReader());
      t.start();

    } catch (Exception e) {
      System.err.println("Unable to connect to server!");
      e.printStackTrace();
    }
  }

  private class SendHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String text = input.getText();
      text = usernames.getSelectedItem() + ": " + text + "\n";
      serverOut.print(text);
      input.setText("");
    }
  }

  private class CloseHandler extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
      exit();
    }
  }

  private void exit() {
    try {
      connection.close();
    } catch (Exception e) {
      System.err.println("Error while shutting down the server connection.");
    }
    System.exit(0);
  }

  private class AboutHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      // Create the aboutDialog when it is requested
      if ( aboutDialog == null ) {
        aboutDialog = new AboutDialog(frame, "About", true);
      }
      aboutDialog.setVisible(true);
    }
  }

  private class AboutDialog extends Dialog implements ActionListener  {
    public AboutDialog(Frame parent, String title, boolean modal) {
      super(parent,title,modal);
      add(new Label("The ChatClient is a neat tool that allows you to talk " +
                  "to other ChatClients via a ChatServer"),BorderLayout.NORTH);
      Button b = new Button("OK");
      add(b,BorderLayout.SOUTH);
      b.addActionListener(this);
      pack();
    }
    // Hide the dialog box when the OK button is pushed
    public void actionPerformed(ActionEvent e) {
      setVisible(false);
    }
  }

  private class RemoteReader implements Runnable {
    public void run() {
      try {
        while ( true ) {
          String nextLine = serverIn.readLine();
          output.append(nextLine + "\n");
        }
      } catch (Exception e) {
        System.err.println("Error while reading from server.");
        e.printStackTrace();
      }
    }
  }

  public static void main(String[] args) {
    ChatClient c = new ChatClient();
    c.launchFrame();
  }
}