-2

这是我用来ArrayList从服务器发送到客户端(或 Visa-Versa)的代码。它不起作用,但我不确定为什么。抛出的错误是SocketClosed. Interfacer 是一个允许用户决定是服务器还是客户端的类,这就是构建服务器或客户端的地方。服务器和客户端每秒调用下面的调用 60 次。Server 与 Client 类极为相似。

(对于“重复线程”,我找不到它关闭的原因,这只是一个小错误,因为我在重用类时忘记更改一段代码的位置)

客户使用-

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

客户端类:

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

}
4

1 回答 1

0

抛出的异常其实是SocketException: socket closed,说明已经关闭了socket,然后继续使用。

可能您不知道关闭套接字的输入或输出流会关闭套接字。

于 2016-05-29T00:19:06.340 回答