///说明:下列代码分别复制进入三个控制台程序中执行
///第一步:新建一个类库用于序列化后传递
[System.Serializable]
public class filetest
{
private string name;
private int age;
private string message;
public string Message
{
get { return message; }
set { message = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
///第二步:服务器端代码
class program
{
static void Main()
{
new program().gettarget();
}
System.Collections.Generic.Dictionary<string, System.Net.Sockets.TcpClient> dr = new System.Collections.Generic.Dictionary<string, System.Net.Sockets.TcpClient>();
//得到连接对象后,新建线程并来执行新的连接(读和写)
private void gettarget()
{
System.Net.Sockets.TcpListener tl = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Parse("127.0.0.1"), 2009);
tl.Start();
while (true)
{
System.Net.Sockets.TcpClient tc = tl.AcceptTcpClient();
System.DateTime dt=System.DateTime.Now;
string s = dt.ToString("yyMMddhhmmss")+dt.Millisecond.ToString();
dr.Add(s, tc);
System.Threading.Thread th = new System.Threading.Thread(GetMessage);
th.Name = s;
th.IsBackground = true;
th.Start(tc);
System.Threading.Thread.Sleep(1);
}
}
private void GetMessage(object o)
{
System.Net.Sockets.TcpClient tc = (System.Net.Sockets.TcpClient)o;
System.Net.Sockets.NetworkStream ns = tc.GetStream();
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
try
{
while (true)
{
filetest ft = (filetest)formatter.Deserialize(ns);
System.Console.WriteLine(ft.Message);
foreach (System.Collections.Generic.KeyValuePair<string, System.Net.Sockets.TcpClient> scg in dr)
{
System.Net.Sockets.TcpClient tt = scg.Value;
System.Net.Sockets.NetworkStream nns = tt.GetStream();
formatter.Serialize(nns, ft);
}
}
}
catch (System.Exception e)
{
dr.Remove(System.Threading.Thread.CurrentThread.Name);
System.Threading.Thread.CurrentThread.Abort();
}
}
}
///第三步:客户端代码
class program
{
static void Main()
{
System.Net.Sockets.TcpClient tc = new System.Net.Sockets.TcpClient();
tc.Connect(System.Net.IPAddress.Parse("127.0.0.1"), 2009);
System.Net.Sockets.NetworkStream ns = tc.GetStream();
System.Threading.Thread th = new System.Threading.Thread(SendMessage);
th.IsBackground = true;
th.Start(ns);
System.Threading.Thread th2 = new System.Threading.Thread(GetMessage);
th.IsBackground = true;
th2.Start(ns);
}
private static void SendMessage(object o)
{
System.Net.Sockets.NetworkStream ns = (System.Net.Sockets.NetworkStream)o;
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
try
{
while (true)
{
string s = System.Console.ReadLine();
filetest ft = new filetest();
ft.Message = s;
formatter.Serialize(ns, ft);
}
}
catch (System.Exception e)
{
System.Console.WriteLine("出现错误:{0}",e.Message);
}
}
private static void GetMessage(object o)
{
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.Net.Sockets.NetworkStream nw = (System.Net.Sockets.NetworkStream)o;
try
{
while (true)
{
filetest ft = (filetest)formatter.Deserialize(nw);
System.Console.WriteLine(ft.Message);
}
}
catch (System.Exception e)
{
System.Console.WriteLine("出现错误:{0}",e.Message);
System.Console.ReadLine();
System.Environment.Exit(0);
}
}
}