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

时间:2016-05-28 23:19:16

标签: java sockets arraylist server clients

这是我用来从服务器向客户端(或Visa-Versa)发送ArrayList的代码。它不起作用,但我不确定为什么。抛出的错误是SocketClosed。 Interfacer是一个允许用户决定成为服务器或客户端的类,也就是构建服务器或客户端的地方。每个服务器和客户端每秒调用60次以下调用。服务器与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; 
}

}

1 个答案:

答案 0 :(得分:0)

抛出的异常实际上是SocketException: socket closed,这意味着关闭套接字然后继续使用它。

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