DigitalDream

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  16 随笔 :: 0 文章 :: 5 评论 :: 0 引用

2007年5月23日 #

   今天整理硬盘时发现自己机子上有Ubuntu上安装Beryl的详细方法,已经不记的是从网上什么地方copy下来的,依稀记得好像是某位仁兄对官方网上安装方法的翻译.看看还有很大的参考价值,现在就放上来吧.

Install Beryl on Ubuntu Edgy with nVidia
本文主要包括以下几个部分:
1.使用脚本自动安装
2.安装nvidia的beta驱动
3.添加beryl的源
4.启动beryl
5.使用beryl
6.问题聚焦

1.使用脚本自动安装
以下步骤在使用nvidia GeForce FX 5200 显卡并已经安装了ubuntu基本系统更新到最新的机器上测试通过(对于所有nvidia的beta驱动支持的显卡应该同样适用)
警告:请确定关闭了新立得软件包管理器!

ubuntu
自动化的安装将会:
不需要人工干预地完成beryl的安装
安装稳定的nvidia beta驱动
使用nvidia-xconfig配置/etc/X11/xorg.conf,并且打开ARGBGLXVisuals选项
为安装创建一个桌面图标
添加菜单项到Applications:Accessories
对每个用户都自动启动beryl

创建脚本
打开一个终端,启动:
gedit ./beryl-install-script
复制下面的代码到刚打开的窗口:
#!/bin/bash
if [ `whoami` != "root" ]; then
echo "You must run this script as root.";
else
cp /etc/apt/sources.list /etc/apt/sources.list.backup.beryl-script
cp /etc/X11/xorg.conf /etc/X11/xorg.conf.backup.beryl-script
echo "deb http://nvidia.limitless.lupine.me.uk/ubuntu edgy stable" >> /etc/apt/sources.list
wget http://nvidia.limitless.lupine.me.uk/ubuntu/root@lupine.me.uk.gpg -O- | apt-key add -
aptitude -y update && aptitude -y install linux-restricted-modules-$(uname -r) nvidia-glx
nvidia-xconfig --add-argb-glx-visuals
echo "deb http://ubuntu.beryl-project.org/ edgy main" >> /etc/apt/sources.list
wget http://ubuntu.beryl-project.org/root@lupine.me.uk.gpg -O- | apt-key add -
aptitude -y update && aptitude -y dist-upgrade
aptitude -y install beryl emerald emerald-themes
echo "[Desktop Entry]
Encoding=UTF-8
Name=Beryl Manager
GenericName=3D Window Manager
Comment=Beryl Manager daemon
Icon=/usr/share/icons/hicolor/scalable/apps/beryl-manager.svg
Exec=beryl-manager
Terminal=false
Type=Application
Categories=GTK;GNOME;Application;Utility;
StartupNotify=true
X-Ubuntu-Gettext-Domain=beryl-manager" | sudo tee /etc/xdg/autostart/beryl-manager.desktop
sudo cp /etc/xdg/autostart/beryl-manager.desktop /usr/share/applications/beryl-manager.desktop
cp /etc/xdg/autostart/beryl-manager.desktop ~/Desktop/beryl-manager.desktop
echo -e "\n\nBeryl is now installed.\n\nTo run Beryl on Ubuntu startup, please add beryl-manager to your\nstartup programs (System > Preferences > Sessions, and click on\nthe \"startup programs\" tab). Afterwards, please reboot.\n\nBackups of /etc/apt/sources.list and /etc/X11/xorg.conf were made:\n /etc/apt/sources.list.backup.beryl-script\n /etc/X11/xorg.conf.backup"
fi;
保存文件并且退出gedit.

执行脚本

使用以下命令:
sudo chmod +x ./beryl-install-script
sudo ./beryl-install-script
在脚本运行期间请不要关闭终端,该脚本会安装97××系列的驱动,该系列驱动不支持GeForce 4和其它一些显卡。

安装后工作
最好不要重新运行这个脚本,因此,你应该删除之:
rm ./beryl-install-script

取消beryl开机自动启动

单用户
打开一个终端,运行:
gnome-session-properties
单击”启动”选项卡选择”beryl-manager”然后单击”禁用”
或者从菜单
系统->首选项->会话
单击”启动”选项卡选择”beryl-manager”然后单击”禁用”

所有用户
打开一个终端,运行:
sudo rm /etc/xdg/autostart/beryl-manager.desktop

kubuntu
自动化的安装将会:
不需要人工干预地完成beryl的安装
安装稳定的nvidia beta驱动
使用nvidia-xconfig配置/etc/X11/xorg.conf,并且打开ARGBGLXVisuals选项
在~/.kde/Autostart里面创建一个符号链接用来在kde启动时启动beryl
创建脚本
打开一个终端,启动:
kate ./beryl-install-script
复制下面的代码到刚打开的窗口:
#!/bin/bash
if [ `whoami` != "root" ]; then
echo "You must run this script as root.";
else
cp /etc/apt/sources.list /etc/apt/sources.list.backup.beryl-script
cp /etc/X11/xorg.conf /etc/X11/xorg.conf.backup.beryl-script
echo "deb http://nvidia.limitless.lupine.me.uk/ubuntu edgy stable" >> /etc/apt/sources.list
wget http://nvidia.limitless.lupine.me.uk/ubuntu/root@lupine.me.uk.gpg -O- | apt-key add -
aptitude -y update && aptitude -y install linux-restricted-modules-$(uname -r) nvidia-glx
nvidia-xconfig --add-argb-glx-visuals
echo "deb http://ubuntu.beryl-project.org/ edgy main" >> /etc/apt/sources.list
wget http://ubuntu.beryl-project.org/root@lupine.me.uk.gpg -O- | apt-key add -
aptitude -y update && aptitude -y dist-upgrade
aptitude -y install beryl emerald emerald-themes aquamarine
ln -s /usr/bin/beryl-manager ~/.kde/Autostart/beryl-manager
echo -e "\n\nBeryl is now installed; please reboot.\n\nBackups of /etc/apt/sources.list and /etc/X11/xorg.conf were made:\n /etc/apt/sources.list.backup.beryl-script\n /etc/X11/xorg.conf.backup"
fi;
保存文件并且退出kate.

执行脚本

使用以下命令:
sudo chmod +x ./beryl-install-script
sudo ./beryl-install-script
在脚本运行期间请不要关闭终端.

安装后工作
最好不要重新运行这个脚本,因此,你应该删除之:
rm ./beryl-install-script
现在重新启动,beryl将会在你登录后启动,一个红宝石图标将会出现在你的系统托盘。

2.安装nvidia的beta驱动

首先,备份你的/etc/apt/sources.list
sudo cp /etc/apt/sources.list /etc/apt/_sources.list.backup
方法一:从源安装(推荐)
首先,从下面选择一个源添加到sources.list.
sudo echo -e "\n## nVidia driver repository\nrepository" >> /etc/apt/sources.list其中## nVidia driver repository\nrepository用下面的这些源中的一个代替:
稳定的驱动(推荐)
deb http://nvidia.limitless.lupine.me.uk/ubuntu edgy stable
不稳定的驱动(firefish:我的理解应该是比较新的驱动)
deb http://nvidia.limitless.lupine.me.uk/ubuntu edgy unstable
现在导入GPG密钥:
sudo echo && wget http://nvidia.limitless.lupine.me.uk/ubuntu/root@lupine.me.uk.gpg -O- | sudo apt-key add -
安装驱动使用如下指令安装nvidia驱动:
sudo apt-get update && sudo apt-get install linux-restricted-modules-$(uname -r) nvidia-glx
如果出现问题
有可能ubuntu的开发者发布了更新版本的linux-restricted-modules,但是你选择的源还没有被更新(举个例子,这将会发生在November 6th, 2006 and January 9th, 2007),要解决这些问题,运行:
apt-cache policy linux-restricted-modules-$(uname -r)
分析终端输出:
linux-restricted-modules-2.6.17-10-generic:
Installed: 2.6.17.5-12~amaranth
Candidate: 2.6.17.6-1
Version table:
2.6.17.6-1 0
500 http://security.ubuntu.com edgy-security/restricted Packages
*** 2.6.17.6-2~amaranth 0
500 http://amaranth.selfip.com edgy/lrm Packages
100 /var/lib/dpkg/status
2.6.17.5-11 0
500 http://gb.archive.ubuntu.com edgy/restricted Packages
如上,在这里包含nvidia beta驱动的最新版本是”2.6.17.5-12~amaranth”(注意“~”),在这种情况下,你应该运行:
sudo apt-get install linux-restricted-modules-$(uname -r)=2.6.17.6-2~amaranth
不幸的是,该软件包会在系统升级时被覆盖掉。不管怎样,出现这种情况只是暂时的。

配置X.Org使用新驱动
方法一:(推荐)
使用如下命令:
sudo nvidia-xconfig --add-argb-glx-visuals
该命令会自动备份xorg.conf到xorg.conf.backup
方法二:(修改xorg.conf)
Composite扩展重启X(Ctrl + Alt + Backspace)或者重启计算机,然后运行如下命令
grep "Composite extension" /var/log/Xorg.0.log如果返回
(WW) NVIDIA(0): 32-bit ARGB GLX visuals require the Composite extension.
运行:
sudo echo -e '\nSection "Extensions"\n Option "Composite" "Enable"\nEndSection' >> /etc/X11/xorg.conf
修改”Device”部分编辑/etc/X11/xorg.conf 将”driver”部分改为nvidia:
Section "Device"
Identifier [...]
Driver "nvidia" # Edit only this line.
BusID [...]
EndSection

在一些笔记本上(dell)安装nvidia驱动会导致白屏,添加下面的一行到”Device”部分来解决问题:
Option "UseDisplayDevice" "DFP-0"
打开ARGBGLXVisuals选项。运行:
sudo sed -i -e '/Section "Screen"/,/EndSection/ { s/EndSection/ Option "AddARGBGLXVisuals" "True"\nEndSection/ }' /etc/X11/xorg.conf
或者手动修改/etc/X11/xorg.conf加上”AddARGBGLXVisuals”这行:
Section "Screen"
[...your configuration...]
Option "AddARGBGLXVisuals" "True" # Edit only this line.
EndSection

重启X11
按Ctrl + Alt + Backspace或者重启计算机。
注意:如果X启动失败,有可能是旧的nvidia内核模块被加载,运行:
sudo rmmod nvidia && sudo modprobe nvidia
现在转到#3.添加beryl的源

方法二:(安装官方驱动)
注意:如果使用官方驱动在升级内核后驱动需重新安装

下载最新的nvidia官方驱动for 32-bit x86:
wget http://us.download.nvidia.com/XFree86/Linux-x86/1.0-9746/NVIDIA-Linux-x86-1.0-9746-pkg1.run

如果你使用的是AMD64/EM64T系统:
wget http://us.download.nvidia.com/XFree86/Linux-x86_64/1.0-9746/NVIDIA-Linux-x86_64-1.0-9746-pkg2.run

安装需要编译内核模块
sudo apt-get install linux-headers-$(uname -r) build-essential
运行nvidia安装包
首先打开一个真实的终端(ctrl+alt+F1)登录,运行:
sudo /etc/init.d/gdm stop
sudo sh ./nvidia_installation_pkg_filename
nvidia_installation_pkg_filename是你刚下载的文件,安装过程基本选择默认即可以,但是不要在安装时选择运行nvidia-xconfig,因为它将不会打开ARGBGLXVisuals选项。
在安装程序结束后,运行:
sudo nvidia-xconfig --add-argb-glx-visuals
重启X:
sudo /etc/init.d/gdm start
正常情况下,将会出现图形界面,如果没有,请看#如果安装官方驱动出现问题。
如果升级了内核
重新安装驱动,参考#方法二:(安装官方驱动),注意:nvidia-xconfig不需要再重新运行。

如果安装官方驱动出现问题
如果X不能启动,有可能是X尝试使用旧的内核模块,在这种情况下,运行:
sudo rmmod nvidia && sudo modprobe nvidia
如果重启ubuntu后X启动不了,禁用”nv”模块:
sudo cp /etc/default/linux-restricted-modules-common /etc/default/_linux-restricted-modules-common.backup && sudo sed -i -e 's/^DISABLED_MODULES="/DISABLED_MODULES="nv /' -e 's/ "/"/' /etc/default/linux-restricted-modules-common
或者,手动编辑linux-restricted-modules-common:
sudo cp /etc/default/linux-restricted-modules-common /etc/default/_linux-restricted-modules-common.backup && sudo nano /etc/default/linux-restricted-modules-common
添加“nv”到如下行:
DISABLED_MODULES="nv, [...]" # Edit only this line.

3.添加beryl的源

添加如下源:
deb http://ubuntu.beryl-project.org/ edgy main
可以使用如下命令添加:
sudo echo -e "\n## Beryl repository\ndeb http://ubuntu.beryl-project.org/ edgy main" >> /etc/apt/sources.list
取得GPG密钥:
sudo echo && wget http://ubuntu.beryl-project.org/root@lupine.me.uk.gpg -O- | sudo apt-key add -

升级系统
sudo apt-get update && sudo apt-get dist-upgrade
安装
sudo apt-get install beryl emerald emerald-themes
kde 的使用者
一个新的窗口装饰器aquamarine可以通过如下命令安装:
sudo apt-get install aquamarine
TripleBuffer(可选的)
该选项可以使beryl更加流畅,但是需要牺牲额外的显存。在一些显卡上,效果将会好一点。
要打开TripleBuffer选项,可以运行
sudo sed -i -e '/Section "Device"/,/EndSection/ { s/EndSection/ Option "TripleBuffer" "True"\nEndSection/ }' /etc/X11/xorg.conf
或者手动编辑/etc/X11/xorg.conf
Section "Device"
[...your configuration...]
Option "TripleBuffer" "True" # Edit only this line.
EndSection

4.启动beryl
使用如下命令启动beryl,在此之前最好重新启动一下X(Ctrl + Alt + Backspace):
beryl-manager
GNOME的使用者
从菜单 系统->首选项->会话 单击”启动”选项卡选择 添加 ,输入”beryl-manager”,单击”OK”.

kde
运行:
ln -s /usr/bin/beryl-manager ~/.kde/Autostart/beryl-manager
重启X全设置生效。

5.使用berylhttp://wiki.beryl-project.org/wiki/Tips/Default_Commands

6.问题聚焦
如果窗口边框和窗口装饰器丢失:
1.尝试在xorg.conf的”Screen”段将默认的色深改为24
2.你有没有禁用桌面图标,如果有,重新启用之…
3.beryl和Xinerama(多显示器?)不兼容

如果出现打开多窗口时有一些窗口变黑,这是nvidia beta驱动的已知bug,解决方法是在beryl的设置里面Rendering Path选择复制。

posted @ 2007-05-23 19:14 zsun666 阅读(1652) | 评论 (0)编辑

2007年5月3日 #

      自从用上Gmail以来就让我彻底放弃了使用其它邮箱的念头,人性化的邮件管理,方便的搜索,令我在使用中感到无比的愉快。但近段时间来频频发生无法登陆的现象,即使登陆后也出现由于连接缓慢导致的只能在普通HTML模式下访问的情况。
      对于出现的种种原因,我无法相信是由于邮件服务器的缘故。看看中国电信之前做的种种卑劣行为。对搜索结果进行过滤,采用信息强推技术对自己的用户强送广告,限制用户使用路由,等等。相信现在Gmail的问题,也应该是电信出于某种利益上的因素在背后搞的鬼。
      对于目前的状况还是有解决办法的,就是用以下的地址来访问Gmail
      https://mail.google.com/mail/
      注意是使用的https协议。

posted @ 2007-05-03 18:07 zsun666 阅读(123) | 评论 (0)编辑

2007年3月24日 #

      在GridView的实际使用中可能会遇到这种情况,某一列使用数据绑定直接从数据库中读取数据,另一列(或几列)中的数据由有数据绑定的那列的数据决定。这就需要动态的生成GridView。
      具体的实现方法如下:
      这里假设GridView的实例为GridView1

      1.将GridView1的某列与某个数据源绑定。
      2.分析GridView1的每一行的数据,并对每行需要动态变化的列进行赋值
         下面代码假设GridView1的第1列为数据绑定的列,第2列需要动态变化

        foreach (GridViewRow grv in GridViewTe.Rows)
        
{
            
string sourcedata = grv.Cells[0].Text;
            
string targetdata = Analyze(targetdata);    //Analyze(targetdata)为自定义函数,通过源数据获取目标数据
            grv.Cell[1].Text = targetdata;            
        }

          如果需要操作列中的控件,比如根据第1列的数据设置第2列中的图片样子,代码如下

        foreach (GridViewRow grv in GridViewTe.Rows)
        
{
            
string sourcedata = grv.Cells[0].Text;
            
string MyImageUrl = GetUrl(targetdata);    //GetUrl(targetdata),通过源数据获取不同的图片路径            
            Image img1 =(Image)grv.Cells[1].Controls[1]
            img1.ImageUrl 
= MyImageUrl;
        }

      3.找一个合适的事件把包含上述代码的函数进去(当然也可以直接把代码放进去)
这个没有一定的限制,我使用的是GridView的PreRender事件,但有一点要注意,绝对不能放在Page_Load中。因为当使用的GridView控件带分页功能时,只要读第2页,上面的代码就没有作用了。

posted @ 2007-03-24 18:55 zsun666 阅读(586) | 评论 (0)编辑

2007年3月18日 #

       ASP.NET AJAX AutoComplete Extender能够在用户输入时提供自动完成的提示,就好像Google的搜索提示那样,的确是不错的功能,前几天一个项目需要在输入工装编号时提示已有的最大编号,而且工装的编号种类不止一种,必须提示所输入的编号种类的最大编号。我立刻就想到了这个。最后顺利完成,但中间却走了个弯路,原来AutoComplete Extender在调用WebService时有些限制:
1.必须在WebService中加上[System.Web.Script.Services.ScriptService]
2.用来返回数据的函数必须返回String[]类型
3.函数的参数名字和数量不能更改,必须为string prefixText, int count
posted @ 2007-03-18 18:48 zsun666 阅读(494) | 评论 (2)编辑

2007年2月19日 #

Beryl新加了两个很眩的功能:下雪和窗口分组
下雪这个功能纯粹是为了炫耀。
窗口分组(Group and Tan Windows)这个功能就很有用了,它可以把不同的窗口归为一组,然后在窗口的正反面显示组内的内容。
这2个功能需要安装beryl-plugins-extra,我是在Ubuntu6.10下安装的,如果遇到类似以下的错误:
E:/var/cache/apt/archives/beryl-plugins-extra_0.1.99.2~0beryl1_all.deb: 正试图覆盖“/usr/lib/beryl/libthumbnail.so”,它属于软件包 beryl-plugins
解决方法为:
sudo dpkg ---force-overwrite /var/cache/apt/archives/beryl-plugins-extra_0.1.99.2~0beryl1_all.deb 

安装完毕后需要设置Beryl,“桌面”-“桌面立方体”-“Mipmap”选项需要选择,否则会屏蔽掉这两个功能。

posted @ 2007-02-19 20:08 zsun666 阅读(152) | 评论 (0)编辑

2007年1月2日 #

元旦放假的最后一天了,怎么也要写点什么。但最近实在是没学到什么值得搬上来的东西。就随便写一些关于用户体验的感想吧。

首先是关于Linux和Windows
前一段时间迷上了Linux,辗转于各个Linux的论坛,受上面气氛的影响,甚至一度认为windows迟早要被Linux替代。但现在认为这是几乎不可能的了,在中国这个盗版横行的现象下,谁会去使用第三方软件及其稀少的Linux呢?个人认为Linux目前最大的问题就是软件的稀少以及各种发行版与硬件的兼容性问题。目前有些Linux的使用者和传播者抱着一种怕麻烦就不要使用Linux的态度,其实这根本不利于Linux的发展。如果是作为桌面系统,用户是为了办公及娱乐来使用系统的,而不是为了学习系统而使用系统的,所以为达目的以外的事越简单越好。看来除了商务应用,在中国Linux是很难进入普通人的电脑了。

然后是关于操作系统的界面
前一段时间SUN公司推出了Looking Glass,能够实现用户桌面的三维化,回顾一年来,各大公司纷纷推出自己的三维桌面产品。微软的Vista、Linux上的xgl,Beryl、以及BumpTop。其中BumpTop甚至加上了物理碰撞的效果。据说可能会在以后集成到windows中,对此我是很期待。
尽管很多人对三维桌面持反对态度,认为这都是些华而不实的东西。但不可否认的是这确实是以后的发展趋势。普通用户使用软件时根本不会去关心你内部是如何如何,他唯一关心的是界面是如何的人性化,用的舒服。关注目前网上提到最多的总是用户体验方面的内容,对于软件内部的效率问题明显少很多。由此可见一斑。所以自己写的东西,别人用了以后,看到一个优良的用户体验就会很高兴。而自己所真正花精力的东西根本不会被普通人放在眼里。这也是目前ajax技术大行其道的原因。虽然很无奈,但这也是没办法的事。
posted @ 2007-01-02 16:36 zsun666 阅读(36) | 评论 (0)编辑

2006年12月12日 #

      接触ASP.NET也将近两年的时间了,但从来没想过去了解它的执行流程到底是怎样的,今天偶然发现了这张图,赶快把它传上来.
   
  
posted @ 2006-12-12 20:25 zsun666 阅读(140) | 评论 (0)编辑

2006年9月7日 #

在.net中获取网页html源代码最简单的方法莫过于使用WebBrowser.DocumentText,但是在碰到含中文的网页时,经常会出现乱码,下面给出两种解决方法:
1)
WebBrowser myweb = new WebBrowser();
myweb.Navigate(
"http://zsun666.cnblogs.com/");
StreamReader strmrd 
= new StreamReader(myweb.DocumentStream,Encoding.GetEncoding("GB2312"));
string strhtml = strmrd.ReadToEnd();

2)
WebRequest wrq = WebRequest.Create("http://zsun666.cnblogs.com/");
//如果服务器要验证用户名,密码
//NetworkCredential mycred = new NetworkCredential(struser, strpassword);
//wrq.Credentials = mycred;
WebResponse wrs = wrq.GetResponse();
StreamReader strmrd 
= new System.IO.StreamReader(wrs.GetResponseStream(),Encoding.GetEncoding("GB2312"));
string strhtml = strmrd.ReadToEnd();
posted @ 2006-09-07 10:34 zsun666 阅读(107) | 评论 (0)编辑

2006年8月27日 #

近期迷上了Linux。正在学习中,先记下Ubuntu的系统目录结构,以后肯定会用到。
    /   根目录
    │
    ├boot/      启动文件。所有与系统启动有关的文件都保存在这里
    │    └grub/   Grub引导器相关的文件
    │
    ├dev/       设备文件
    ├proc/      内核与进程镜像
    │
    ├mnt/      临时挂载
    ├media/   挂载媒体设备
    │
    ├root/      root用户的$HOME目录
    ├home/         
    │    ├user/   普通用户的$HOME目录
    │    └/
    │
    ├bin/      系统程序
    ├sbin/      管理员系统程序
    ├lib/      系统程序库文件
    ├etc/      系统程序和大部分应用程序的全局配置文件
    │   ├init.d/   SystemV风格的启动脚本
    │   ├rcX.d/   启动脚本的链接,定义运行级别
    │   ├network/   网络配置文件
    │   ├X11/      图形界面配置文件
    │
    ├usr/      
    │   ├bin/      应用程序
    │   ├sbin/   管理员应用程序
    │   ├lib/      应用程序库文件
    │   ├share/   应用程序资源文件
    │   ├src/      应用程序源代码
    │   ├local/      
    │   │     ├soft/      用户程序      
    │   │     └/      通常使用单独文件夹
    │   ├X11R6/   图形界面系统
    │
    ├var/         动态数据
    │
    ├temp/         临时文件
    ├lost+found/   磁盘修复文件


posted @ 2006-08-27 09:47 zsun666 阅读(88) | 评论 (0)编辑

2006年7月24日 #

一、实用类:

1、System.MarshalByRefObject :
    
系统中远程调用的对象必须是从MarshalByRefObject对象中派生出来的;

2System.Runtime.Remoting.Channels.Tcp.TcpServerChannel :
     服务器端的Tcp信道;

3System.Runtime.Remoting.Channels.Http.HttpServerChannel :
     服务器端的Http信道;

4System.Runtime.Remoting.Channels.ChannelServices :
     注册信道,使之可用于远程对象;

5System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnowServiceType :
     指定远程对象中类的类的类型,客户机使用的URI和模式;
6System.Runtime.Remoting.Channels.Tcp.TcpClientChannel :
     客户端的Tcp信道;

7System.Runtime.Remoting.Channels.Http.HttpClientChannel :
     客户端的Http信道。


二、简单的示例

1、创建远程对象,在这里创建一个dll程序集,这个dll在服务器和客户机代码中都会用到。
     创建名为RemoteHello.dll的程序集

using System;
using System.Collections.Generic;
using System.Text;

namespace RemoteHello
{
    
public class Hello:System.MarshalByRefObject
    
{
        
public Hello()
        
{
            Console.WriteLine(
"Constructor called");
        }


        
~Hello()
        
{
            Console.WriteLine(
"Destructor called");
        }


        
public string HelloWorld(string name)
        
{
            Console.WriteLine(
"Hello World!");
            
return "Hi," + name;
        }

    
    }

}

 

2、创建服务器。需要引用System.Runtime.Remoting程序集和之前创建的RemoteHello.dll程序集
     在此创建名为HelloServer的Console Application

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteHello;
namespace HelloService
{
    
class HelloServer
    
{
        
static void Main(string[] args)
        
{
            TcpServerChannel channel 
= new TcpServerChannel(6666);
            
//ChannelServices.RegisterChannel(channel);
            ChannelServices.RegisterChannel(channel,false);
            RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Hello), "HelloWorld", WellKnownObjectMode.SingleCall);
            System.Console.WriteLine(
"Press Any Key to Exit ! ");
            System.Console.ReadLine();
        }

    }

}

上面代码中可以用

ChannelServices.RegisterChannel(channel);
来替换
ChannelServices.RegisterChannel(channel,false);
但是在.NET Framework 2.0中编译时会提示
'System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(System.Runtime.Remoting.Channels.IChannel)' is obsolete: 'Use System.Runtime.Remoting.ChannelServices.RegisterChannel(IChannel chnl, bool ensureSecurity) instead.'

原因是

System.Runtime.Remoting.ChannelServices.RegisterChannel(IChannel chnl, bool ensureSecurity)

.NET Framework 2.0新增的函数

其中参数:ensureSecurity

如果启用了安全,则为 true;否则为 false。将该值设置为 false 将不会使在 TCP IPC 信道上所做的安全设置无效。

此外MSDN中注释:对于 TcpServerChannel,将 esureSecurity 设置为 true 将在 Win98 上引发异常(因为 Wi9x 上不支持安全 tcp 信道);对于 Http 服务器信道,这样会在所有平台上引发异常(如果想要安全的 http 信道,用户需要在 IIS 中承载服务)。

3、创建客户机。需要引用System.Runtime.Remoting程序集和之前创建的RemoteHello.dll程序集
     在此创建名为HelloClient的Console Application

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteHello;

namespace HelloClient
{
    
class HelloClient
    
{
        
static void Main(string[] args)
        
{
            
            ChannelServices.RegisterChannel(
new TcpClientChannel(), false);
            Hello obj 
= (Hello)Activator.GetObject(typeof(Hello), "tcp://zjx:6666/HelloWorld");
            
if (obj == null)
            
{
                Console.WriteLine(
"False to Link Server.");
                
return;
            }

            
for (int i = 0; i < 6; i++)
            
{
                Console.WriteLine(obj.HelloWorld(
"MadRam.neo"));
            }

        }

    }

}

其中

tcp://zjx:6666/HelloWorld

中的zjx为服务器主机名。
上面的代码基本上照搬了Professional C# 2nd Edition中的代码,其中客户机代码的原意应该是如果没有连接到服务器则显示 False to Link Server.但事实上当服务器没开时 obj!=null 所以程序会执行 for 循环,在执行

obj.HelloWorld("MadRam.neo")

时就会出错,提示“System.Net.Sockets.SocketException : 由于目标机器积极拒绝,无法连接。”
这一点暂时没找到原因。
如果服务器打开的话,这个程序可以顺利的运行。

posted @ 2006-07-24 21:06 zsun666 阅读(784) | 评论 (2)编辑