2

@TCP によってクライアントからサーバーにシリアル化されたファイルとして画像オブジェクトを送信しようとすると、この例外が発生します

画像の例外

サーバーコード

 namespace Receiver
  {
 [Serializable()]
   public class ImageSerial : ISerializable
   {
    public Image img = null;

    public ImageSerial(SerializationInfo info, StreamingContext ctxt)
    {
        img = (Image)info.GetValue("OMG", typeof(Image));
    }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("OMG", img);
    }
}

public class ObjSerial
{
    private Stream stream;
    private BinaryFormatter bformatter;
    private string FILENAME = "m.bin";
    ImageSerial mp = new ImageSerial();

    public Image getImgFromBin()
    {

        stream = File.Open(FILENAME, FileMode.Open);
        bformatter = new BinaryFormatter();

        mp = (ImageSerial)bformatter.Deserialize(stream);
        stream.Close();
        return mp.img;
    }

クライアントコード

    namespace WindowsFormsApplication5
    {
   [Serializable()]
     class ImageSerial :ISerializable
    {
    public Image img = null;
    public ImageSerial() { }

    public ImageSerial(SerializationInfo info, StreamingContext ctxt)
      { 
           img = (Image)info.GetValue("OMG", typeof(Image));
      }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
       info.AddValue("OMG",img);
    }
}

public class ObjSerial
{
   private string FILENAME = "m.bin";
   private TcpClient tcpClient;
   private FileStream fstFile;
   private NetworkStream strRemote;
   private string SERVERIP = "10.102.239.207";
   private int SERVERPort = 5051;

    public  void start(Image ims)
    {

        ImageSerial mp = new ImageSerial();
        mp.img = ims;

        Stream stream = File.Open(FILENAME, FileMode.Create);
        BinaryFormatter bformatter = new BinaryFormatter(); 

        bformatter.Serialize(stream, mp);
        stream.Close();

        //Clear mp for further usage.
        sendFile();



       }
       private void ConnectToServer(string ServerIP, int ServerPort)
       {  
        tcpClient = new TcpClient();
        try
        { 
            tcpClient.Connect(ServerIP, ServerPort);
        }
        catch (Exception exMessage)
        {
            // Display any possible error
        }
    }

    private void sendFile()
    {

        ConnectToServer(SERVERIP, SERVERPort);
        if (tcpClient.Connected == false)
        { 
            ConnectToServer(SERVERIP, SERVERPort);
        } 
        strRemote = tcpClient.GetStream();
        fstFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);

        int bytesSize = 0; 
        byte[] downBuffer = new byte[2048];

        while ((bytesSize = fstFile.Read(downBuffer, 0, downBuffer.Length)) > 0)
        {
             strRemote.Write(downBuffer, 0, bytesSize);
        }
        tcpClient.Close();
        strRemote.Close();
        fstFile.Close();


    }
    }

私はこの例外に関する多くのトピックを読みましたが、それらはすべて2つの解決策について話しています

  • formatter.Binder
  • AppDomain.CurrentDomain.AssemblyResolve

しかし、まだ動作しません

4

1 に答える 1

3

もちろん、サーバー側はクライアント側のアセンブリを見つけられません。そして、そうすべきではありません。クライアント側のコードはサーバー側に存在してはなりません。ここでの問題は、ImageSerialクラスを 2 回 (サーバーで 1 つ、クライアントで 1 つ) 定義したことです。両側を制御する場合、それは明らかに間違っています。クライアントとサーバーの両方から参照される共通アセンブリを作成し、そこに共通クラスを配置します。

また、サーバーからクライアントへのすべての参照を削除します。必要に応じて逆方向にバインドするか、WCF などの中間サービス層を使用する必要があります。

于 2012-11-09T21:56:24.167 に答える