你的位置:首页 > ASP.net教程

[ASP.net教程]介绍开源的.net通信框架NetworkComms框架之三 传递List


原文网址: http://www.cnblogs.com/csdev

Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  开源地址是:https://github.com/MarcFletcher/NetworkComms.Net

这一节讲一下,如何从服务器获取列表数据

示例图:

客户单点击 获取数据 从服务器返回数据

客户端代码:

 private void button4_Click(object sender, EventArgs e)    {      //GetUserList 是数据的请求消息类型      //ResUserList 是服务器端返回消息类型 当服务器返回ResUserList的数据时 客户端自动对应获取      IList<User> listUser = newTcpConnection.SendReceiveObject<string, IList<User>>("GetUserList", "ResUserList", 5000, "100");      BindData(listUser);    }    public void BindData(IList<User> theUsers)    {      listView1.Items.Clear();      listView1.Columns.Clear();      listView1.View = View.Details;  //新增的 详细方式      listView1.AllowColumnReorder = true;      listView1.FullRowSelect = true;      listView1.GridLines = true;           listView1.Columns.Add("用户ID", 100, HorizontalAlignment.Left);      listView1.Columns.Add("用户名", 100, HorizontalAlignment.Left);      foreach (User theUser in theUsers)      {        ListViewItem li = new ListViewItem();        li.SubItems[0].Text = theUser.UserID.ToString();               li.SubItems.Add(theUser.Name);        listView1.Items.Add(li);      }        }

服务器端代码:

 private void button1_Click(object sender, EventArgs e)    {      //IP地址和端口      IPEndPoint thePoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text));      //开始监听此IP和端口 使用TCP协议      Connection.StartListening(ConnectionType.TCP, thePoint);      NetworkComms.AppendGlobalIncomingPacketHandler<string>("GetName", IncomingMsgHandle);      NetworkComms.AppendGlobalIncomingPacketHandler<User>("UserLong", IncoingHandleLogin);      NetworkComms.AppendGlobalIncomingPacketHandler<string>("GetUserList", IncoingHandleGetUserList);            button1.Text = "已经开始监听";    }

 private void IncoingHandleGetUserList(PacketHeader header, Connection connection, string param)    {       //模拟一些数据      IList<User> userList=new List<User>();      User user1=new User ();      user1.UserID="10000";      user1.Name="天涯共此时";      User user2=new User();      user2.UserID="20000";      user2.Name="海上生明月";      if (param == "100")      {        userList.Add(user1);        userList.Add(user2);      }      else      {        userList.Add(user1);      }       connection.SendObject("ResUserList", userList);    }

 源码下载:http://pan.baidu.com/s/1geXMSef