C#下用TcpClient传输数组

来源:互联网 发布:苹果mac系统修复教程 编辑:程序博客网 时间:2024/05/18 02:52

       这个还算简单,在C#中用TcpListener  TcpClient NetworkStream这三个类可以很好的实现服务端和客户端的通信。至于传送对象类型就要用到序列化和反序列化了。关于这方面的文章可见冰点的《C# 序列化和反序列化Ojecthttp://blog.myspace.cn/index.cfm?fuseaction=blog.view&friendID=1306783616&blogID=401530033

 

下面又开始贴代码-_-!

服务端

  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.IO;
  5. using System.Text;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.Runtime.Serialization; 
  8. namespace SocketDis
  9. {
  10.     class server
  11.     {        
  12.         static void Main()
  13.         {
  14.             
  15.             TcpListener listener = new TcpListener(6161);
  16.             listener.Start();
  17.             Console.Write("等待连接.................");            
  18.             TcpClient client = listener.AcceptTcpClient();
  19.             Console.WriteLine("已连接.");
  20.             NetworkStream ns = client.GetStream();                
  21.             try
  22.             {
  23.                 //反序列化
  24.                 IFormatter formatter = new BinaryFormatter();
  25.                 int[] a=((int[])formatter.Deserialize(ns));
  26.                 //计算结果
  27.                 int result = cout(a);
  28.                 //序列化并回发数据                
  29.                 formatter = new BinaryFormatter();
  30.                 formatter.Serialize(ns, result);             
  31.             }
  32.              catch (Exception e)
  33.              {
  34.                 Console.WriteLine(e.ToString());
  35.              }
  36.              Console.ReadKey(true);
  37.         }
  38.         /// <summary>
  39.         /// 计算
  40.         /// </summary>
  41.         /// <param name="data">需要计算的数组</param>
  42.         /// <returns>计算结果</returns>
  43.         public static int cout(int[] data)
  44.         {
  45.             int total = 0;
  46.             foreach (int b in data)
  47.             {
  48.                 total += b;
  49.             }
  50.             return total;
  51.         }
  52.     }
  53. }

客户端:

  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. using System.IO;
  6. using System.Text;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.Runtime.Serialization; 
  9. namespace SocketDis
  10. {
  11.     class client
  12.     {
  13.         static void Main()
  14.         {
  15.             string ip = "127.0.0.1";
  16.             string port = "6161";
  17.             IPAddress serverIp = IPAddress.Parse(ip);
  18.             int serverPort = Convert.ToInt32(port);
  19.             IPEndPoint iep = new IPEndPoint(serverIp, serverPort);//主机终结点
  20.             TcpClient client = new TcpClient();
  21.             client.Connect(iep);//连接服务端
  22.             NetworkStream ns = client.GetStream();//网络数据流
  23.             int[] data ={100,234,116};
  24.             //序列化数组并发送
  25.             IFormatter formatter = new BinaryFormatter();
  26.             formatter.Serialize(ns, data);
  27.             Console.WriteLine("等待主机返回结果......");
  28.             Thread.Sleep(1000);
  29.             formatter = new BinaryFormatter();
  30.             int result = (int)formatter.Deserialize(ns);
  31.             Console.WriteLine("计算结果是:"+result.ToString());
  32.             Console.ReadKey(true);
  33.         }
  34.     }
  35. }
原创粉丝点击