跨套接字发送信息我做错了什么?

What am I doing wrong with sending information across sockets?

这是我用来从服务器向客户端(或 Visa-Versa)发送 ArrayList 的代码。它不起作用,但我不确定为什么。抛出的错误是 SocketClosed。 Interfacer是一个class,让用户自己决定是服务器还是客户端,也就是构建服务器或客户端的地方。下面的调用每秒钟被服务器和客户端调用 60 次。服务器与客户端极其相似class。

(对于 "duplicate thread" 我找不到它关闭的原因,这只是我在重用 [=26= 时忘记更改一段代码的位置的一个小错误])

客户使用-

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

服务器使用-

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

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

}

抛出的异常其实是SocketException: socket closed,意思是关闭了socket然后继续使用

您可能没有意识到关闭套接字的输入或输出流会关闭套接字。