-1

作成したシリアル化可能なオブジェクトをソケット経由で送信する際に奇妙な問題があります。実際、サーバーとクライアントを同じマシンで実行するとうまく動作しますが、サーバーとクライアントが異なるマシンにある場合、サーバー側の readen オブジェクトは空です (サイズがゼロに等しい)

誰でもそれを修正する考えがありますか? (コードは以下)

サーバ:

public static void main () {
...
InputStream is = mysocket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);

ArrayList<MyObject> list_of_object;
list_of_object = (ArrayList<MyObject>) ois.readObject();
logger.log(Level.INFO,"object readen with size : "+list_of_object.size());
...
}

クライアント:

public static void main () {
...
ObjectOutputStream oos = new ObjectOutputStream(mysocket.getOutputStream());
oos.writeObject(list_of_object);
...
}
4

1 に答える 1

0

-classのバージョンで次のテスト ケースを試してくださいMyObject。問題を理解するのに役立ちます。これがあなたのクラスで問題なく動作する場合、それはネットワーク レイヤーである可能性があります。よくある間違いは、古いバージョンのサーバー コードをどこかで実行していて、実際に MyObjects の空のリストを読んでいる可能性があることです。

import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import org.junit.Test;

public class StreamTest {

public static class MyObject implements Serializable {

}

@Test
public void test() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    ArrayList<MyObject> list_of_object = new ArrayList<MyObject>();
    list_of_object.add(new MyObject());
    oos.writeObject(list_of_object);

    byte[] whatGoesOverWire = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(whatGoesOverWire);
    ObjectInputStream ois = new ObjectInputStream(bais);
    ArrayList<MyObject> deserialized = (ArrayList<MyObject>) ois
            .readObject();
    assertEquals(1, list_of_object.size());
    assertEquals(1, deserialized.size());
}

}
于 2012-11-06T15:52:37.093 に答える