The Broker Client
The broker client performs the following basic operations:
Loading the XML file
Connecting with the server
Sending the XML file to the server
Receiving and saving the XML response
The classes in the System.Xml namespace provide XML support in .NET.
The following sections describe each of these operations.
Loading the XML File
The XmlDocument class implements the Document Object Model (DOM) in .NET. The Document Object Model is an in-memory representation of the XML document. It presents the document in a tree format, allowing the developer to programmatically read, create, and manipulate individual nodes of the XML document. Editing is the primary function of the XmlDocument class. The following code shows how you create an instance of the XmlDocument class and then retrieve the XML file using the filename:
XmlDocument orderXml = new XmlDocument(); // loading XML file into document object model orderXml.Load("order.xml");
The XmlDocument's Load function can be used to read XML from a file:
public virtual void Load(string filename);
Connecting with the Server
After loading the XML file, the client tries to connect with the server. The .NET Framework provides several classes for network programming. It provides separate interfaces for programming with lower-level sockets and with higher-level TCP classes. There are different classes for implementing server and client. The TcpClient class represents a TCP client and is provided in the System.Net.Sockets namespace:
// creating a new instance of the TcpClient class TcpClient newClient = new TcpClient(); // connect with the host newClient.Connect(ipAddr, port);
Before calling any method of TcpClient, we must create an instance of the class. The connect method is then called to establish a connection with the server. The method takes two parameters:
public void connect(string ipaddress, int port);
Sending the XML File to the Server
Before sending anything to the server side of the application, the application needs to get a stream from the client. Picture the stream as a wire that passes data from one end to the other; the GetStream method gets the wire end for you. The .NET Framework provides the NetworkStream class specifically to manage communication between two-networked applications. This class is derived from the Stream class and offers several methods to send and receive bytes to and from the server:
// creating a network stream NetworkStream tcpStream = newClient.GetStream(); string xmlFile = orderXml.OuterXml; // sending XML to server Byte sendBytes = Encoding.ASCII.GetBytes(xmlFile + "\r\n"); tcpStream.Write(sendBytes,0, sendBytes.Length);
Calling the GetStream method of the TcpClient object returns an instance of the NetworkStream class. This method returns the underlying stream used to send and receive data. Next, we use the XmlDocument's OuterXml property to store XML as a String; the contents of the string are later converted into bytes to send through to the server. The GetBytes function does the conversion; it takes a string as a parameter and returns its equivalent byte array. The byte array is transmitted to the server using the NetworkStream's Write member function:
public void Write(byte  buffer, int offset, int size);
The first parameter is the data buffer to write, the second is the offset from which to start writing data, and the third is the size of the buffer.
Receiving and Saving the XML Response
After sending the XML file to the server, the last step for the client is to wait for the incoming response. Since this is a synchronous application, the function blocks the current thread. As soon as the server application finishes its task, it sends the XML response back to the client. The client uses the StreamReader class to read the response into a string. I like using StreamReader because its ReadLine function reads the complete response and then stores it in a string. By contrast NetworkStream's Read function reads the response into bytes; you have to use the GetString method to convert it back to a string.
StreamReader strReader = new StreamReader(newClient.GetStream()); string returnData = strReader.ReadLine(); orderXml.LoadXml(returnData); string filename = "response.xml"; // saving XML to file orderXml.Save(filename);
The StreamReader's constructor requires the stream object as a parameter to construct the reader.
After receiving the response, the XML file is loaded into an XmlDocument object and then saved. The XmlDocument's Save function persists the XML DOM to a file. Notice the LoadXML function in the above code. The XmlDocument facilitates loading from a file or a string, so there are two different functions provided for each of these tasks. The Load function is used in conjunction with the filename, but here we have the XML as a string; therefore, we use the LoadXML function to transform the XML stream into an XML document object model.
Now let's look at how to implement the broker server.