Employee Server : TCP Server : Network C# Source Code


Custom Search

C# Source Code » Network » TCP Server »

 

Employee Server








    

/*
C# Network Programming 
by Richard Blum

Publisher: Sybex 
ISBN: 0782141765
*/
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

public 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, 0, 2);
      int packsize = BitConverter.ToInt16(size, 0);
      Console.WriteLine("packet size = {0}", packsize);
      recv = ns.Read(data, 0, packsize);

      Employee emp1 = new Employee(data);
      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);

      size = new byte[2];
      recv = ns.Read(size, 0, 2);
      packsize = BitConverter.ToInt16(size, 0);
      data = new byte[packsize];
      Console.WriteLine("packet size = {0}", packsize);
      recv = ns.Read(data, 0, packsize);

      Employee emp2 = new Employee(data);
      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);

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

   }
}

public class Employee {
   public int EmployeeID;
   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;
      EmployeeID = 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(EmployeeID), 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;
   }
}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Network
» TCP Server