Binary Data Sender : Data Send Receive « Network « C# / C Sharp






Binary Data Sender

using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public class BinaryDataSender
{
   public static void Main()
   {
      SerialEmployee emp1 = new SerialEmployee();
      SerialEmployee emp2 = new SerialEmployee();

      emp1.EmployeeID = 1;
      emp1.LastName = "B";
      emp1.FirstName = "K";
      emp1.YearsService = 12;
      emp1.Salary = 35000.50;

      emp2.EmployeeID = 2;
      emp2.LastName = "B";
      emp2.FirstName = "J";
      emp2.YearsService = 9;
      emp2.Salary = 23700.30;

      TcpClient client = new TcpClient("127.0.0.1", 9050);
      IFormatter formatter = new BinaryFormatter();
      NetworkStream strm = client.GetStream();

      formatter.Serialize(strm, emp1);
      formatter.Serialize(strm, emp2);
      strm.Close();
      client.Close();
   }
}



[Serializable]
public class SerialEmployee
{
   public int EmployeeID;
   public string LastName;
   public string FirstName;
   public int YearsService;
   public double Salary;

   public SerialEmployee()
   {
      EmployeeID = 0;
      LastName = null;
      FirstName = null;
      YearsService = 0;
      Salary = 0.0;
   }
}

           
       








Related examples in the same category

1.Better Data Receiver
2.Better Data Sender
3.Binary Data Receiver