Sending Xml file using TcpClient
-
buffer = new byte[1024] ; while(ns.DataAvailable) { ns.Read ( buffer, 0, buffer.Length ) ; str += Encoding.ASCII.GetString ( buffer ) ; } sw.WriteLine(str); sw.Close(); }
Is it necessary for you to decode and encode the string to a byte array and back? You can simply wrap
ns
in aStreamWriter
and write the string itself. At the other end, you can wrapns
using aStreamReader
and simply callReadToEnd()
. And don't forget toFlush
the writer after you're done. Regards Senthil _____________________________ My Blog | My Articles | WinMacro -
Is it necessary for you to decode and encode the string to a byte array and back? You can simply wrap
ns
in aStreamWriter
and write the string itself. At the other end, you can wrapns
using aStreamReader
and simply callReadToEnd()
. And don't forget toFlush
the writer after you're done. Regards Senthil _____________________________ My Blog | My Articles | WinMacro -
I don't really get it, do you mind showing me how to do it in coding? Well I didn't think about encoding though, I just thought that when transfering through socket, infomation needed to be converted to byte form... isn't it so? Thank you,
Something like this
// Sending code
TextReader infoReader = new StreamReader("SystemInfo.xml");
string info = infoReader.ReadToEnd();
StreamWriter writer = new StreamWriter(ns);
writer.Write(info);
writer.Flush();//Receiving code
using (StreamWriter sw = File.AppendText("clientInf.xml"))
{
StreamReader reader = new StreamReader(ns);
string str = reader.ReadToEnd();
sw.WriteLine(str);
sw.Close();
}I guess the .NET functions use the default encoding scheme to send and receive data. What you might have missed is
Flush
. What exactly are you receiving? Regards Senthil _____________________________ My Blog | My Articles | WinMacro -
Something like this
// Sending code
TextReader infoReader = new StreamReader("SystemInfo.xml");
string info = infoReader.ReadToEnd();
StreamWriter writer = new StreamWriter(ns);
writer.Write(info);
writer.Flush();//Receiving code
using (StreamWriter sw = File.AppendText("clientInf.xml"))
{
StreamReader reader = new StreamReader(ns);
string str = reader.ReadToEnd();
sw.WriteLine(str);
sw.Close();
}I guess the .NET functions use the default encoding scheme to send and receive data. What you might have missed is
Flush
. What exactly are you receiving? Regards Senthil _____________________________ My Blog | My Articles | WinMacroCan this method handle multiple client? I mean like if the client are sending at the same time.. will the data get mixed up ? Well, I am making a service to inpect client hardware specf and then display it back on the website. It is for my internship project though. Thanks a lot, will try it out tomorrow ..
-
Can this method handle multiple client? I mean like if the client are sending at the same time.. will the data get mixed up ? Well, I am making a service to inpect client hardware specf and then display it back on the website. It is for my internship project though. Thanks a lot, will try it out tomorrow ..
Well... after looking through the resulting xml file, I discovered that it didn't really get mess up but instead one of the line get copied twice and somehow the second copy appeared at the end of the file... the same thing happened to the code you given though