How to only send messages to other clients?

2

I have a question with java sockets. My code sends the messages to all clients, including what they send. I want it to send only to other customers. Can you do that? I test using the telnet 127.0.0.1 2015 command on the terminal.

Customer

package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Clientes implements Runnable {

    public Socket cliente;

    public Clientes(Socket cliente) {
        this.cliente = cliente;
    }

    public void run() {

        try {
            PrintWriter out = new PrintWriter(cliente.getOutputStream(), true);
            out.write("---Seja Bem Vindo---\n");
            out.flush();

            System.out.println("Nova conexao: "
                    + this.cliente.getInetAddress().getHostAddress());

            while (true) {
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        cliente.getInputStream()));
                String veioDoCliente = in.readLine();

                if(veioDoCliente.equalsIgnoreCase("SAIR")){
                    cliente.close();
                    break;
                }
                System.out.println("MSG vinda do cliente " + veioDoCliente);

                for (Clientes writer : Servidor.clientes) {
                    PrintWriter out2 = new PrintWriter(writer.cliente.getOutputStream(), true);
                    out2.write("teste:"+veioDoCliente+"\n");
                    out2.flush();

                }


                //s.close();
                 //this.cliente.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Server

package socket;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Servidor {
    public Socket cliente;

    public Servidor(Socket cliente) {
        this.cliente = cliente;
    }


    public static List<Clientes> clientes = new ArrayList<Clientes>();

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

        ServerSocket servidor = new ServerSocket(2015);

        System.out.println("Esperando alguem se conectar...");

        while (true) {
            Socket cliente = servidor.accept();

            Clientes tratamento = new Clientes(cliente);

            clientes.add(tratamento);

            Thread t = new Thread(tratamento);
            t.start();
        }
    }

}
    
asked by anonymous 23.09.2015 / 20:15

1 answer

4

You send the messages in this code:

for (Clientes writer : Servidor.clientes) {
    PrintWriter out2 = new PrintWriter(writer.cliente.getOutputStream(), true);
    out2.write("teste:"+veioDoCliente+"\n");
    out2.flush();
}

For each instance of Clientes (called writer ), you send the generated message to the current instance (which is an instance of Clientes ) - so you can compare writer to the current instance: / p>

if (writer != this) {
    // ... código exposto acima
}

"If writer is not myself, send the message."

I answered a question, including an example that contained exactly what you are doing here. I suggest you take a look - can serve you for something .

  

Suggestion:

     

In this loop , use cliente instead of writer , and when instantiating Socket in class Servidor , also call socket . >

So when you write:

new PrintWriter(writer.cliente.getOutputStream(), true);

It will look like this:

new PrintWriter(cliente.socket.getOutputStream(), true);

I think it's easier to understand.

    
22.10.2015 / 09:19