程序是用C#写的
远程对象还在内存中,所以当重新注册一个客户端端口的时候会报TCP端口已经存在。
处理的方法如下
public static TestLoader loader
//注:TestLoader 是远程组件的名字。
if(loader==null)
{
//当还没有对象时在客户端注册端口,创建远程对象
ChannelServices.RegisterChannel(new TcpChannel());
//创建远程对象
loader=(TestLoader)Activator.GetObject(typeof(yuancheng_zujian.TestLoader),"tcp://"+fuwuqi+":"+port+"/TestLoader");
}
else
{
Label8.Text="已经有对象了!";
}
//执行方法 Writer是在远程组件中定义的方法
loader.Writer("xx");
如果没有loader==null
那么就会在第二次运行客户端的时候程序有创建一个端口,同时创建一个对象。
我的程序是用浏览器执行的。
没有loader==null的判断,用来屏蔽掉已经创立的对象。浏览器就会报
TCP端口已经占用。
大家可以试试
这可是我做了一天终于得到的解决方法
原程序如下:
.net服务组件 创立强名称,注册组件,并且把组件变成服务器组件启动服务器
using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.EnterpriseServices;
[assembly:ApplicationName("diange")]
namespace fuwu_zhujian
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
///
[ObjectPooling(Enabled=true,MinPoolSize=0,MaxPoolSize=5,CreationTimeout=8000)]
[JustInTimeActivation(true)]
[ConstructionEnabled(Default="diange_jilu.txt")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class logFile:ServicedComponent
{
private StreamWriter w;
protected override void Construct(string constructString)
{
w=new StreamWriter(constructString,true,Encoding.Default);
w.BaseStream.Seek(0,SeekOrigin.End);
}
[AutoComplete]
public void Write(string msg)
{
w.WriteLine("点歌曲的信息"+msg);
}
protected override void Activate()
{
w.WriteLine("开始记录");
}
protected override void Deactivate()
{
w.WriteLine("ok");
}
protected override bool CanBePooled()
{
w.WriteLine("Com+Pooling the objects");
w.Flush();
return true;
}
}
}
远程组件
using System;
using System.Reflection;
using System.Windows.Forms;
using fuwu_zhujian;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
namespace yuancheng_zujian
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class TestLoader:System.MarshalByRefObject
{
public TestLoader()
{
System.Console.WriteLine("创建远程组件对象成功");
}
public void Write(string str)
{
try
{
using(logFile log=new logFile())
{
log.Write("点歌信息: "+str);
}
}
catch(Exception ex)
{
MessageBox.Show("出线异常"+ex.ToString());
}
}
public string findMp3Path(string geshou,string gequming)
{
//找出MP3所在位置的绝对路径。或着相对路径
string conStr,comStr,mp3path,geshoustr,gequmingstr;
mp3path="";
conStr="uid=sa;pwd=;server=.;database=fengbin";
comStr="";
geshoustr=geshou;
gequmingstr=gequming;
if(geshoustr.Length==0 || geshoustr.Equals(""))
{
comStr=@"select * from mp3path where gequming='"+gequmingstr+@"'";
}
else if(gequmingstr.Length==0 || gequmingstr.Equals(""))
{
comStr=@"select * from mp3path where geshou='"+geshoustr+"'";
}
else if(gequmingstr.Length!=0 && geshou.Length!=0)
{
comStr=@"select * from mp3path where geshou='"+geshou+@"' and gequming='"+gequming+@"'";
}
else
{
return "未传值进来!";
}
SqlConnection con=new SqlConnection(conStr);
con.Open();
SqlCommand com=new SqlCommand(comStr,con);
SqlDataReader red=com.ExecuteReader();
if(red.Read())
{
mp3path=red.GetValue(2).ToString();
}
return mp3path;
}
public void dispaly(string geshou,string gequming,string use,string touse)
{
//避免执行的时候,不会在这里卡住,当然 老板可以在这里看到情况。
// MessageBox.Show("歌手:"+geshou);
// MessageBox.Show("歌曲名"+gequming);
// MessageBox.Show(use+"为"+touse+"点了一首歌曲");
}
}
}
服务器端程序
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.IO;
using yuancheng_zujian;
namespace diange_fuwuqi
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
///
public TcpServerChannel channel;
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.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(168, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(144, 23);
this.label1.TabIndex = 0;
this.label1.Text = "点歌台服务器端";
//
// button1
//
this.button1.Location = new System.Drawing.Point(160, 176);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "启动服务器";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(240, 56);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 2;
this.textBox1.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(112, 64);
this.label2.Name = "label2";
this.label2.TabIndex = 5;
this.label2.Text = "服务器端口";
//
// button2
//
this.button2.Location = new System.Drawing.Point(288, 176);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 24);
this.button2.TabIndex = 6;
this.button2.Text = "关闭服务器";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(184, 112);
this.textBox2.Name = "textBox2";
this.textBox2.ReadOnly = true;
this.textBox2.Size = new System.Drawing.Size(136, 21);
this.textBox2.TabIndex = 7;
this.textBox2.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(496, 278);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button2);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
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)
{
//qidong
int port ;
port=Convert.ToInt16(textBox1.Text);
channel =new TcpServerChannel(port);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(TestLoader),"TestLoader",WellKnownObjectMode.SingleCall);
MessageBox.Show("服务器启动成功");
textBox2.Text="服务器已经启动";
}
private void button2_Click(object sender, System.EventArgs e)
{
ChannelServices.UnregisterChannel(channel);
MessageBox.Show("服务器关闭");
textBox2.Text="服务器已经关闭";
}
}
}
客户端程序
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Windows.Forms;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using yuancheng_zujian;
namespace diange_client
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.TextBox TextBox5;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.TextBox TextBox6;
protected System.Web.UI.WebControls.Label Label8;
protected System.Web.UI.WebControls.Label Label9;
protected System.Web.UI.WebControls.Label Label10;
protected System.Web.UI.WebControls.Label Label11;
protected System.Web.UI.WebControls.Button Button1;
public static TestLoader loader;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
string geshou,gequming;
string fuwuqi;
int port;
string mp3path,use,touse;
int temp_port=12345;
int jiaoyan=0;
fuwuqi=TextBox1.Text.ToString();
port=Convert.ToInt16(TextBox2.Text);
geshou=TextBox3.Text.ToString();
gequming=TextBox4.Text.ToString();
//点歌者和给谁点歌曲
use=TextBox5.Text.ToString();
touse=TextBox6.Text.ToString();
if(loader==null)
{
//创建对象
ChannelServices.RegisterChannel(new TcpChannel());
loader=(TestLoader)Activator.GetObject(typeof(yuancheng_zujian.TestLoader),"tcp://"+fuwuqi+":"+port+"/TestLoader");
temp_port=port;
}
else
{
Label8.Text="已经有对象了!";
}
string txt9;
loader.Write("歌手名"+geshou+"歌曲名"+gequming);
mp3path=loader.findMp3Path(geshou,gequming);
txt9="歌曲路径"+mp3path;
Label9.Text=txt9;
loader.dispaly(geshou,gequming,use,touse);
//在这里传递参数到播放的网页
string url;
url="bofang.aspx?name=" + TextBox4.Text + "&mp3path=" + mp3path;
Response.Redirect(url);
}
}
}
大家可以去掉if(loader==null)的判断语句。当你第二次运行的时候就会报TCP端口被占用的错误。