-2

This is the code I'm using to send an ArrayList from the server to the client (or Visa-Versa). It's not working, but I'm unsure of why. The error thrown is SocketClosed. Interfacer is a class that allows the user to decide to be a server or the client, and that is where the server or client is constructed. The calls below are called 60 times a second by each the server and the client. Server is extremely similar to the Client class.

(for the "duplicate thread" I couldn't find why it was closing, and was just a minor error of me forgetting to change the location of a piece of code as I was reusing the class)

used by Client-

Interfacer.getClient().sendArrayList(Game.troops);
ArrayList<Troops> array = Interfacer.getClient().getArrayList();
Game.troops = Utils.mend(Game.troops, array);

used by Server-

ArrayList<Troops> array = Interfacer.getServer().getArrayList();
Interfacer.getServer().sendArrayList(Game.troops);
Game.troops = Utils.mend(Game.troops, array);

The Client class:

package jandek.connections;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.*;
import java.net.*;
import java.util.ArrayList;

import javax.swing.*;

import jandek.main.Game;

public class Client extends JFrame{


private static final long serialVersionUID = 1L;
private ObjectOutputStream output;
private ObjectInputStream input;

private String serverIP;
private Socket connection;

JTextArea t;
JFrame f;

//constructor
public Client(String host){

    serverIP = host;

    f = new JFrame();
    f.getContentPane().setPreferredSize(new Dimension(300, 300));
    f.pack();

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null);
    t = new JTextArea();
    f.add(t, BorderLayout.CENTER);
    f.setVisible(true);




    try{
        connectToServer();
        setupStreams();
        new Game(1).start();
    }catch(EOFException eofException){
        //t.append("Connection was terminated");
    }catch(IOException ioException){
        ioException.printStackTrace();
    }
   // EDIT- this part needs to move to the Game.stop() method
    finally{
        closeConnection();
    }
}

public Client(){

    serverIP = "127.0.0.1";

    f = new JFrame();
    f.getContentPane().setPreferredSize(new Dimension(300, 300));
    f.pack();

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null);
    t = new JTextArea();
    f.add(t, BorderLayout.CENTER);
    f.setVisible(true);




    try{
        connectToServer();
        setupStreams();
        new Game(1).start();
    }catch(EOFException eofException){
        //t.append("Connection was terminated");
    }catch(IOException ioException){
        ioException.printStackTrace();
    }finally{
        closeConnection();
    }
}



//connect to server
private void connectToServer() throws IOException{
    t.append("Attempting connection...");
    connection = new Socket(InetAddress.getByName(serverIP), 6987);
    t.append("Connection Established! Connected to: " + connection.getInetAddress().getHostName());
}

//set up streams
private void setupStreams() throws IOException{
    output = new ObjectOutputStream(connection.getOutputStream());
    output.flush();
    input = new ObjectInputStream(connection.getInputStream());
    t.append(" The streams are now set up!");
    f.setVisible(false);
}


//Close connection
private void closeConnection(){
    //t.append(" Closing the connection!");
    try{
        output.close();
        input.close();
        connection.close();
    }catch(IOException ioException){
        ioException.printStackTrace();
    }
}

@SuppressWarnings("rawtypes")
public void sendArrayList(ArrayList array){
    try {
        output.writeUnshared(array);
        output.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@SuppressWarnings("rawtypes")
public ArrayList getArrayList(){
    try {
        return (ArrayList) input.readUnshared();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null; 
}

}
Dak31
  • 82
  • 8

1 Answers1

0

The exception thrown is actually SocketException: socket closed, and it means you have closed socket and then continued to use it.

Possibly you are unaware that closing the input or output stream of a socket closes the socket.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • you're right, I forgot to move the finally{ closeConnection();} since I copied the class from an instant messenger. But the issue I have now is the troop object is not sterilized. Specifically Java.Awt.Image.BufferedImage from it is not sterilized (as Troops implements Serializable). I don't want to have to rip apart the object and send each bit by itself and put it back together. Any ideas? – Dak31 May 29 '16 at 00:55
  • Do you mean 'not serialized'? 'Not serializable'? There are plenty of answers here about how to send a `BufferedImage`. – user207421 May 29 '16 at 01:01
  • I'll have to look into it more, one step at a time :). I really appreciate the help! – Dak31 May 29 '16 at 01:14