Problems with sockets in java, how to send and receive messages simultaneously between client and server?

1

I'm working with sockets in java and I was able to find the following problem, writing the server class I sent a message to the client to read, so far, ok !! But when I try to send a message to the server from the client, I can not get the two messages to be read on both sides simultaneously!

What is the problem with these classes?

public class Cliente {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 5000);

        try (Scanner scanner = new Scanner(socket.getInputStream())) {
            System.out.println("Cliente : -- Qual a  mensagem?\n" + scanner.nextLine());
        }

        socket.getOutputStream().write("This is ridiculous!!".getBytes());
        socket.getOutputStream().flush();
    }
}

And in the Server class:

public class Servidor {

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

        ServerSocket server = new ServerSocket(5000);

        while (true) {
            Socket socket = server.accept();
            Scanner entrada = new Scanner(socket.getInputStream());

            try (PrintWriter w = new PrintWriter(socket.getOutputStream())) {
                w.println("Servidor: Java é uma boa linguagem!");
            }
            while (entrada.hasNextLine()) {
                System.out.println(entrada.nextLine());
            }

        }
     }
 }

What should I do? I've tried everything, but I could not get the server to write and read a message from the client and also the client to read and write a message to the server, in that order respectively!  Everything at once is possible?

The output of the Client class is as follows:

Cliente : -- Qual a  mensagem?
Servidor: Java é uma boa linguagem!
Exception in thread "main" java.net.SocketException: Socket is closed
    at java.net.Socket.getOutputStream(Socket.java:943)
    at aula.ari.teste3.Cliente.main(Cliente.java:21)
    
asked by anonymous 03.07.2016 / 22:57

2 answers

3

My main language is not Java, so I'll try to help you

So I was able to interpret your server code as it kills the client connection as soon as the first message is sent ("Server: Java is a good language!") whereas input.hasNextLine () is false at first , then the infinite loop is executed again, accepting a new connection and killing the existing one due to loss of connection scope.

It is necessary to manipulate your connection while it is active and respond as soon as a new message is received, which is not the case for your code.

Already in the client code you apparently tried to send a message before receiving something from the server, so it is necessary to review the order of this implementation to get the desired result. Only after making these adjustments can you interpret what was received between the connections and then respond accordingly.

I hope I have helped.

    
03.07.2016 / 23:45
1

This is because your port is closed when the client tries to do this.

You have a A good tutorial on Caelum that can help you, he has an example of Chat, which I believe is more or less what you want: #

04.07.2016 / 00:47