Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Sending Xml file using TcpClient

Sending Xml file using TcpClient

Scheduled Pinned Locked Moved C#
sysadminxmlquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yeast27
    wrote on last edited by
    #1

    buffer = new byte[1024] ; while(ns.DataAvailable) { ns.Read ( buffer, 0, buffer.Length ) ; str += Encoding.ASCII.GetString ( buffer ) ; } sw.WriteLine(str); sw.Close(); }

    S 1 Reply Last reply
    0
    • Y Yeast27

      buffer = new byte[1024] ; while(ns.DataAvailable) { ns.Read ( buffer, 0, buffer.Length ) ; str += Encoding.ASCII.GetString ( buffer ) ; } sw.WriteLine(str); sw.Close(); }

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Is it necessary for you to decode and encode the string to a byte array and back? You can simply wrap ns in a StreamWriter and write the string itself. At the other end, you can wrap ns using a StreamReader and simply call ReadToEnd(). And don't forget to Flush the writer after you're done. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      Y 1 Reply Last reply
      0
      • S S Senthil Kumar

        Is it necessary for you to decode and encode the string to a byte array and back? You can simply wrap ns in a StreamWriter and write the string itself. At the other end, you can wrap ns using a StreamReader and simply call ReadToEnd(). And don't forget to Flush the writer after you're done. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        Y Offline
        Y Offline
        Yeast27
        wrote on last edited by
        #3

        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,

        S 1 Reply Last reply
        0
        • Y Yeast27

          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,

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          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

          Y 1 Reply Last reply
          0
          • S S Senthil Kumar

            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

            Y Offline
            Y Offline
            Yeast27
            wrote on last edited by
            #5

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

            Y 1 Reply Last reply
            0
            • Y Yeast27

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

              Y Offline
              Y Offline
              Yeast27
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups