Sending a Java Socket message

Asked

Viewed 36 times

0

Hello I am "trying" to make a server socket until there I got but I am not able to make the communication between the servers.

But the weirdest thing is the following check out the print below

Server socket

But what is happening instead the message come out in just one sentence is "flooding" the console with the letters. The message that should be sent to the server would be "Player" but the one that is returning "J" "O" "G" "A" "D" "O" "R"

I’m just starting this whole socket thing. 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

  1. Relate the item

    better than this?

  • 1

    Please provide a [mcve] so that we can execute the code and help solve the problem.

1 answer

0


Use comma friend, makes understanding easier. Anyway, why don’t you try using a Scanner to receive 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");

Browser other questions tagged

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