分类

链接

2016 年 3 月
 123456
78910111213
14151617181920
21222324252627
28293031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > .NET > 正文
共享办公室出租
在C#中使用PHPRPC
.NET 暂无评论 阅读(1,760)
  1. PHPRPC 是一个轻型的、安全的、跨网际的、跨语言的、跨平台的、跨环境的、跨域的、支持复杂对象传输的、支持引用参数传递的、支持内容输出重定向的、支持分级错误处理的、支持会话的、面向服务的高性能远程过程调用协议。
  2. 一个已经完成的访问量统计项目,发上来记录一下,服务端和客户端都使用C#编写
  3.  
  4. 服务端代码:
  5.  
  6. using System;    
  7. using System.Data;    
  8. using System.Configuration;    
  9. using System.Collections;    
  10. using System.Web;    
  11. using System.Web.Security;    
  12. using System.Web.UI;    
  13. using System.Web.UI.WebControls;    
  14. using System.Web.UI.WebControls.WebParts;    
  15. using System.Web.UI.HtmlControls;    
  16. using System.Data.SqlClient;    
  17.    
  18.    
  19. public partial class phprpc : System.Web.UI.Page    
  20. {    
  21.     protected void Page_Load(object sender, EventArgs e)    
  22.     {    
  23.         org.phprpc.PHPRPC_Server server = new org.phprpc.PHPRPC_Server();    
  24.         server.Add(new string[] { "add", "sub", "getRv","getType","getPv","getUv" }, new Phpox());    
  25.         server.Add("hello", typeof(Phpox));    
  26.         server.Start();    
  27.     }    
  28. }    
  29.    
  30. public class Phpox    
  31. {    
  32.     SqlConnection conn;    
  33.     public Phpox()    
  34.     {    
  35.         conn = new SqlConnection(@"Server=(local);User=sa;PWD=00;Database=channelCount");    
  36.         conn.Open();    
  37.     }    
  38.    
  39.     public ArrayList getType()    
  40.     {    
  41.         string sql = "select type from [view] group by type";    
  42.         SqlCommand comm = conn.CreateCommand();    
  43.         comm.CommandText = sql;    
  44.         SqlDataReader reader = comm.ExecuteReader();    
  45.         ArrayList al = new ArrayList();    
  46.         while (reader.Read())    
  47.         {    
  48.             al.Add(reader["type"]);    
  49.         }    
  50.         reader.Close();    
  51.         conn.Close();    
  52.         return al;    
  53.     }    
  54.    
  55.     public int getPv(string type)    
  56.     {    
  57.         string sql = "select count(*) AS num from [view] where type = '"+type+"'";    
  58.         SqlCommand comm = conn.CreateCommand();    
  59.         comm.CommandText = sql;    
  60.         SqlDataReader reader = comm.ExecuteReader();    
  61.         reader.Read();    
  62.         int num = Convert.ToInt32(reader["num"]);    
  63.         reader.Close();    
  64.         conn.Close();    
  65.         return num;    
  66.     }    
  67.    
  68.     public int getUv(string type)    
  69.     {    
  70.         string sql = "select count(DISTINCT ip) AS num from [view] where type = '" + type + "'";    
  71.         SqlCommand comm = conn.CreateCommand();    
  72.         comm.CommandText = sql;    
  73.         SqlDataReader reader = comm.ExecuteReader();    
  74.         reader.Read();    
  75.         int num = Convert.ToInt32(reader["num"]);    
  76.         reader.Close();    
  77.         conn.Close();    
  78.         return num;    
  79.     }    
  80.    
  81.     public ArrayList getRv(string type)    
  82.     {    
  83.         string sql = "select ip,count(*) AS num from [view] where type = '" 
  84.                                     + type + "' group by ip order by num desc";    
  85.         SqlCommand comm = conn.CreateCommand();    
  86.         comm.CommandText = sql;    
  87.         SqlDataReader reader = comm.ExecuteReader();    
  88.         ArrayList al = new ArrayList();    
  89.         ArrayList al1 = new ArrayList();    
  90.         int i = 0;    
  91.         while (reader.Read())    
  92.         {    
  93.             Hashtable ht = new Hashtable();    
  94.             int num = Convert.ToInt32(reader["num"]);    
  95.             string ip = Convert.ToString(reader["ip"]);    
  96.             if (num > 1)    
  97.             {    
  98.                 i++;    
  99.             }    
  100.             ht.Add("ip", ip);    
  101.             ht.Add("num",num);    
  102.             al1.Add(ht);    
  103.         }    
  104.         al.Add(i);    
  105.         al.Add(al1);    
  106.         reader.Close();    
  107.         conn.Close();    
  108.         return al;    
  109.     }    
  110.    
  111.     public double add(double a, double b)    
  112.     {    
  113.         return a + b;    
  114.     }    
  115.     public string add(string a, string b)    
  116.     {    
  117.         return a + b;    
  118.     }    
  119.     public int sub(int a, int b)    
  120.     {    
  121.         return a - b;    
  122.     }    
  123.     public int inc(ref int n)    
  124.     {    
  125.         return n++;    
  126.     }    
  127.     public static string hello(string name, System.IO.TextWriter output)    
  128.     {    
  129.         string result = String.Concat("hello ", name);    
  130.         output.Write("output: " + result);    
  131.         return result;    
  132.     }    
  133. }  
  134.      
  135. 客户端代码:
  136.  
  137. using System;    
  138. using System.Collections.Generic;    
  139. using System.ComponentModel;    
  140. using System.Data;    
  141. using System.Drawing;    
  142. using System.Text;    
  143. using System.Windows.Forms;    
  144. using org.phprpc;    
  145. using org.phprpc.util;    
  146. using System.Collections;    
  147.    
  148. namespace channelCount    
  149. {    
  150.     public interface Phpox    
  151.     {    
  152.         double add(double a, double b);    
  153.         string add(string a, string b);    
  154.         int sub(int a, int b);    
  155.         Decimal inc(ref String n);    
  156.         string hello(string name);    
  157.         ArrayList getType();    
  158.         int getPv(string type);    
  159.         int getUv(string type);    
  160.         ArrayList getRv(string type);    
  161.     }    
  162.    
  163.    
  164.     public partial class Form1 : Form    
  165.     {    
  166.         PHPRPC_Client rpc;    
  167.         Phpox p;    
  168.         public Form1()    
  169.         {    
  170.             InitializeComponent();    
  171.             rpc = new PHPRPC_Client("http://www.xxxx.cn/channelCount/ssss.aspx");    
  172.             p = (Phpox)rpc.UseService(typeof(Phpox));    
  173.         }    
  174.    
  175.         private void Form1_Load(object sender, EventArgs e)    
  176.         {    
  177.             ArrayList ht = (ArrayList)p.getType();    
  178.             foreach (byte[] de in ht)    
  179.             {    
  180.                 string str = System.Text.Encoding.Default.GetString(de);    
  181.                 comboBox1.Items.Add(str);    
  182.             }    
  183.         }    
  184.    
  185.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)    
  186.         {    
  187.             string type = comboBox1.SelectedItem.ToString();    
  188.             int num = (int)p.getPv(type);    
  189.             lblPv.Text = num.ToString();    
  190.             //int uv = (int)p.getUv(type);    
  191.             //lblUv.Text = uv.ToString();    
  192.             ArrayList rv = (ArrayList)p.getRv(type);    
  193.             lblRv.Text = rv[0].ToString();    
  194.             lblUv.Text = rv[2].ToString();    
  195.             //lblPv.Text = rv[3].ToString();    
  196.             ArrayList row = PHPConvert.ToArrayList(rv[1]);    
  197.             listView1.Items.Clear();    
  198.             for(int i=0;i<row.Count;i++)    
  199.             {    
  200.                 Hashtable ht = PHPConvert.ToHashtable(row[i]);    
  201.                 string ip = System.Text.Encoding.Default.GetString((byte[])ht["ip"]);    
  202.                 int num1 = (int)ht["num"];    
  203.                 listView1.Items.Add(ip);    
  204.                 listView1.Items[i].SubItems.Add(num1.ToString());    
  205.             }    
  206.         }    
  207.     }    
  208. }   

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:在C#中使用PHPRPC | Bruce's Blog

发表评论

留言无头像?