服务端是怎么向类库传值
我用服务端向类库传值(调用类库的方法),客户端取出来是空的
我把值直接赋在类库的变量中,客户端可以正常取到
服务端是怎么向类库传值
我用服务端向类库传值(调用类库的方法),客户端取出来是空的
我把值直接赋在类库的变量中,客户端可以正常取到
你说的那种可以讲明白点吗
[此贴子已经被作者于2007-10-25 16:29:35编辑过]
以前学习时候。自己学别人写的DEMO
//RunServer:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
namespace runserver
{
class Program
{
static void Main(string[] args)
{
RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.ToString(),true);
Console.WriteLine("正在运行中!");
Console.ReadLine();
}
}
}
//server.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Runtime.Remoting.Lifetime;
namespace server
{
/// <summary>
/// 服务器端,接口inf
/// </summary>
public class server : MarshalByRefObject,Inf
{
private static evensinks evensinks;
public static evensinks EvenSinks
{
get
{
if (evensinks == null)
evensinks = new evensinks();
return evensinks;
}
}
/// <summary>
/// 数据库连接
/// </summary>
/// <returns></returns>
private SqlConnection db()
{
return new SqlConnection("server=.;uid=sa;pwd=******;database=fcoa;");
}
/// <summary>
/// 数据库写入
/// </summary>
/// <param name="a"></param>
public void send(string a)
{
show();
SqlConnection con = db();
SqlCommand cmd = new SqlCommand("insert into 系统公告 (公告类别,公告名称,公告内容,发送人)values('a','"+a.Trim()+"','c','d')", con);
con.Open();
Console.WriteLine("连接数据库成功");
int i =cmd.ExecuteNonQuery();
Console.WriteLine("数据库操作成功");
con.Close();
Console.WriteLine("关闭数据库连接");
eventarges e = new eventarges();
e.Message = i.ToString();
if (scanes())
{
Console.WriteLine("应返回值为:" + i.ToString());
show();
if (eventhandle != null)
{
try
{
eventhandle(null, e);
Console.WriteLine("消息发送成功");
}
catch (Exception ex)
{
Console.WriteLine("触发消息发送异常:" + ex.Message);
}
}
else
{
Console.WriteLine("消息发送失败");
}
}
}
void show()
{
Console.WriteLine("eventsinks个数为:" + EvenSinks.Count.ToString());
Console.WriteLine("委托列表的个数:" + eventhandle.GetInvocationList().Length.ToString());
}
private bool scanes()
{
retry:
int i = 0;
bool b = false;
foreach (evensink es in EvenSinks)
{
try
{
Console.WriteLine("状态"+es.GetLifetimeService());
}
catch (Exception ex)
{
Console.WriteLine("读取连接异常:" + ex.Message);
b = true;
break;
}
i++;
}
if (b)
{
eventhandle -= new handler(EvenSinks[i].showmessage);
Console.WriteLine("删除订阅成功");
EvenSinks.RemoveAt(i);
Console.WriteLine("删除连接成功");
goto retry;
}
return true;
}
public void add(evensink es)
{
eventhandle += new handler(es.showmessage);
Console.WriteLine("添加订阅成功");
EvenSinks.Add(es);
Console.WriteLine("添加连接成功");
}
public void cle(evensink es)
{
eventhandle -= new handler(es.showmessage);
Console.WriteLine("删除订阅成功");
EvenSinks.Remove(es);
Console.WriteLine("删除连接成功");
}
/// <summary>
/// 事件定义
/// </summary>
private event handler eventhandle;
}
}
//interface.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Messaging;
namespace server
{
/// <summary>
/// 服务器端接口
/// </summary>
public interface Inf
{
[OneWay]
/// <summary>
/// 数据库写入接口
/// </summary>
/// <param name="a">公告名称</param>
void send(string a);
/// <summary>
/// 服务器事件接口
/// </summary>
void add(evensink es);
void cle(evensink es);
}
/// <summary>
/// 参数
/// </summary>
///
[Serializable]
public class eventarges :EventArgs
{
private string message;
public string Message
{
get { return message; }
set { message = value; }
}
}
/// <summary>
/// 委托定义
/// </summary>
/// <param name="sender"></param>
/// <param name="e">参数</param>
public delegate void handler(object sender,eventarges e);
public class evensink : MarshalByRefObject
{
public event handler evenhandler1;
public void showmessage(object sender, eventarges e)
{
if (evenhandler1 != null)
{
Console.WriteLine("dsafas");
evenhandler1(sender, e);
Console.WriteLine("消息接受成功");
}
else
{
Console.WriteLine("消息接受失败");
}
}
}
public class evensinks :List<evensink>
{}
}
//client.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using server;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
using System.Runtime.Serialization.Formatters;
namespace client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private server.Inf s;
private evensink evens;
private void Form1_Load(object sender, EventArgs e)
{
evens = new evensink();
evens.evenhandler1 += new handler(refform);
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 0;
TcpChannel chn = new TcpChannel(props, null, provider);
ChannelServices.RegisterChannel(chn,true);
s = (server.Inf)Activator.GetObject(typeof(server.Inf), "tcp://localhost:6791/server.soap");
try
{
s.add(evens);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
s.send("ccc");
}
private void refform(object sender, eventarges e)
{
if (this.InvokeRequired)
{
server.handler hand = new server.handler(RefreshHistory);
Invoke(hand, sender, e);
}
}
private void RefreshHistory(object sender, eventarges e)
{
this.textBox1.Text += e.Message.ToString()+Environment.NewLine;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
s.cle(evens);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
[此贴子已经被作者于2007-10-25 16:41:33编辑过]
//这是dll
using System;
public class HelloServer : MarshalByRefObject
{
public HelloServer()
{
}
private string scp1 = "";
private string scp2 = "";
private string scp3 = "";
private string scp4 = "";
private string scp5 = "";
/// <summary>
/// 服务端调用
/// </summary>
/// <param name="flag"></param>
public void scp11(string flag)
{
scp1 = flag;
}
public void scp22(string flag)
{
scp2 = flag;
}
public void scp33(string flag)
{
scp3 = flag;
}
public void scp44(string flag)
{
scp4 = flag;
}
public void scp55(string flag)
{
scp5 = flag;
}
/// <summary>
/// 客户端调用
/// </summary>
/// <returns></returns>
public string ccp1()
{
string cp1 = "";
cp1 = scp1;
return cp1;
}
public string ccp2()
{
string cp2 = "";
cp2 = scp2;
return cp2;
}
public string ccp3()
{
string cp3 = "";
cp3 = scp3;
return cp3;
}
public string ccp4()
{
string cp4 = "";
cp4 = scp4;
return cp4;
}
public string ccp5()
{
string cp5 = "";
cp5 = scp5;
return cp5;
}
}
//服务端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ChannelServices.RegisterChannel(new TcpChannel(8082));
}
private void button1_Click(object sender, EventArgs e)
{
RemotingConfiguration.RegisterActivatedServiceType(typeof(HelloServer));
HelloServer server = new HelloServer();
server.scp11(this.textBox1.Text);
}
}
}
//客户端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
ChannelServices.RegisterChannel(new TcpChannel());
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
// 创建 激活 客户端类型 实体.
ActivatedClientTypeEntry myActivatedClientTypeEntry =
new ActivatedClientTypeEntry(typeof(HelloServer),
"tcp://localhost:8082");
// 通过注册客户端类型激活服务端
RemotingConfiguration.RegisterActivatedClientType(
myActivatedClientTypeEntry);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
// 实例化客户端对象
HelloServer server = new HelloServer();
// 在本地检查与服务端的连接.
if (server == null)
{
this.label1.Text = ("连接失败!");
}
else
{
this.label1.Text = server.ccp1();
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
}
}
[此贴子已经被作者于2007-10-25 17:03:30编辑过]