JRH
JRH

Reputation: 77

Client -Server Socket

I trying to implement a client server in java where I read a input in the client and do a UperCase in the server and returnt to the client and print the UperCase. I used an ObjectOutputStream and ObjectInputStream to read and write, but when I type a msg in my client my program shows me this errors:

Digite uma msg casa java.io.EOFException at java.io.DataInputStream.readUnsignedShort(Unknown Source) at java.io.ObjectInputStream$BlockDataInputStream.readUnsignedShort(Unknown Source) at java.io.ObjectInputStream$BlockDataInputStream.readUTF(Unknown Source) at java.io.ObjectInputStream.readUTF(Unknown Source) at Client.java.Client.main(Client.java:32)

My client side is this one :

package Client.java;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {

    public static void main(String[] args) {

        //estabelece conexao 
        // trocar msg com o server

        try {
            //cria conexao entre  client e servidor
            Socket socket = new Socket("localhost",5558);

            //cria input and output
            ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream input = new ObjectInputStream(socket.getInputStream());

            Scanner scan = new Scanner(System.in);
            System.out.println("Digite uma msg");
            String word = scan.nextLine();
            output.writeUTF(word);
            output.flush();
            word = input.readUTF();
            System.out.println("Upercase"+ word);

            input.close();
            output.close();
            socket.close();

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

and this is my server side:

package Server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.ServerSocket;

public class Servidor {

    private ServerSocket servidor;

    private void criarServerSocket(int porta) throws IOException {
        servidor = new ServerSocket(porta);
    }

    private Socket esperaConexao() throws IOException
    {
        Socket socket = servidor.accept();
        return socket;
    }

    private void fechaSocket(Socket s) throws IOException {
        s.close();
    }

    private void trataConexao(Socket socket) throws IOException {
        //*recebe e envia msg */

        try {
            ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream input = new ObjectInputStream(socket.getInputStream());

            String msg = input.readUTF();
            output.writeObject(msg.toUpperCase());
            output.flush();

            input.close();
            output.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }

        fechaSocket(socket);
    }

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        Servidor server = new Servidor();
        System.out.println("Wait for a connection");
        server.criarServerSocket(5558);
        System.out.println("Conectado ");
        Socket socket = server.esperaConexao();
        server.trataConexao(socket);
    }
}

Is it due my server is closing the stream ? and how I could fix it and still closing the stream? Thank you for any help.

Upvotes: 2

Views: 75

Answers (1)

Sarel Foyerlicht
Sarel Foyerlicht

Reputation: 927

you should use in Servidor

output.writeUTF(msg.toUpperCase());

and not

output.writeObject(msg.toUpperCase());

Upvotes: 1

Related Questions