I have a client and server application. The server sends the file via TCP, the client accepts. That's only after the server has sent, the client cannot deserialize. Here is the function of sending a file.
public string SendFileName = null; public void SendData() { // Π‘ΠΎΡΡΠ°Π² ΠΎΡΡΡΠ»Π°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ½ΠΈΠ²Π΅ΡΡΠ°Π»ΡΠ½ΠΎΠ³ΠΎ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ // 1. ΠΠ°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ ΠΎ ΡΠ»Π΅Π΄ΡΡΡΠΈΠΌ ΠΎΠ±ΡΠ΅ΠΊΡΠΎΠΌ ΠΊΠ»Π°ΡΡΠ° ΠΏΠΎΠ΄ΡΠΎΠ±Π½ΠΎΠΉ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΠΈ Π΄Π°Π»ΡΠ½Π΅ΠΉΡΠΈΡ
Π±Π°ΠΉΡΠΎΠ² // 2. ΠΠ±ΡΠ΅ΠΊΡ ΠΊΠ»Π°ΡΡΠ° ΠΏΠΎΠ΄ΡΠΎΠ±Π½ΠΎΠΉ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΠΈ ΠΎ ΡΠ»Π΅Π΄ΡΡΡΠΈΡ
Π±Π°ΠΉΡΠ°Ρ
// 3. ΠΠ°ΠΉΡΡ Π½Π΅ΠΏΠΎΡΡΠ΅Π΄ΡΡΠ²Π΅Π½Π½ΠΎ Π³ΠΎΡΠΎΠ²ΡΡ
ΠΊ Π·Π°ΠΏΠΈΡΠΈ Π² ΡΠ°ΠΉΠ» ΠΈΠ»ΠΈ Π΄Π»Ρ ΡΠ΅Π³ΠΎ-ΡΠΎ ΠΈΠ½ΠΎΠ³ΠΎ. SendInfo si = new SendInfo(); // ΠΡΠ»ΠΈ Π½Π΅Ρ ΠΎΡΡΡΠ»Π°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ°ΠΉΠ»Π° ΠΏΡΠΎΠ΄ΠΎΠ»ΠΆΠ°ΡΡ ΠΏΡΠΎΡΠ΅Π΄ΡΡΡ ΠΎΡΠΏΡΠ°Π²ΠΊΠΈ Π½Π΅Ρ ΡΠΌΡΡΠ»Π°. if (String.IsNullOrEmpty(SendFileName) == true) return; if (SendFileName != null) { FileInfo fi = new FileInfo(SendFileName); if (fi.Exists == true) { si.filesize = (int)fi.Length; si.filename = fi.Name; } fi = null; } BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, si); ms.Position = 0; byte[] infobuffer = new byte[ms.Length]; int r = ms.Read(infobuffer, 0, infobuffer.Length); ms.Close(); byte[] header = GetHeader(infobuffer.Length); byte[] total = new byte[header.Length + infobuffer.Length + si.filesize]; Buffer.BlockCopy(header, 0, total, 0, header.Length); Buffer.BlockCopy(infobuffer, 0, total, header.Length, infobuffer.Length); // ΠΡΠ»ΠΈ ΠΏΡΡΡ ΡΠ°ΠΉΠ»Π° ΡΠΊΠ°Π·Π°Π½, Π΄ΠΎΠ±Π°Π²ΠΈΠΌ Π΅Π³ΠΎ ΡΠΎΠ΄Π΅ΡΠΆΠΈΠΌΠΎΠ΅ Π² ΠΎΡΠΏΡΠ°Π²Π»ΡΠ΅ΠΌΡΠΉ ΠΌΠ°ΡΡΠΈΠ² Π±Π°ΠΉΡΠΎΠ² if (si.filesize > 0) { FileStream fs = new FileStream(SendFileName, FileMode.Open, FileAccess.Read); fs.Read(total, header.Length + infobuffer.Length, si.filesize); fs.Close(); fs = null; } try { // ΠΡΠΏΡΠ°Π²ΠΈΠΌ Π΄Π°Π½Π½ΡΠ΅ ΠΏΠΎΠ΄ΠΊΠ»ΡΡΠ΅Π½Π½ΡΠΌ ΠΊΠ»ΠΈΠ΅Π½ΡΠ°ΠΌ NetworkStream ns = _tcpClient.tcpClient.GetStream(); // Π’Π°ΠΊ ΠΊΠ°ΠΊ Π΄Π°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ Π² ΠΎΡΠ΄Π΅Π»ΡΠ½ΠΎΠΌ ΠΏΠΎΡΠΎΠΊΠ΅ ΡΠ°ΡΠΈΠΎΠ½Π°Π»ΡΠ½Π΅ΠΉ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ ΠΎΡΠΏΡΠ°Π²ΠΊΠΈ ns.Write(total, 0, total.Length); // ΠΠ±Π½ΡΠ»ΠΈΠΌ Π²ΡΠ΅ ΡΡΡΠ»ΠΊΠΈ Π½Π° ΠΌΠ½ΠΎΠ³ΠΎΠ±Π°ΠΉΡΠ½ΡΠ΅ ΠΎΠ±ΡΠ΅ΠΊΡΡ ΠΈ ΠΏΠΎΠΏΡΠΎΠ±ΡΠ΅ΠΌ ΠΎΡΠΈΡΡΠΈΡΡ ΠΏΠ°ΠΌΡΡΡ header = null; infobuffer = null; total = null; SendFileName = null; GC.Collect(); GC.WaitForPendingFinalizers(); // ΠΠΎΠ΄ΡΠ²Π΅ΡΠΆΠ΄Π΅Π½ΠΈΠ΅ ΡΡΠΏΠ΅ΡΠ½ΠΎΠΉ ΠΎΡΠΏΡΠ°Π²ΠΊΠΈ Parent.ShowReceiveMessage("ΠΠ°Π½Π½ΡΠ΅ ΡΡΠΏΠ΅ΡΠ½ΠΎ ΠΎΡΠΏΡΠ°Π²Π»Π΅Π½Ρ!"); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } } Here is the function of accepting the file on the Client side:
public void ReadCallback(IAsyncResult ar) { if (modeNetwork == Mode.indeterminately) return; TcpClientData myTcpClient = (TcpClientData)ar.AsyncState; try { NetworkStream ns = myTcpClient.tcpClient.GetStream(); int r = ns.EndRead(ar); if (r > 0) { // ΠΠ· Π³Π»Π°Π²Π½ΠΎΠ³ΠΎ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠ° ΠΏΠΎΠ»ΡΡΠΈΠΌ ΡΠ°Π·ΠΌΠ΅Ρ ΠΌΠ°ΡΡΠΈΠ²Π° Π±Π°ΠΉΡΠΎΠ² ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΠΎΠ½Π½ΠΎΠ³ΠΎ ΠΎΠ±ΡΠ΅ΠΊΡΠ° string header = Encoding.Default.GetString(myTcpClient.buffer); int leninfo = int.Parse(header); // ΠΠΎΠ»ΡΡΠΈΠΌ ΠΈ Π΄Π΅ΡΠ΅ΡΠΈΠ°Π»ΠΈΠ·ΡΠ΅ΠΌ ΠΎΠ±ΡΠ΅ΠΊΡ Ρ ΠΏΠΎΠ΄ΡΠΎΠ±Π½ΠΎΠΉ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΠ΅ΠΉ ΠΎ ΡΠΎΠ΄Π΅ΡΠΆΠ°Π½ΠΈΠΈ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅ΡΠ΅Π²ΠΎΠ³ΠΎ ΠΏΠ°ΠΊΠ΅ΡΠ° MemoryStream ms = new MemoryStream(leninfo); byte[] temp = new byte[leninfo]; r = ns.Read(temp, 0, temp.Length); ms.Write(temp, 0, r); BinaryFormatter bf = new BinaryFormatter(); ms.Position = 0; SendInfo sc = (SendInfo)bf.Deserialize(ms); ms.Close(); if (sc.filesize > 0) { // Π‘ΠΎΠ·Π΄Π°Π΄ΠΈΠΌ ΡΠ°ΠΉΠ» Π½Π° ΠΎΡΠ½ΠΎΠ²Π΅ ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΠΎΠΉ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΠΈ ΠΈ ΠΌΠ°ΡΡΠΈΠ²Π° Π±Π°ΠΉΡΠΎΠ² ΡΠ»Π΅Π΄ΡΡΡΠΈΡ
Π·Π° ΠΎΠ±ΡΠ΅ΠΊΡΠΎΠΌ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΠΈ FileStream fs = new FileStream(sc.filename, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, sc.filesize); do { temp = new byte[global.MAXBUFFER]; r = ns.Read(temp, 0, temp.Length); // ΠΠ°ΠΏΠΈΡΡΠ²Π°Π΅ΠΌ ΡΡΡΠΎΠ³ΠΎ ΡΡΠΎΠ»ΡΠΊΠΎ Π±Π°ΠΉΡΠΎΠ² ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΡΡΠ΅Π½ΠΎ ΠΌΠ΅ΡΠΎΠ΄ΠΎΠΌ Read() fs.Write(temp, 0, r); // ΠΠ°ΠΊ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½Ρ Π²ΡΠ΅ Π±Π°ΠΉΡΡ ΡΠ°ΠΉΠ»Π°, ΠΎΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅ΠΌ ΡΠΈΠΊΠ», // ΠΈΠ½Π°ΡΠ΅ ΠΎΠ½ Π·Π°Π±Π»ΠΎΠΊΠΈΡΡΠ΅ΡΡΡ Π² ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠΈ Π½ΠΎΠ²ΡΡ
ΡΠ΅ΡΠ΅Π²ΡΡ
Π΄Π°Π½Π½ΡΡ
if (fs.Length == sc.filesize) { fs.Close(); fs = null; break; } } while (r > 0); temp = null; GC.Collect(); GC.WaitForPendingFinalizers(); } if (Receive != null) Receive(this, new ReceiveEventArgs(sc)); myTcpClient.buffer = new byte[global.LENGTHHEADER]; ns.BeginRead(myTcpClient.buffer, 0, myTcpClient.buffer.Length, new AsyncCallback(ReadCallback), myTcpClient); } else { DeleteClient(myTcpClient); // Π‘ΠΎΠ±ΡΡΠΈΠ΅ ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΎΡΠΊΠ»ΡΡΠΈΠ»ΡΡ if (Disconnected != null) Disconnected.BeginInvoke(this, "ΠΠ»ΠΈΠ΅Π½Ρ ΠΎΡΠΊΠ»ΡΡΠΈΠ»ΡΡ!", null, null); } } catch (Exception e) { MessageBox.Show(e.ToString()); DeleteClient(myTcpClient); // Π‘ΠΎΠ±ΡΡΠΈΠ΅ ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΎΡΠΊΠ»ΡΡΠΈΠ»ΡΡ if (Disconnected != null) { Disconnected.BeginInvoke(this, "ΠΡΠΎΠΈΠ·ΠΎΡΠ»Π° ΠΎΡΠΈΠ±ΠΊΠ° Π²ΠΎ Π²ΡΠ΅ΠΌΡ ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΡ ΠΠ°Π·Ρ ΠΠ°Π½ΡΡ
. ΠΡΠΎΡΡΠ±Π° ΠΏΠΎΠ²ΡΠΎΡΠΈΡΡ ΠΏΠΎΠΏΡΡΠΊΡ.", null, null); } SoundError(); } } This is the class that exists both on the Client and on the Server:
[Serializable] class SendInfo { public string message; public string filename; public int filesize; } Here on this line
SendInfo sc = (SendInfo)bf.Deserialize(ms); and an error occurs, namely, writes:
{"Failed to cast object type \" SERVER.SendInfo \ "to type \" KLIENT.SendInfo \ "."} System.Exception {System.InvalidCastException}
SERVER.SendInfoand on the clientKLIENT.SendInfo. These are different classes, even if their definitions are the same. - PetSerAlSendInfo sc = (SendInfo)bf.Deserialize(ms);onobject o = bf.Deserialize(ms);andSendInfo sc = (SendInfo)o;, then you will see that the exception does not appear in the call toDeserialize. - PetSerAl