标题:remoting(传值)问题
只看楼主
rindybo
Rank: 2
等 级:论坛游民
帖 子:126
专家分:51
注 册:2007-3-8
结帖率:100%
 问题点数:0 回复次数:14 
remoting(传值)问题

服务端是怎么向类库传值
我用服务端向类库传值(调用类库的方法),客户端取出来是空的

我把值直接赋在类库的变量中,客户端可以正常取到

搜索更多相关主题的帖子: remoting 传值 
2007-10-25 15:57
davia
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-7-24
得分:0 
是服务器回调么?好像是。通过委托,激活事件,通过传参数(EventArgs)。说的不对的地方请多关照。

我是新手,说的不对的地方,请多关照!望请指点!
2007-10-25 16:16
rindybo
Rank: 2
等 级:论坛游民
帖 子:126
专家分:51
注 册:2007-3-8
得分:0 


你说的那种可以讲明白点吗

[此贴子已经被作者于2007-10-25 16:29:35编辑过]


[ffg,#6CCFF7,#FFFFFF] ←★→┠最┨┠愛┨┠伱┨┠了┨←★→ [/ft]
2007-10-25 16:25
davia
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-7-24
得分:0 

以前学习时候。自己学别人写的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编辑过]


我是新手,说的不对的地方,请多关照!望请指点!
2007-10-25 16:37
davia
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-7-24
得分:0 
就是通过委托。sorry。刚才把密码都发上了。。

我是新手,说的不对的地方,请多关照!望请指点!
2007-10-25 16:39
rindybo
Rank: 2
等 级:论坛游民
帖 子:126
专家分:51
注 册:2007-3-8
得分:0 
是这样啊
你看看有什么方法能让我这样写可以行的同的
我把代码贴上来
本来用socket做好了
因为我的 remoting不懂,想联系下

[ffg,#6CCFF7,#FFFFFF] ←★→┠最┨┠愛┨┠伱┨┠了┨←★→ [/ft]
2007-10-25 16:47
rindybo
Rank: 2
等 级:论坛游民
帖 子:126
专家分:51
注 册:2007-3-8
得分:0 

//这是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);
}

}
}
}


[ffg,#6CCFF7,#FFFFFF] ←★→┠最┨┠愛┨┠伱┨┠了┨←★→ [/ft]
2007-10-25 16:50
rindybo
Rank: 2
等 级:论坛游民
帖 子:126
专家分:51
注 册:2007-3-8
得分:0 
我自己想出来的传值方法
但是服务端调用不成功

[ffg,#6CCFF7,#FFFFFF] ←★→┠最┨┠愛┨┠伱┨┠了┨←★→ [/ft]
2007-10-25 16:52
davia
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-7-24
得分:0 
客户端激活 每个客户端new的实例不能共享。换 WellKnown试试

[此贴子已经被作者于2007-10-25 17:03:30编辑过]



我是新手,说的不对的地方,请多关照!望请指点!
2007-10-25 17:02
rindybo
Rank: 2
等 级:论坛游民
帖 子:126
专家分:51
注 册:2007-3-8
得分:0 
只是传个值而已
还有没有别的方法啊

[ffg,#6CCFF7,#FFFFFF] ←★→┠最┨┠愛┨┠伱┨┠了┨←★→ [/ft]
2007-10-25 17:14



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-180720-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.352826 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved