Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 G
- */
- public class Janela1 extends Frame //implements ActionListener
- {
- Button send;
- TextField tf;
- TextArea ta;
- String str;
- public Janela1() {
- setLayout(null);
- setTitle("ChatBox - G");
- setBackground(Color.LIGHT_GRAY);
- setForeground(Color.black);
- setSize(500, 450);
- setVisible(true);
- tf = new TextField(20);
- ta = new TextArea(10, 10);
- send = new Button("ENVIAR");
- add(ta);
- ta.setBounds(30, 50, 430, 300);
- add(tf);
- tf.setBounds(30, 380, 350, 40);
- add(send);
- send.setBounds(390, 380, 70, 40);
- 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(i, 4000);
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement