So, let me share my partial knowledge with you guys , please correct me if I am wrong and feel free to extend this post with your outstanding knowledge.
Earlier when I created some .NET applications which interacts with each other by using .NET remoting, ok let me expand a bit, 2 different applications that means atleast in 2 different Appdomains (if they are same Appdomains we dont need remoting we just need to reference assmbly and invokes its method or whatever crap we want to do..) and if we want to these 2 appliations communicate with each other there comes .NET remoting (Now again we can use web services or HTTP to make them communicate but for now lets park them aside, we will keep our focus on TCP) then we used to do marshalling (MBR or MBV).
Something like, Client application makes call to Proxy (again, Tansparent proxy which is something like superset of real proxy) and this proxy and then the object is formatted by suitable formatter (in case of TCP , its Binary formatter and in case of HTTP its SOAP Formatter ) and then formatted object is pushed to channel and then at server application this formmated object is re-formatted back to the original shape nad then get access to remote object or application or whatever.
Dont get confused by formatted object, its basically a serialized object or better Binary serialized object or more specifically an array of bytes, these bytes passes through channel and then at server application Deserialized back to the original object from the array of bytes. Great RIGHT??
I guess NO, in this case you need share your class definitions or object model with client as we were doing since ages while doing web services development as we share WSDL files with client application.
When I was very new to BizTalk, I was very afraid of Streams and BizTalk uses streams as max a possible at adapters , at pipelines, at Maps etc etc.. therefore I was afraid of BizTalk too.
But later on I came to know BizTalk is design to work with any message formats whether it is XML or flat files or binary or anything you can can think of. So, with no doubt Streams was the only choice to work with BizTalk.
So, I thought of creating Client - Server applications which will communicate via TCP and share messages in form of streams.
So in short, SERVER should be able to LISTEN at one port, CLIENT will connect to SERVER then CLIENT creates an object and Serializes this object in bytes and then CLIENT will Send these bytes to SERVER which receives these bytes and then Deserializes it back to the object.
Note: In these extending the functionalities as I am formatting and reformatting objects i.e. Serialization and Deserialization explicitly, Earlier it was done automatically before by the applications at the boundaries of communication channel.
Rather then, sharing the hell lot of crap story lets go ahead with the code.
(when connecion is established between CLIENT and SERVER, resources are allocated for communication which are generally termed as Channel, and in this code Network streams is used to read and write bytes at both client and server, Code itself speaks everything and its working but not that beautiful) , If you people have any doubts or correction please let me know
CLIENT CODE:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
namespace TCPClient
{
class Program
{
static void Main(string[] args)
{
try
{
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect("192.168.1.7", 8002);
// use the ipaddress as in the server program
Console.WriteLine("Connected");
TRGRequest req = new TRGRequest();
req.Packet = "
req.Metadata = new Metadata();
req.Metadata.AppStackVersion = "2.630.0.0";
Stream stm = tcpclnt.GetStream();
byte[] ba = Utilies.Serialize(req);
Console.WriteLine("Transmitting.....");
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < bformatter =" new" ms =" new" ipad =" IPAddress.Parse(" mylist =" new" cl =" myList.AcceptTcpClient();" ns =" cl.GetStream();" b =" Utilities.ReadFully(ns," ttrgrequest =" (TRGRequest)Utilities.Deserialize(b);" asen =" new" ack =" asen.GetBytes(" bformatter =" new" strm =" new">
/// Reads data from a stream until the end is reached. The
/// data is returned as a byte array. An IOException is
/// thrown if any of the underlying IO calls fail.
///
/// The stream to read data from
/// The initial buffer length
public static byte[] ReadFully(Stream stream, int initialLength)
{
// If we've been passed an unhelpful initial length, just
// use 32K.
if (initialLength < initiallength =" 32768;" buffer =" new" read =" 0;" chunk =" stream.Read(buffer,"> 0)
{
read = read + chunk;
// If we've reached the end of our buffer, check to see if there's
// any more information
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();
// End of stream? If so, we're done
if (nextByte == -1)
{
return buffer;
}
// Nope. Resize the buffer, put in the byte we've just
// read, and continue
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read] = (byte)nextByte;
buffer = newBuffer;
read = read + 1;
}
else
{
//return buffer;
// Buffer is now too big. Shrink it.
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
return ret;
}
}
byte[] ret1 = new byte[read];
Array.Copy(buffer, ret1, read);
return ret1;
}
}
}
OBJECT MODEL of TRGRequest :
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version:2.0.50727.42
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
using System.Xml.Serialization;
using System.Runtime.Serialization;
//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//
///
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class TRGRequest {
private string packetField;
private Metadata metadataField;
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Packet {
get {
return this.packetField;
}
set {
this.packetField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute("Metadata", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public Metadata Metadata {
get {
return this.metadataField;
}
set {
this.metadataField = value;
}
}
}
///
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class Metadata {
private string sendingOrgIDField;
private string sendingSystemField;
private string receivingOrgIDField;
private string receivingSystemField;
private string domainField;
private string messageTypeField;
private string messageIDField;
private string iXMLVersionField;
private string appStackVersionField;
private string machineAddrField;
private string serviceNameField;
private string sessionIDField;
private string replayFlagField;
private string messageSystemField;
private string originalProductionTypeField;
private string originalProductionNameField;
private string currentProductionNameField;
private string finalProductionTypeField;
private string finalProductionNameField;
private string directionField;
private string currentFormatField;
private string statusField;
private string errorCodeField;
private string errorStringField;
private string dateCreatedField;
private string contentTypeField;
private string refToMessageIDField;
private string custom1Field;
private string custom2Field;
private string custom3Field;
private string mSATextField;
private string patientIDField;
private string patientPrimaryFacilityField;
private string patientPrimaryCareProviderField;
private string patientClassField;
private string assignedPatientLocationField;
private string preAdmitNumberField;
private string visitIDField;
private string placerOrderNumberField;
private string placerGroupNumberField;
private string orderStatusField;
private string enteredByField;
private string orderingProviderField;
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string SendingOrgID {
get {
return this.sendingOrgIDField;
}
set {
this.sendingOrgIDField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string SendingSystem {
get {
return this.sendingSystemField;
}
set {
this.sendingSystemField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ReceivingOrgID {
get {
return this.receivingOrgIDField;
}
set {
this.receivingOrgIDField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ReceivingSystem {
get {
return this.receivingSystemField;
}
set {
this.receivingSystemField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Domain {
get {
return this.domainField;
}
set {
this.domainField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string MessageType {
get {
return this.messageTypeField;
}
set {
this.messageTypeField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string MessageID {
get {
return this.messageIDField;
}
set {
this.messageIDField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string iXMLVersion {
get {
return this.iXMLVersionField;
}
set {
this.iXMLVersionField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string AppStackVersion {
get {
return this.appStackVersionField;
}
set {
this.appStackVersionField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string MachineAddr {
get {
return this.machineAddrField;
}
set {
this.machineAddrField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ServiceName {
get {
return this.serviceNameField;
}
set {
this.serviceNameField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string SessionID {
get {
return this.sessionIDField;
}
set {
this.sessionIDField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ReplayFlag {
get {
return this.replayFlagField;
}
set {
this.replayFlagField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string MessageSystem {
get {
return this.messageSystemField;
}
set {
this.messageSystemField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string OriginalProductionType {
get {
return this.originalProductionTypeField;
}
set {
this.originalProductionTypeField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string OriginalProductionName {
get {
return this.originalProductionNameField;
}
set {
this.originalProductionNameField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CurrentProductionName {
get {
return this.currentProductionNameField;
}
set {
this.currentProductionNameField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string FinalProductionType {
get {
return this.finalProductionTypeField;
}
set {
this.finalProductionTypeField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string FinalProductionName {
get {
return this.finalProductionNameField;
}
set {
this.finalProductionNameField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Direction {
get {
return this.directionField;
}
set {
this.directionField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CurrentFormat {
get {
return this.currentFormatField;
}
set {
this.currentFormatField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Status {
get {
return this.statusField;
}
set {
this.statusField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ErrorCode {
get {
return this.errorCodeField;
}
set {
this.errorCodeField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ErrorString {
get {
return this.errorStringField;
}
set {
this.errorStringField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string DateCreated {
get {
return this.dateCreatedField;
}
set {
this.dateCreatedField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ContentType {
get {
return this.contentTypeField;
}
set {
this.contentTypeField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string RefToMessageID {
get {
return this.refToMessageIDField;
}
set {
this.refToMessageIDField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Custom1 {
get {
return this.custom1Field;
}
set {
this.custom1Field = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Custom2 {
get {
return this.custom2Field;
}
set {
this.custom2Field = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Custom3 {
get {
return this.custom3Field;
}
set {
this.custom3Field = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string MSAText {
get {
return this.mSATextField;
}
set {
this.mSATextField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string PatientID {
get {
return this.patientIDField;
}
set {
this.patientIDField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string PatientPrimaryFacility {
get {
return this.patientPrimaryFacilityField;
}
set {
this.patientPrimaryFacilityField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string PatientPrimaryCareProvider {
get {
return this.patientPrimaryCareProviderField;
}
set {
this.patientPrimaryCareProviderField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string PatientClass {
get {
return this.patientClassField;
}
set {
this.patientClassField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string AssignedPatientLocation {
get {
return this.assignedPatientLocationField;
}
set {
this.assignedPatientLocationField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string PreAdmitNumber {
get {
return this.preAdmitNumberField;
}
set {
this.preAdmitNumberField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string VisitID {
get {
return this.visitIDField;
}
set {
this.visitIDField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string PlacerOrderNumber {
get {
return this.placerOrderNumberField;
}
set {
this.placerOrderNumberField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string PlacerGroupNumber {
get {
return this.placerGroupNumberField;
}
set {
this.placerGroupNumberField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string OrderStatus {
get {
return this.orderStatusField;
}
set {
this.orderStatusField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string EnteredBy {
get {
return this.enteredByField;
}
set {
this.enteredByField = value;
}
}
///
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string OrderingProvider {
get {
return this.orderingProviderField;
}
set {
this.orderingProviderField = value;
}
}
}
No comments:
Post a Comment