Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package Chat;
  6.  
  7. import java.io.*;
  8. import java.applet.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import java.net.*;
  12.  
  13. /**
  14.  *
  15.  * @author G
  16.  */
  17. public class Janela1 extends Frame //implements ActionListener
  18. {
  19.  
  20.     Button send;
  21.     TextField tf;
  22.     TextArea ta;
  23.     String str;
  24.  
  25.     public Janela1() {
  26.  
  27.         setLayout(null);
  28.         setTitle("ChatBox - G");
  29.         setBackground(Color.LIGHT_GRAY);
  30.         setForeground(Color.black);
  31.         setSize(500, 450);
  32.  
  33.         setVisible(true);
  34.  
  35.         tf = new TextField(20);
  36.         ta = new TextArea(10, 10);
  37.         send = new Button("ENVIAR");
  38.  
  39.         add(ta);
  40.         ta.setBounds(30, 50, 430, 300);
  41.         add(tf);
  42.         tf.setBounds(30, 380, 350, 40);
  43.         add(send);
  44.         send.setBounds(390, 380, 70, 40);
  45.         addWindowListener(new wh());
  46.  
  47.         send.addActionListener(new send());
  48.  
  49.         while (true) {
  50.             try {
  51.                 ServerSocket s = new ServerSocket(3000);
  52.                 Socket c = s.accept();
  53.                 BufferedReader b = new BufferedReader(new InputStreamReader(c.getInputStream()));
  54.                 str = b.readLine();
  55.                 ta.append("\n");
  56.                 ta.append(str);
  57.             } catch (Exception e) {
  58.             }
  59.         }
  60.     }
  61.  
  62.     class wh extends WindowAdapter {
  63.  
  64.         public void windowClosing(WindowEvent we) {
  65.             System.exit(0);
  66.         }
  67.     }
  68.  
  69.     class send implements ActionListener {
  70.  
  71.         public void actionPerformed(ActionEvent ae) {
  72.             String str1 = ae.getActionCommand();
  73.             if (str1.equals("ENVIAR")) {
  74.                 try {
  75.                     String s1 = tf.getText();
  76.  
  77.                     InetAddress address = InetAddress.getLocalHost();
  78.                     String mm = address.getHostName();
  79.  
  80.                     //    Se quiser utilizar um host, sΓ³ colocar o ip:
  81.                     //  String i="172.16.77.214;
  82.  
  83.                     String i = "localhost";
  84.                     Socket s = new Socket(i, 4000);
  85.                     PrintWriter out = null;
  86.                     ta.append("\n");
  87.                     ta.append(mm + " : " + s1);
  88.                     out = new PrintWriter(s.getOutputStream());
  89.                     out.println(mm + " : " + s1);
  90.                     out.flush();
  91.                 } catch (Exception e) {
  92.                 }
  93.  
  94.             }
  95.  
  96.         }
  97.     }
  98.  
  99.     public static void main(String args[]) throws IOException {
  100.  
  101.         Janela1 ff = new Janela1();
  102.  
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement