Sending Java Socket message

0

Hello, I'm trying to make a server socket, but I'm not able to communicate between servers.

But the strangest is the following check out the print below

Butwhatishappeninginsteadthemessagecomesoutinjustonesentenceis"flooding" the console with the letters. The message that should be sent to the server would be "Player" but what is returning "J" "O" "G" "A" "D" "O" "R"

I'm new to this socket cast. The way I sent the message:

public void socketSendMensagem(String mensagem) {

    try {

        PrintStream saida = new PrintStream(cliente.getOutputStream());

        saida.println(mensagem);


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

What I had to do to get the full message:

        while(true) {
            System.out.println((char) cliente.getInputStream().read());
        }

There is another method

  • Relate the item

    Better than this?

  • asked by anonymous 24.06.2018 / 20:01

    1 answer

    0

    Use a comma, make it easier to understand. In any case, why not try using a Scanner to get the getInputStream from your socket? Ex:

    Scanner entrada =  New Scanner(cliente.getInputStream());
    System.out.println("Recebido: " + entrada.nextLine());
    System.out.println("Eu tenho um vídeo explicando uma aplicação que utilizei socket, porque não confere? Talvez ajude: https://youtu.be/RkH4DmYH0hI");
    
        
    26.06.2018 / 03:20