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?
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");
0
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);
0
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

0

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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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?
0
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.
0
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?
0
pointemanAuthor Commented:
0
pointemanAuthor Commented:
Thanks for helping me understand...
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.