title: How to send variables/objects in TCP Socket?
body: <p>How can I send variables/objects/data from one program to another in a LAN using TCP Socket? In particular, I want to send variables like TreeNode and ListViewItem. How can I do this? How will a sender program convert the variable to a form which it can send to another program in a LAN? And how will the receiver program bring back the sent variable to it's original form? </p>
  
  <p>EDIT: Found the following code on a website which is no longer available and requested to remove the link.</p>
  
  <pre><code>        // Convert an object to a byte array
          private byte[] ObjectToByteArray(Object obj)
          {
              if (obj == null)
                  return null;
              BinaryFormatter bf = new BinaryFormatter();
              MemoryStream ms = new MemoryStream();
              bf.Serialize(ms, obj);
              return ms.ToArray();
          }
  
          // Convert a byte array to an Object
          private Object ByteArrayToObject(byte[] arrBytes)
          {
              MemoryStream memStream = new MemoryStream();
              BinaryFormatter binForm = new BinaryFormatter();
              memStream.Write(arrBytes, 0, arrBytes.Length);
              memStream.Seek(0, SeekOrigin.Begin);
              Object obj = (Object)binForm.Deserialize(memStream);
              return obj;
          }
  </code></pre>