TCP BinaryFormatter Deserialize To File?

I always 'write' an emty file, not sure exacty where I went wrong, help!

[Sending]
tcpClient = new TcpClient();
tcpClient.Connect(IPAddress.Parse("192.168.1.7"), 5000);
NetworkStream ns = tcpClient.GetStream();
IFormatter bf = new BinaryFormatter();
bf.Serialize(ns,"test.txt");
tcpClient.Close();

[Receiving]
listener = new TcpListener(IPAddress.Any, 5000);
listener.Start();
                       
while ((client = listener.AcceptTcpClient()) != null)
{
     NetworkStream ns = client.GetStream();
     object obj = null;

     if (ns.DataAvailable)
     {
           try
           {
                  IFormatter bf = new BinaryFormatter();
                  obj = bf.Deserialize(ns);
           }
           finally
           {                            
                  StreamWriter sw = new StreamWriter(new FileStream("test.txt", FileMode.Create, FileAccess.Write, FileShare.Read));
                  sw.WriteLine(obj.ToString());
           }
     }
pointemanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lazyberezovskyCommented:
You are serializing string "test.txt", not content of test.txt file:
bf.Serialize(ns,"test.txt");
pointemanAuthor Commented:
I haven't tried this yet, but I understand your point...

FileStream fs = new FileStream("test.txt", FileMode.Open) ;
bf.Serialize(ns, fs);
lazyberezovskyCommented:
Well, actually you don't need serialization here.
byte[] buffer = new byte[1024];

using(FileStream file = File.OpenRead("test.txt"))
using(NetworkStream networkStream = tcpClient.GetStream())
{
    int bytesRead;

    do
    {
        bytesRead = file.Read(buffer, 0, buffer.Length);
        networkStream.Write(buffer, 0, bytesRead);
    }
    while (bytesRead == buffer.Length);
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

pointemanAuthor Commented:
I read other posts on the Internet where people experienced problems sending both text files and image files. I thought serialization would enable sending any file type via TCP?
lazyberezovskyCommented:
No, serialization just allows to represent objects as
1) text (XmlSerializer, SoapFormatter)
2) bytes (BinaryFormatter)

File (image or text file) is already stream of bytes. So you do not need serialization to convert it to bytes.
pointemanAuthor Commented:
Okay, understood. I also read about poor serialization performance too. Anyway, I used your code and could not receive the data on the client and write to file. Always blank file... Help
Q. How do you receive the file and write it to harddrive?
pointemanAuthor Commented:
pointemanAuthor Commented:
Thanks for helping me understand...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.