Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 跨套接字发送信息有什么不对?_Java_Sockets_Arraylist_Server_Clients - Fatal编程技术网

Java 跨套接字发送信息有什么不对?

Java 跨套接字发送信息有什么不对?,java,sockets,arraylist,server,clients,Java,Sockets,Arraylist,Server,Clients,这是我用来将ArrayList从服务器发送到客户端或从客户端发送到服务器的代码。它不起作用,但我不确定为什么。抛出的错误是SocketClosed。Interfacer是一个类,允许用户决定是服务器还是客户机,这就是服务器或客户机的构造位置。服务器和客户端每秒调用以下调用60次。服务器与客户机类非常相似 对于重复的线程,我找不到它关闭的原因,这只是一个小错误,因为在重用该类时,我忘记了更改一段代码的位置 由客户使用- Interfacer.getClient().sendArrayList(Ga

这是我用来将ArrayList从服务器发送到客户端或从客户端发送到服务器的代码。它不起作用,但我不确定为什么。抛出的错误是SocketClosed。Interfacer是一个类,允许用户决定是服务器还是客户机,这就是服务器或客户机的构造位置。服务器和客户端每秒调用以下调用60次。服务器与客户机类非常相似

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

由客户使用-

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

抛出的异常实际上是SocketException:socketclosed,这意味着您已经关闭了socket,然后继续使用它


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

您是对的,我忘记移动最后的{closeConnection;},因为我从instant messenger复制了该类。但我现在面临的问题是,部队目标没有消毒。特别是Java.Awt.Image.buffereImage,由于它实现了可序列化,因此不会对其进行消毒。我不想把这个东西撕成碎片,然后把每一个碎片都单独发送,然后再重新组装起来。有什么想法吗?你是说“没有连载”吗不可序列化'?关于如何发送BuffereImage,这里有很多答案。我将不得不进一步研究,一步一步:。我真的很感谢你的帮助!
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; 
}

}