TCP BinaryFormatter Deserialize To File?

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

tcpClient = new TcpClient();
tcpClient.Connect(IPAddress.Parse(""), 5000);
NetworkStream ns = tcpClient.GetStream();
IFormatter bf = new BinaryFormatter();

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

     if (ns.DataAvailable)
                  IFormatter bf = new BinaryFormatter();
                  obj = bf.Deserialize(ns);
                  StreamWriter sw = new StreamWriter(new FileStream("test.txt", FileMode.Create, FileAccess.Write, FileShare.Read));
Who is Participating?

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

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.

You are serializing string "test.txt", not content of test.txt file:
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);
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;

        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?
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

From novice to tech pro — start learning today.