
在C#中使用PHPRPC
- PHPRPC 是一个轻型的、安全的、跨网际的、跨语言的、跨平台的、跨环境的、跨域的、支持复杂对象传输的、支持引用参数传递的、支持内容输出重定向的、支持分级错误处理的、支持会话的、面向服务的高性能远程过程调用协议。
- 一个已经完成的访问量统计项目,发上来记录一下,服务端和客户端都使用C#编写
- 服务端代码:
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Data.SqlClient;
- public partial class phprpc : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- org.phprpc.PHPRPC_Server server = new org.phprpc.PHPRPC_Server();
- server.Add(new string[] { "add", "sub", "getRv","getType","getPv","getUv" }, new Phpox());
- server.Add("hello", typeof(Phpox));
- server.Start();
- }
- }
- public class Phpox
- {
- SqlConnection conn;
- public Phpox()
- {
- conn = new SqlConnection(@"Server=(local);User=sa;PWD=00;Database=channelCount");
- conn.Open();
- }
- public ArrayList getType()
- {
- string sql = "select type from [view] group by type";
- SqlCommand comm = conn.CreateCommand();
- comm.CommandText = sql;
- SqlDataReader reader = comm.ExecuteReader();
- ArrayList al = new ArrayList();
- while (reader.Read())
- {
- al.Add(reader["type"]);
- }
- reader.Close();
- conn.Close();
- return al;
- }
- public int getPv(string type)
- {
- string sql = "select count(*) AS num from [view] where type = '"+type+"'";
- SqlCommand comm = conn.CreateCommand();
- comm.CommandText = sql;
- SqlDataReader reader = comm.ExecuteReader();
- reader.Read();
- int num = Convert.ToInt32(reader["num"]);
- reader.Close();
- conn.Close();
- return num;
- }
- public int getUv(string type)
- {
- string sql = "select count(DISTINCT ip) AS num from [view] where type = '" + type + "'";
- SqlCommand comm = conn.CreateCommand();
- comm.CommandText = sql;
- SqlDataReader reader = comm.ExecuteReader();
- reader.Read();
- int num = Convert.ToInt32(reader["num"]);
- reader.Close();
- conn.Close();
- return num;
- }
- public ArrayList getRv(string type)
- {
- string sql = "select ip,count(*) AS num from [view] where type = '"
- + type + "' group by ip order by num desc";
- SqlCommand comm = conn.CreateCommand();
- comm.CommandText = sql;
- SqlDataReader reader = comm.ExecuteReader();
- ArrayList al = new ArrayList();
- ArrayList al1 = new ArrayList();
- int i = 0;
- while (reader.Read())
- {
- Hashtable ht = new Hashtable();
- int num = Convert.ToInt32(reader["num"]);
- string ip = Convert.ToString(reader["ip"]);
- if (num > 1)
- {
- i++;
- }
- ht.Add("ip", ip);
- ht.Add("num",num);
- al1.Add(ht);
- }
- al.Add(i);
- al.Add(al1);
- reader.Close();
- conn.Close();
- return al;
- }
- public double add(double a, double b)
- {
- return a + b;
- }
- public string add(string a, string b)
- {
- return a + b;
- }
- public int sub(int a, int b)
- {
- return a - b;
- }
- public int inc(ref int n)
- {
- return n++;
- }
- public static string hello(string name, System.IO.TextWriter output)
- {
- string result = String.Concat("hello ", name);
- output.Write("output: " + result);
- return result;
- }
- }
- 客户端代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using org.phprpc;
- using org.phprpc.util;
- using System.Collections;
- namespace channelCount
- {
- public interface Phpox
- {
- double add(double a, double b);
- string add(string a, string b);
- int sub(int a, int b);
- Decimal inc(ref String n);
- string hello(string name);
- ArrayList getType();
- int getPv(string type);
- int getUv(string type);
- ArrayList getRv(string type);
- }
- public partial class Form1 : Form
- {
- PHPRPC_Client rpc;
- Phpox p;
- public Form1()
- {
- InitializeComponent();
- rpc = new PHPRPC_Client("http://www.xxxx.cn/channelCount/ssss.aspx");
- p = (Phpox)rpc.UseService(typeof(Phpox));
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- ArrayList ht = (ArrayList)p.getType();
- foreach (byte[] de in ht)
- {
- string str = System.Text.Encoding.Default.GetString(de);
- comboBox1.Items.Add(str);
- }
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- string type = comboBox1.SelectedItem.ToString();
- int num = (int)p.getPv(type);
- lblPv.Text = num.ToString();
- //int uv = (int)p.getUv(type);
- //lblUv.Text = uv.ToString();
- ArrayList rv = (ArrayList)p.getRv(type);
- lblRv.Text = rv[0].ToString();
- lblUv.Text = rv[2].ToString();
- //lblPv.Text = rv[3].ToString();
- ArrayList row = PHPConvert.ToArrayList(rv[1]);
- listView1.Items.Clear();
- for(int i=0;i<row.Count;i++)
- {
- Hashtable ht = PHPConvert.ToHashtable(row[i]);
- string ip = System.Text.Encoding.Default.GetString((byte[])ht["ip"]);
- int num1 = (int)ht["num"];
- listView1.Items.Add(ip);
- listView1.Items[i].SubItems.Add(num1.ToString());
- }
- }
- }
- }
============ 欢迎各位老板打赏~ ===========

