Dak31 Dak31 - 6 months ago 26
Java Question

What am I doing wrong with sending information across sockets?

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;
}

}

EJP EJP
Answer

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.