Socket with threads in Java

Asked

Viewed 123 times

2

I have this code that involves socket TCP and threads.

How do I get the server to send a message when the client connects? In this case the client sends the message and the result has to return to the client, and it does not return.

I tried but does not send, I already picked up some examples but by this code will not.

public class Cliente extends Thread {

    DataOutputStream saida;
    BufferedReader teclado;
    BufferedReader entrada;

    public static void main(String args[]) {

        try {

            Socket conexao = new Socket("localhost", 40000);
            System.out.println("Conectado ao servidor " + "localhost" + ", na porta: " + 40000);
            Thread t = new Cliente(conexao);
            t.start();

        } catch (IOException e) {
            System.out.println("IOException" + e);
        }
    }
    private Socket conexao;

    public Cliente(Socket s) {
        conexao = s;
    }

    public void run() {
        try {
            saida = new DataOutputStream(conexao.getOutputStream());

            teclado = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("Digite qualquer coisa");

            String digito = teclado.readLine();

            saida.writeUTF(digito);

            entrada = new BufferedReader(new InputStreamReader(conexao.getInputStream()));
            entrada.readLine();
        } catch (IOException e) {
            System.out.println("IOException" + e);
        } finally {
            try {
                conexao.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Server class:

public class Servidor extends Thread {

    public static void main(String args[]) {

        ServerSocket s = null;
        try {
            s = new ServerSocket(40000);
            System.out.println("       <<Servidor iniciado>>");
            System.out.println("<<Aguardando conexão na Porta 40000>>");
            while (true) {

                Socket conexao = s.accept();
                System.out.println("Conexao estabelecida com eleitor com o ip: " + conexao.getInetAddress().getHostAddress());
                Thread t = new Servidor(conexao);
                t.start();

            }
        } catch (IOException e) {
            System.out.println("IOException " + e);
        }
    }
    private Socket conexao;

    public Servidor(Socket s) {
        conexao = s;
    }

    public void run() {

        try {

            DataInputStream entrada = new DataInputStream(conexao.getInputStream());
            DataOutputStream saida = new DataOutputStream(conexao.getOutputStream());

            String clientedigitou = entrada.readUTF();
            System.out.println("Cliente Digitou: " + clientedigitou);
            saida.writeUTF(clientedigitou);




        } catch (IOException e) {
            System.out.println("IOException " + e);
        }

    }
}

1 answer

2


I think it’s a lack of giving flush after sending. For example:

saida.writeUTF(clientedigitou);
saida.flush();

It is necessary in this case for the message to be effectively sent.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.