Object data sender/Receiver : Object Send Receive « Network « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Network » Object Send Receive 
33.9.1.Object data sender/Receiver
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class EmployeeClient
{
   public static void Main()
   {
      Employee emp1 = new Employee();
      Employee emp2 = new Employee();
      TcpClient client;

      emp1.ID = 1;
      emp1.LastName = "D";
      emp1.FirstName = "C";
      emp1.YearsService = 12;
      emp1.Salary = 35.50;

      emp2.ID = 2;
      emp2.LastName = "B";
      emp2.FirstName = "A";
      emp2.YearsService = 9;
      emp2.Salary = 23.30;

      client = new TcpClient("127.0.0.1"9050);
      NetworkStream ns = client.GetStream();

      byte[] data = emp1.GetBytes();
      int size = emp1.size;
      byte[] packsize = new byte[2];
      packsize = BitConverter.GetBytes(size);
      ns.Write(packsize, 02);
      ns.Write(data, 0, size);
      ns.Flush();

      data = emp2.GetBytes();
      size = emp2.size;
      packsize = new byte[2];
      packsize = BitConverter.GetBytes(size);
      ns.Write(packsize, 02);
      ns.Write(data, 0, size);
      ns.Flush();
   
      ns.Close();
      client.Close();    
   }
}




class Employee
{
   public int ID;
   private int LastNameSize;
   public string LastName;
   private int FirstNameSize;
   public string FirstName;
   public int YearsService;
   public double Salary;
   public int size;

   public Employee()
   {
   }

   public Employee(byte[] data)
   {
      int place = 0;
      ID = BitConverter.ToInt32(data, place);
      place += 4;
      LastNameSize = BitConverter.ToInt32(data, place);
      place += 4;
      LastName = Encoding.ASCII.GetString(data, place, LastNameSize);
      place = place + LastNameSize;
      FirstNameSize = BitConverter.ToInt32(data, place);
      place += 4;
      FirstName = Encoding.ASCII.GetString(data, place, FirstNameSize);
      place += FirstNameSize;
      YearsService = BitConverter.ToInt32(data, place);
      place += 4;
      Salary = BitConverter.ToDouble(data, place);
   }

   public byte[] GetBytes()
   {
      byte[] data = new byte[1024];
      int place = 0;
      Buffer.BlockCopy(BitConverter.GetBytes(ID)0, data, place, 4);
      place += 4;
      Buffer.BlockCopy(BitConverter.GetBytes(LastName.Length)0, data, place, 4);
      place += 4;
      Buffer.BlockCopy(Encoding.ASCII.GetBytes(LastName)0, data, place, LastName.Length);
      place += LastName.Length;
      Buffer.BlockCopy(BitConverter.GetBytes(FirstName.Length)0, data, place, 4);
      place += 4;
      Buffer.BlockCopy(Encoding.ASCII.GetBytes(FirstName)0, data, place, FirstName.Length);
      place += FirstName.Length;
      Buffer.BlockCopy(BitConverter.GetBytes(YearsService)0, data, place, 4);
      place += 4;
      Buffer.BlockCopy(BitConverter.GetBytes(Salary)0, data, place, 8);
      place += 8;
      size = place;
      return data;
   }
   public void Display(){
      Console.WriteLine("ID = {0}", ID);
      Console.WriteLine("LastName = {0}", LastName);
      Console.WriteLine("FirstName = {0}", FirstName);
      Console.WriteLine("YearsService = {0}", YearsService);
      Console.WriteLine("Salary = {0}\n", Salary);
   }
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class EmployeeSrvr
{
   public static void Main()
   {
      byte[] data = new byte[1024];
      TcpListener server = new TcpListener(9050);

      server.Start();
      TcpClient client = server.AcceptTcpClient();
      NetworkStream ns = client.GetStream();

      byte[] size = new byte[2];
      int recv = ns.Read(size, 02);
      int packsize = BitConverter.ToInt16(size, 0);
      recv = ns.Read(data, 0, packsize);

      Employee emp1 = new Employee(data);
      emp1.Display();
 
      size = new byte[2];
      recv = ns.Read(size, 02);
      packsize = BitConverter.ToInt16(size, 0);
      data = new byte[packsize];
      recv = ns.Read(data, 0, packsize);

      Employee emp2 = new Employee(data);
      emp2.Display();

      ns.Close();
      client.Close();
      server.Stop();

   }
}

class Employee
{
   public int ID;
   private int LastNameSize;
   public string LastName;
   private int FirstNameSize;
   public string FirstName;
   public int YearsService;
   public double Salary;
   public int size;

   public Employee()
   {
   }

   public Employee(byte[] data)
   {
      int place = 0;
      ID = BitConverter.ToInt32(data, place);
      place += 4;
      LastNameSize = BitConverter.ToInt32(data, place);
      place += 4;
      LastName = Encoding.ASCII.GetString(data, place, LastNameSize);
      place = place + LastNameSize;
      FirstNameSize = BitConverter.ToInt32(data, place);
      place += 4;
      FirstName = Encoding.ASCII.GetString(data, place, FirstNameSize);
      place += FirstNameSize;
      YearsService = BitConverter.ToInt32(data, place);
      place += 4;
      Salary = BitConverter.ToDouble(data, place);
   }

   public byte[] GetBytes()
   {
      byte[] data = new byte[1024];
      int place = 0;
      Buffer.BlockCopy(BitConverter.GetBytes(ID)0, data, place, 4);
      place += 4;
      Buffer.BlockCopy(BitConverter.GetBytes(LastName.Length)0, data, place, 4);
      place += 4;
      Buffer.BlockCopy(Encoding.ASCII.GetBytes(LastName)0, data, place, LastName.Length);
      place += LastName.Length;
      Buffer.BlockCopy(BitConverter.GetBytes(FirstName.Length)0, data, place, 4);
      place += 4;
      Buffer.BlockCopy(Encoding.ASCII.GetBytes(FirstName)0, data, place, FirstName.Length);
      place += FirstName.Length;
      Buffer.BlockCopy(BitConverter.GetBytes(YearsService)0, data, place, 4);
      place += 4;
      Buffer.BlockCopy(BitConverter.GetBytes(Salary)0, data, place, 8);
      place += 8;
      size = place;
      return data;
   }
   public void Display(){
      Console.WriteLine("ID = {0}", ID);
      Console.WriteLine("LastName = {0}", LastName);
      Console.WriteLine("FirstName = {0}", FirstName);
      Console.WriteLine("YearsService = {0}", YearsService);
      Console.WriteLine("Salary = {0}\n", Salary);
   }
}
33.9.Object Send Receive
33.9.1.Object data sender/Receiver
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.