Primero la vista y el socket que envía los mensajes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chat; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.KeyEvent; | |
import java.awt.event.KeyListener; | |
import java.io.ObjectOutputStream; | |
import java.net.Socket; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTextArea; | |
public class chat extends JFrame implements ActionListener, KeyListener{ | |
public chat(int port1, int port2, int pos){ | |
this.port1=port1; | |
this.port2=port2; | |
this.pos=pos; | |
inicializar(); | |
entrada=new recepcion(this,port1); | |
entrada.start(); | |
} | |
private Socket cliente; | |
private ObjectOutputStream flujo_de_salida; | |
int port1, port2; | |
int pos; | |
recepcion entrada; | |
JTextArea history, message; | |
JScrollPane sp1, sp2; | |
JButton send; | |
JLabel gp; | |
public void inicializar(){ | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
setBounds(300*pos,300,300,300); | |
history=new JTextArea(); | |
sp1=new JScrollPane(history); | |
sp1.setBounds(10,10,265,180); | |
history.setEditable(false); | |
add(sp1); | |
message=new JTextArea(); | |
sp2=new JScrollPane(message); | |
sp2.setBounds(10,200,200,50); | |
message.addKeyListener(this); | |
add(sp2); | |
send=new JButton(">>"); | |
send.setBounds(220,200,54,49); | |
send.setEnabled(false); | |
send.addActionListener(this); | |
add(send); | |
gp=new JLabel(); | |
add(gp); | |
setVisible(true); | |
} | |
public void mensaje_Saliente(String ip, String mensaje){ | |
try{ | |
cliente = new Socket(ip, port2); | |
flujo_de_salida = new ObjectOutputStream(cliente.getOutputStream()); | |
flujo_de_salida.writeObject(mensaje); | |
cliente.close(); | |
} | |
catch(Exception ex){} | |
} | |
@Override | |
public void actionPerformed(ActionEvent ae) { | |
if(!message.getText().equalsIgnoreCase("")){ | |
mensaje_Saliente("localhost",message.getText()); | |
if(!history.getText().equalsIgnoreCase("")) | |
history.setText(history.getText()+"\n"+message.getText()); | |
else | |
history.setText(history.getText()+message.getText()); | |
message.setText(""); | |
send.setEnabled(false); | |
} | |
} | |
@Override | |
public void keyTyped(KeyEvent ke) { | |
check(); | |
} | |
@Override | |
public void keyPressed(KeyEvent ke) { | |
check(); | |
} | |
@Override | |
public void keyReleased(KeyEvent ke) { | |
check(); | |
} | |
public void check(){ | |
if(message.getText().equalsIgnoreCase("")) | |
send.setEnabled(false); | |
else | |
send.setEnabled(true); | |
} | |
public static void main(String[] args) { | |
chat chat1=new chat(9000,9001,1); | |
} | |
} |
Y ahora la clase que funciona comos servidor. Es un hilo que constantemente recibe y actualiza mensajes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chat; | |
import java.io.IOException; | |
import java.io.ObjectInputStream; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
public class recepcion extends Thread{ | |
chat cht; | |
int port; | |
public recepcion(chat cht, int port){ | |
this.cht=cht; | |
this.port=port; | |
try { | |
servidor = new ServerSocket(port); | |
} | |
catch(IOException ex ){ | |
System.out.println("puerto ocupado"); | |
} | |
} | |
private Socket socket; | |
private ServerSocket servidor; | |
private ObjectInputStream flujo_entrada; | |
@Override | |
public void run(){ | |
while(true){ | |
String mensaje = null; | |
try{ | |
socket =servidor.accept(); | |
flujo_entrada = new ObjectInputStream(socket.getInputStream()); | |
mensaje = (String) flujo_entrada.readObject(); | |
if(!cht.history.getText().equalsIgnoreCase("")) | |
cht.history.setText(cht.history.getText()+"\n"+mensaje); | |
else | |
cht.history.setText(cht.history.getText()+mensaje); | |
socket.close(); | |
} | |
catch(IOException | ClassNotFoundException ex){} | |
} | |
} | |
} |