Csharp/C Sharp/Network/Data Send Receive — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:47, 26 мая 2010

Better Data Receiver

/*
C# Network Programming 
by Richard Blum
Publisher: Sybex 
ISBN: 0782141765
*/
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
public class BetterDataRcvr
{
   private SerialEmployee RecvData(NetworkStream strm)
   {
      MemoryStream memstrm = new MemoryStream();
      byte[] data = new byte[4];
      int recv = strm.Read(data, 0, 4);
      int size = BitConverter.ToInt32(data, 0);
      int offset = 0;
      while(size > 0)
      {
         data = new byte[1024];
         recv = strm.Read(data, 0, size);
         memstrm.Write(data, offset, recv);
         offset += recv;
         size -= recv;
      }
      IFormatter formatter = new SoapFormatter();
      memstrm.Position = 0;
      SerialEmployee emp = (SerialEmployee)formatter.Deserialize(memstrm);
      memstrm.Close();
      return emp;
   }      
   public BetterDataRcvr()
   {
      TcpListener server = new TcpListener(9050);
      server.Start();
      TcpClient client = server.AcceptTcpClient();
      NetworkStream strm = client.GetStream();
      SerialEmployee emp1 = RecvData(strm);
      Console.WriteLine("emp1.EmployeeID = {0}", emp1.EmployeeID);
      Console.WriteLine("emp1.LastName = {0}", emp1.LastName);
      Console.WriteLine("emp1.FirstName = {0}", emp1.FirstName);
      Console.WriteLine("emp1.YearsService = {0}", emp1.YearsService);
      Console.WriteLine("emp1.Salary = {0}\n", emp1.Salary);
      SerialEmployee emp2 = RecvData(strm);
      Console.WriteLine("emp2.EmployeeID = {0}", emp2.EmployeeID);
      Console.WriteLine("emp2.LastName = {0}", emp2.LastName);
      Console.WriteLine("emp2.FirstName = {0}", emp2.FirstName);
      Console.WriteLine("emp2.YearsService = {0}", emp2.YearsService);
      Console.WriteLine("emp2.Salary = {0}", emp2.Salary);
      strm.Close();
      server.Stop();
   }
   public static void Main()
   {
      BetterDataRcvr bdr = new BetterDataRcvr();
   }
}

[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;
   }
}


Better Data Sender

/*
C# Network Programming 
by Richard Blum
Publisher: Sybex 
ISBN: 0782141765
*/
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
public class BetterDataSender
{
   private void SendData(NetworkStream strm, SerialEmployee emp)
   {
      IFormatter formatter = new SoapFormatter();
      MemoryStream memstrm = new MemoryStream();
      formatter.Serialize(memstrm, emp);
      byte[] data = memstrm.GetBuffer();
      int memsize = (int)memstrm.Length;
      byte[] size = BitConverter.GetBytes(memsize);
      strm.Write(size, 0, 4);
      strm.Write(data, 0, memsize);
      strm.Flush();
      memstrm.Close();
   }
   public BetterDataSender()
   {
      SerialEmployee emp1 = new SerialEmployee();
      SerialEmployee emp2 = new SerialEmployee();
      emp1.EmployeeID = 1;
      emp1.LastName = "Blum";
      emp1.FirstName = "Katie Jane";
      emp1.YearsService = 12;
      emp1.Salary = 35000.50;
      emp2.EmployeeID = 2;
      emp2.LastName = "Blum";
      emp2.FirstName = "Jessica";
      emp2.YearsService = 9;
      emp2.Salary = 23700.30;
      TcpClient client = new TcpClient("127.0.0.1", 9050);
      NetworkStream strm = client.GetStream();
      SendData(strm, emp1);
      SendData(strm, emp2);
      strm.Close();
      client.Close();
   }
   public static void Main()
   {
      BetterDataSender bds = new BetterDataSender();
   }
}

[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;
   }
}


Binary Data Receiver

/*
C# Network Programming 
by Richard Blum
Publisher: Sybex 
ISBN: 0782141765
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public class BinaryDataRcvr
{
   public static void Main()
   {
      TcpListener server = new TcpListener(9050);
      server.Start();
      TcpClient client = server.AcceptTcpClient();
      NetworkStream strm = client.GetStream();
      IFormatter formatter = new BinaryFormatter();
      SerialEmployee emp1 = (SerialEmployee)formatter.Deserialize(strm);
      Console.WriteLine("emp1.EmployeeID = {0}", emp1.EmployeeID);
      Console.WriteLine("emp1.LastName = {0}", emp1.LastName);
      Console.WriteLine("emp1.FirstName = {0}", emp1.FirstName);
      Console.WriteLine("emp1.YearsService = {0}", emp1.YearsService);
      Console.WriteLine("emp1.Salary = {0}\n", emp1.Salary);
      SerialEmployee emp2 = (SerialEmployee)formatter.Deserialize(strm);
      Console.WriteLine("emp2.EmployeeID = {0}", emp2.EmployeeID);
      Console.WriteLine("emp2.LastName = {0}", emp2.LastName);
      Console.WriteLine("emp2.FirstName = {0}", emp2.FirstName);
      Console.WriteLine("emp2.YearsService = {0}", emp2.YearsService);
      Console.WriteLine("emp2.Salary = {0}", emp2.Salary);
      strm.Close();
      server.Stop();
   }
}

[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;
   }
}


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;
   }
}