2016-05-28 2 views
-1

これはサーバーからクライアント(またはVisa-Versa)にArrayListを送信するために使用しているコードです。それは機能しませんが、なぜ私は確信していません。スローされたエラーは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; 
} 

} 

答えて

0

例外は実際にはSocketException: socket closedであり、を意味します。ソケットを閉じてから、引き続き使用します。

おそらく、ソケットの入力ストリームまたは出力ストリームを閉じることによってソケットが閉じられることに気付かれません。

+0

あなたが正しいです、私はインスタントメッセンジャーからクラスをコピーしてからfinally {closeConnection();}を移動するのを忘れました。しかし、今私が持っている問題は、兵器の対象は殺菌されていないということです。特に、Java.Awt.Image.BufferedImageは殺菌されていません(TroopsがSerializableを実装しているため)。私はオブジェクトを裂く必要はなく、それぞれのビットを単独で送信して戻す必要はありません。何か案は? – Dak31

+0

「シリアル化されていません」という意味ですか? 'シリアル化できません'? 'BufferedImage'を送る方法についてはここでたくさんの答えがあります。 – EJP

+0

私はそれをもう一度見なければならないでしょう。私は本当に助けに感謝します! – Dak31

関連する問題