客户端代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;namespace Server
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label2;
private TcpChannel chan;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(144, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Client1 GetSN";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(184, 112);
this.label1.Name = "label1";
this.label1.TabIndex = 1;
this.label1.Text = "SN1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 2;
this.textBox1.Text = "Server-dev2";
//
// button2
//
this.button2.Location = new System.Drawing.Point(16, 160);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(144, 23);
this.button2.TabIndex = 3;
this.button2.Text = "Client2 GetSN";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(184, 160);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "SN2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
if(chan==null)
{
chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
} ServerClass.StoreClass store = null;
object[] url = {new UrlAttribute("tcp://" + textBox1.Text + ":9010/MyServer")};
// Note that the second parameter (null) specifies that no arguments
// are being passed.
object[] args = {"SN1111111"};
store = (ServerClass.StoreClass)Activator.CreateInstance(
typeof(ServerClass.StoreClass),
args,
url
); label1.Text = store.SN;
MessageBox.Show(store.SN); //ChannelServices.UnregisterChannel(chan);
} private void button2_Click(object sender, System.EventArgs e)
{
if(chan==null)
{
chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
} ServerClass.StoreClass store = null;
object[] url = {new UrlAttribute("tcp://" + textBox1.Text + ":9010/MyServer")};
// Note that the second parameter (null) specifies that no arguments
// are being passed.
object[] args = {"SN222222222"};
store = (ServerClass.StoreClass)Activator.CreateInstance(
typeof(ServerClass.StoreClass),
args,
url
); label2.Text = store.SN;
MessageBox.Show(store.SN);
}
}
}
服务类代码如下:
using System;
using System.Runtime.Remoting;namespace ServerClass
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class StoreClass:MarshalByRefObject
{
private string sn;
public StoreClass(string strSN)
{
sn =strSN;
} public string SN
{
get
{
return sn;
}
}
~StoreClass()
{
Console.WriteLine("Store Object is Released by GC");
}
}
}该程序当服务端安装在一台专用服务器(无防火墙,联想的)时客户端不能引用到远程方法,总是说连接超时。而程序在其他机器运行正常,哪位高手可以帮小弟解决问题啊。(在线等待,没解答明天继续)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;namespace Server
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label2;
private TcpChannel chan;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(144, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Client1 GetSN";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(184, 112);
this.label1.Name = "label1";
this.label1.TabIndex = 1;
this.label1.Text = "SN1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 2;
this.textBox1.Text = "Server-dev2";
//
// button2
//
this.button2.Location = new System.Drawing.Point(16, 160);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(144, 23);
this.button2.TabIndex = 3;
this.button2.Text = "Client2 GetSN";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(184, 160);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "SN2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
if(chan==null)
{
chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
} ServerClass.StoreClass store = null;
object[] url = {new UrlAttribute("tcp://" + textBox1.Text + ":9010/MyServer")};
// Note that the second parameter (null) specifies that no arguments
// are being passed.
object[] args = {"SN1111111"};
store = (ServerClass.StoreClass)Activator.CreateInstance(
typeof(ServerClass.StoreClass),
args,
url
); label1.Text = store.SN;
MessageBox.Show(store.SN); //ChannelServices.UnregisterChannel(chan);
} private void button2_Click(object sender, System.EventArgs e)
{
if(chan==null)
{
chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
} ServerClass.StoreClass store = null;
object[] url = {new UrlAttribute("tcp://" + textBox1.Text + ":9010/MyServer")};
// Note that the second parameter (null) specifies that no arguments
// are being passed.
object[] args = {"SN222222222"};
store = (ServerClass.StoreClass)Activator.CreateInstance(
typeof(ServerClass.StoreClass),
args,
url
); label2.Text = store.SN;
MessageBox.Show(store.SN);
}
}
}
服务类代码如下:
using System;
using System.Runtime.Remoting;namespace ServerClass
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class StoreClass:MarshalByRefObject
{
private string sn;
public StoreClass(string strSN)
{
sn =strSN;
} public string SN
{
get
{
return sn;
}
}
~StoreClass()
{
Console.WriteLine("Store Object is Released by GC");
}
}
}该程序当服务端安装在一台专用服务器(无防火墙,联想的)时客户端不能引用到远程方法,总是说连接超时。而程序在其他机器运行正常,哪位高手可以帮小弟解决问题啊。(在线等待,没解答明天继续)
CPU:P4
OS:windows 2003 Server
.net FrameWork : 1.0,1.1都试过
双网卡
无防火墙
局域网机器也无法连接,但是服务器连接自己时没问题程序安装在其他机器运行正常。
(store );
prop["timeout"] = 1800000; //half an hourhttp://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF8&selm=0a7b01c17c19%240fec8530%24a4e62ecf%40tkmsftngxa06
多谢思归老兄的回答,问题已经解决。是因为使用ADSL的原因(其实可以看成普通的路由器,而程序中的绑定不完全对),程序中要进行IP绑定或者计算机名绑定。
不过又出现了问题:实际程序中数据库操作使用到了事务,有时会出现事务挂起的错误问题(但在很多机器不会又这样的问题),不知又是什么什么原因?期盼思归老兄等高手能帮忙解答。先谢了。
>>>但在很多机器不会又这样的问题again, check the difference between the computers
******未处理的“System.Net.Sockets.SocketException”类型的异常出现在 system.dll 中。******
******其他信息: 请求的名称有效,但是没有找到请求的类型的数据。******
******其他信息: 请求的名称有效,但是没有找到请求的类型的数据。******检查你的网络状况。
如果机器上网卡没有获得任何地址,处于禁用状态,通常会报告这样的错误。