Unconfigured Ad Widget

Collapse

Anúncio

Collapse
No announcement yet.

Simples MSN (Feito em JAVA)

Collapse
X
 
  • Filter
  • Tempo
  • Show
Clear All
new posts

  • Font Size
    #1

    Simples MSN (Feito em JAVA)

    Bom pessoal, to postando para vocês um exemplo bem simples de como fazer um MSN utilizando Socket's em JAVA!

    MODO DE UTILIZAR:
    só basta executar as duas classes no mesmo computador.


    Janela1.java

    Código PHP:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package Chat;

    import java.io.*;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;

    /**
     *
     * @author ArthurPaixao
     */
    public class Janela1 extends Frame //implements ActionListener
    {

        
    Button send;
        
    TextField tf;
        
    TextArea ta;
        
    String str;

        public 
    Janela1() {

            
    setLayout(null);
            
    setTitle("ChatBox - ArthurPaixão");
            
    setBackground(Color.LIGHT_GRAY);
            
    setForeground(Color.black);
            
    setSize(500450);

            
    setVisible(true);

            
    tf = new TextField(20);
            
    ta = new TextArea(1010);
            
    send = new Button("ENVIAR");

            
    add(ta);
            
    ta.setBounds(3050430300);
            
    add(tf);
            
    tf.setBounds(3038035040);
            
    add(send);
            
    send.setBounds(3903807040);
            
    addWindowListener(new wh());

            
    send.addActionListener(new send());

            while (
    true) {
                try {
                    
    ServerSocket s = new ServerSocket(3000);
                    
    Socket c s.accept();
                    
    BufferedReader b = new BufferedReader(new InputStreamReader(c.getInputStream()));
                    
    str b.readLine();
                    
    ta.append("\n");
                    
    ta.append(str);
                } catch (
    Exception e) {
                }
            }
        }

        class 
    wh extends WindowAdapter {

            public 
    void windowClosing(WindowEvent we) {
                
    System.exit(0);
            }
        }

        class 
    send implements ActionListener {

            public 
    void actionPerformed(ActionEvent ae) {
                
    String str1 ae.getActionCommand();
                if (
    str1.equals("ENVIAR")) {
                    try {
                        
    String s1 tf.getText();

                        
    InetAddress address InetAddress.getLocalHost();
                        
    String mm address.getHostName();

                        
    //    Se quiser utilizar um host, só colocar o ip:
                        //  String i="172.16.77.214;

                        
    String i "localhost";
                        
    Socket s = new Socket(i4000);
                        
    PrintWriter out null;
                        
    ta.append("\n");
                        
    ta.append(mm " : " s1);
                        
    out = new PrintWriter(s.getOutputStream());
                        
    out.println(mm " : " s1);
                        
    out.flush();
                    } catch (
    Exception e) {
                    }

                }

            }
        }

        public static 
    void main(String args[]) throws IOException {

            
    Janela1 ff = new Janela1();

        }

    Janela2.java
    Código PHP:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package Chat;

    /**
     *
     * @author ArthurPaixao
     */
    import java.io.*;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;

    public class 
    Janela2 extends Frame //implements ActionListener
    {

        
    Button send;
        
    TextField tf;
        
    TextArea ta;
    //System.out.println(address);
    //String s=new Integer(address).toString();
    //ta.setText(address);
        
    String str;

        public 
    Janela2() {

            
    setLayout(null);
            
    setTitle("ChatBox - ArthurPaixão");
            
    setBackground(Color.BLACK);
            
    setForeground(Color.BLUE);
            
    setSize(500450);
            
    setVisible(true);

            
    tf = new TextField(20);
            
    ta = new TextArea(1010);
            
    send = new Button("ENVIAR");

            
    add(ta);
            
    ta.setBounds(3050430300);
            
    add(tf);
            
    tf.setBounds(3038035040);
            
    add(send);
            
    send.setBounds(3903807040);
            
    addWindowListener(new wh());

            
    send.addActionListener(new send());

            while (
    true) {
                try {
                    
    ServerSocket s = new ServerSocket(4000);
                    
    Socket c s.accept();
                    
    BufferedReader b = new BufferedReader(new InputStreamReader(c.getInputStream()));
                    
    str b.readLine();
                    
    ta.append("\n");
                    
    ta.append(str);

                } catch (
    Exception e) {
                }

            }
        }

        class 
    wh extends WindowAdapter {

            public 
    void windowClosing(WindowEvent we) {
                
    System.exit(0);
            }
        }

        class 
    send implements ActionListener {

            public 
    void actionPerformed(ActionEvent ae) {
                
    String str1 ae.getActionCommand();
                if (
    str1.equals("ENVIAR")) {
                    try {
                        
    String s1 tf.getText();
                        
    InetAddress address InetAddress.getLocalHost();
                        
    String mm address.getHostName();
                        
    String i "localhost";

                        
    Socket s = new Socket(i3000);
                        
    PrintWriter out null;
                        
    ta.append("\n");
                        
    ta.append(mm " : " s1);
                        
    out = new PrintWriter(s.getOutputStream());
                        
    out.println(mm " : " s1);
                        
    out.flush();
                    } catch (
    Exception e) {
                    }
                }
            }
        }

        public static 
    void main(String args[]) throws IOException {
            
    Janela2 ff = new Janela2();

        }

    ~# Criado pela [IN]Segurança #~

  • Font Size
    #2
    O programa não envia a mensagem.

    Comment


    • Font Size
      #3
      Duvida

      cara fico muito bom seu programa, mas eu queria saber como eu faço se eu tiver que rodar ele em 2 maquinas diferentes, no caso o seu esta dizendo que eles tem que ser executados na mesma maquina certo?
      se eu precisar fazer em maquinas diferentes como faço?
      pode me ajudar?
      desde ja agradeço pela colaboração!

      Comment

      X
      Working...
      X