2

i try to send Image Object as serialized File from Client to Server by @TCP And get this exception

 image exception

Server Code

 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;
    }

Client code

    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();


    }
    }

i have read many topic about this exception and all of them talk about two solution

  • formatter.Binder
  • AppDomain.CurrentDomain.AssemblyResolve

but still not work

Ahmed Samir
  • 53
  • 10

1 Answers1

3

Of course the server side will not find the client side assembly. And it should not. Your client side code must not live in the server side. The problem here is that you defined your ImageSerial class twice, one in the server and one in the client. That's plain wrong if you control both sides. Create a common assembly that is referenced by both the client and the server and put your common classes there.

Also, remove all references from the server to the client. It should be the other way bound if you wish, or use an intermediate service layer such as WCF.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • how to create a common assembly that is referenced by both ?? – Ahmed Samir Nov 09 '12 at 22:03
  • Right click solution in the solution explorer -> new -> project -> Class Library. Then on server and client projects right click -> Add Reference -> Projects -> [your new class library project] – Federico Berasategui Nov 09 '12 at 22:05
  • http://msdn.microsoft.com/en-us/library/wkze6zky(v=vs.80).aspx here is the MSDN about it – Federico Berasategui Nov 09 '12 at 22:05
  • u mean make reference to same dll file from server and client ? Is dll will contain serialization operation ? **note my server and client will be in different machine** – Ahmed Samir Nov 09 '12 at 22:15
  • 1
    not 'operations' as such, but 'Models' are to be put in a shared assembly, things such as classes that both the client and the server need to know in order to operate. That way you don't have to write a server and a client version of the same class, which of course serialization doesn't understand, because when you try do deserialize an object of type `Receiver.ImageSerial` into an object of type `WindowsFormsApplication5.ImageSerial`, .Net does not think those types are the same, and it'll error out. – Federico Berasategui Nov 09 '12 at 22:22
  • 'Operations', in a strict sense of the word, must be placed in their corresponding server or client side code, because 'operations' will be performed by either the client, or the server, but 'Data Types' if you want to call it that, are 'Shared' between server and client, so that they interchange objects of the same types. – Federico Berasategui Nov 09 '12 at 22:23