• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

在C#中使用PHPRPC(转)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一个已经完成的访问量统计项目,发上来记录一下,服务端和客户端都使用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());    
            }    
        }    
    }    
}   

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#实现之(自动更新)发布时间:2022-07-13
下一篇:
C# 注册 Windows 热键发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap