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 over a Socket connection

Sending XML over a Socket connection

Scheduled Pinned Locked Moved C#
questioncsharpcomsysadminxml
2 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.
  • P Offline
    P Offline
    Priya Prk
    wrote on last edited by
    #1

    Dear all, Im very new in this, hope to get some tips. I have to send some data in XML over a Socket connection to a application that opens a TCP/IP port and acts as a server. I found some samples on google like this one : http://www.java2s.com/Tutorial/CSharp/0580\_\_Network/SimpleTcpClientsenddatatotheserver.htm But what is the best way to achieve this? Thanks in advance.

    L 1 Reply Last reply
    0
    • P Priya Prk

      Dear all, Im very new in this, hope to get some tips. I have to send some data in XML over a Socket connection to a application that opens a TCP/IP port and acts as a server. I found some samples on google like this one : http://www.java2s.com/Tutorial/CSharp/0580\_\_Network/SimpleTcpClientsenddatatotheserver.htm But what is the best way to achieve this? Thanks in advance.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Find below code for the same.

      XDocument doc = XDocument.Parse("<Hello>World</Hello>", LoadOptions.PreserveWhitespace);
      string host = "localhost";
      int port = 1234;

      	// Look up the address for the specified host.
      	IPHostEntry address = Dns.GetHostEntry(host);
      	IPEndPoint ipe = new IPEndPoint(address.AddressList\[0\], port);
      
      	// Create Socket.	
      	using (Socket sock = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
      	{
      		sock.Connect(ipe); // Connect to the Socket.
      		// Create Stream.
      		using (NetworkStream sockStream = new NetworkStream(sock))
      		{
      			// Read from the XDocument.
      			using (XmlReader reader = doc.CreateReader())
      			{
      				// Copy nodes to an XmlWriter which transforms them to bytes that are written to the Stream for the Socket.
      				XmlWriterSettings settings = new XmlWriterSettings();
      				settings.Encoding = new UTF8Encoding(false, true);
      				using (XmlWriter writer = XmlWriter.Create(sockStream, settings))
      				{
      					while (reader.Read()) // While there is another XML node...
      					{
      						writer.WriteNode(reader, false); // Copy that node.
      					}
      				}
      			}
      		}
      	}
      

      For more information find following links useful. File Transfer using Socket Application in C# .NET 2.0[^] http://social.msdn.microsoft.com/Forums/en/ncl/thread/6994d38b-5caf-47ff-a425-969651d1a292[^] HTH

      Jinal Desai - LIVE Experience is mother of sage....

      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