Ahora bien, nesesito user Thread porque se pueden llegar a conectar muchos, y se me complica para enviar los datos porque el metodo enviarDatos que esta dentro de la clase User que es cuando ya establesco una conexion con el usuario, es el que envia las cadenas, pero yo el boton enviar lo apreto y obtengo el string en el cliente (donde esta la interfaz), y no se como hacer para poder usar el metodo ese siendo que puede llegar a haber muchos users.....pero en un primer momento no se como hacer ni para uno solo:
Les dejo un pedazo de codigo para que no se pierdan
Código
class User extends Thread{
private Socket socket = null;
private Cliente cliente = null;
private int id = 0;
private ObjectOutputStream salida;
private ObjectInputStream entrada;
private String mensaje="";
public User(Cliente cliente,Socket socket,int id){
this.cliente = cliente;
this.socket = socket;
this.id = id;
}
@Override
public synchronized void run(){
try {
obtenerFlujos();
}
catch (EOFException excepcionEOF) {
System.err.println("Se desconecto alguien");
}
catch (Exception err) {}
finally {
cerrarConexion();//todo
}
}
private void obtenerFlujos() throws IOException{
salida = new ObjectOutputStream(socket.getOutputStream());
salida.flush();
entrada = new ObjectInputStream(socket.getInputStream());
System.out.println("Se conecto alguien");
}
public void enviarDatos( String mensaje ) {
try {
salida.writeObject(mensaje);
salida.flush();
System.out.println("Se envio: "+ mensaje);
}
catch (IOException e) {
System.err.println("Error al escribir objeto");
}
}
}
class socket extends ServerSocket implements Runnable{
private Socket socket = null;
private Cliente cliente = null;
public socket(int port, int backlog,Cliente cliente) throws IOException {
super(port, backlog);
this.cliente = cliente;
}
@Override
public synchronized void run() {
int n=0;
while(true){
try {
this.socket = accept();
User user = new User(cliente,socket,n);
user.start();
}catch(IOException e){}
}
}
}
public class Cliente extends JFrame implements ActionListener {
private socket socket = null;
@Override
public void actionPerformed(ActionEvent e) {
Object evt = e.getSource();
if(evt==buttonInit) {
try {
socket = new socket(Integer.parseInt(textFieldPort.getText()),100,this);
new Thread(socket).start();
}
}catch(IOException err){}
}
if(evt==botonComando){
user.enviarDatos(comando.getText()); //Aca no se como hacer para enviar un string a la otra persona.......
}
}
public Cliente(){
CrearInterfaz();
getContentPane().add(panel,BorderLayout.NORTH);
getContentPane().add(scrollPane,BorderLayout.CENTER);
}
public static void main(String[] args){
new Cliente();
}
private JTextField comando = null;
private JButton botonComando = null;
private JPanel panel = null;
private JScrollPane scrollPane = null;
private void CrearInterfaz(){
this.comando = new JTextField(40);
this.botonComando = new JButton("Enviar");
this.panel.add(comando);
this.panel.add(botonComando);
this.botonComando.addActionListener(this);
}
}
Haber si a alguien se le ocurre algo










Autor


En línea




