Vincent G
Vincent G

Reputation: 361

TCP Server & Client : IOException raised when Server respond to Client

I'm trying to create an app (with a game) where the client can send to the Server some int values, and until a certain value is not reached, the client can exchange with the server, that sends values back.

My first class is the Server of the TCP app. Here I have a main() method, that runs until the game is over. a getMoveFromClient() method that runs to get the object from the client, and a sendRequestToClient(Game g, int reponse) that sends the object to the client. Here is the code :

public static void main(String[] args) {
    serverLife = true;
    cm = new ConnectionManagerServer(); // this one instanciates my Server options
    Game g;
    while (serverLife) { // boolean value that allows me to continue over
        g = getMoveFromClient(); // get data from client, here every thing is ok 
        sendRequestToClient(g, 1); // send data to client, here it crashes.
        serverLife = g.life; // the object has a parameter that tells if the value is reached or not. (end of the game)
    }
}

public static Game getMoveFromClient() {
    // this method get data from clients, and works fine.
}

Until here, everything is ok. But with this method, that send data to the client :

private static void sendRequestToClient(Game g, int reponse) {
    try {
        g.setResponse(reponse);
        OutputStream out = cm.socket.getOutputStream();
        try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
            oos.writeObject(g); 
            oos.flush();
            oos.close();
        }
    } catch (IOException ex) {
        System.out.println("OutpuStreamError : " + ex.getMessage());
    }
}

I have the following error : OutpuStreamError : Software caused connection abort: socket write error

On the other side, on the client side I have nearly the same code, that works fine until the object Game should be returned :

public static Game getRequestFromServer() {
    Game g = null;
    try {
        InputStream in = mc.socket.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(in);
        g = (Calcul) ois.readObject();
    } catch (IOException ex) {
        System.out.println("error reception" + ex.getMessage());
    } catch (ClassNotFoundException ex) {
        System.out.println("erreur lecture de l'objet" + ex.getMessage());
    }
    return jeu;
}

I have the following error : error reception : Socket is closed

I have a class for my Game object, and two others to deal with the connection ports and the sockets for the client and the server.

public ConnectionManagerServer() {
    try {

        this.serverPort = 6464;
        this.serverSocket = new ServerSocket(this.serverPort);
        this.socket = this.serverSocket.accept();
    } catch (IOException ex) {
        System.out.println("serverSocket probleme d'initialisation : " + ex.getMessage());
    }
}

and the second one :

 public ConnectionManagerClient() {
    try {
        this.hostAdress = InetAddress.getByName("localhost");             // adresse du serveur 
        this.serverPort = 6464;
        this.socket = new Socket(this.hostAdress, this.serverPort);

    } catch (UnknownHostException ex) {
        System.out.println("Erreur d'initalisation de l'adresse de l'hote : " + ex.getMessage());
    } catch (IOException ex) {
        System.out.println("Erreur d'initalisation de la connexion : " + ex.getMessage());

    }
}

What I don't understand, is that when I try to send from the client to the server, it works fine, and the server is able to read the object content, but when I try to send it from the server to the client, I cannot read the object from the server. Is it because I didn't open the socket ?

EDIT : Do I have to use accept() in my client class to get data from the server ? This is false.

Upvotes: 0

Views: 309

Answers (1)

user207421
user207421

Reputation: 311052

Closing the input stream or output stream of a socket closes the socket. Don't close the streams, just flush the object output stream. And use the same streams for the life of the socket, not new ones per message.

Using accept() in a client is a contradiction in terms.

Upvotes: 3

Related Questions