Employee类
using System;
using System.Collections.Generic;
using System.Text;
namespace EmployeeClient
...{
class Employee
...{
public int EmployeeID;
public int LastNameSize;
public string LastName;
public 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 += 4;
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;
}
}
}
服务器

using System;
using System.Collections.Generic;
using System.Text;
namespace EmployeeSrvr
...{
class Employee
...{
public int EmployeeID;
public int LastNameSize;
public string LastName;
public 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 += 4;
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;
}
}
}

客户端
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace EmployeeClient
...{
class Program
...{
static void Main(string[] args)
...{
Employee emp1 = new Employee();
Employee emp2 = new Employee();
TcpClient client;
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;
try
...{
client = new TcpClient("127.0.0.1", 9050);
}
catch (SocketException)
...{
Console.WriteLine("Unable to connect to server");
return;
}
NetworkStream ns = client.GetStream();
byte[] data = emp1.GetBytes();
int size = emp1.size;
byte[] packsize = new byte[2];
Console.WriteLine("packet size = {0}", size);
packsize = BitConverter.GetBytes(size);
ns.Write(packsize, 0, 2);
ns.Write(data, 0, size);
ns.Flush();
data = emp2.GetBytes();
size = emp2.size;
packsize = new byte[2];
Console.WriteLine("packet size={0}", size);
packsize = BitConverter.GetBytes(size);
ns.Write(packsize, 0, 2);
ns.Write(data, 0, size);
ns.Flush();
client.Close();
}
}
}
9561

被折叠的 条评论
为什么被折叠?



