使用Remoting做的消息板,第二次登录无法发消息!!!
我做了一个聊天程序, 分为以下三个项目:
Project 1: MsgBoardShard (类库)
Project 2: MsgBrdServer (控制台应用程序,服务器端)
Project 3: MsgBoardClient (控制台应用程序,消息客户端)
第一次启动服务器端和客户端时,客户端输入消息会在服务器上显示,一切正常,可是当我仅关闭客户端,然后再开启客户端的时候,发的第一条消息会在服务器上显示,服务器显示完这条消息之后,就报错了。这是为什么?
两天了,我都要疯了,有达人能够帮我吗?能够解决我这个问题的人,可以把邮箱地址回复,我把源代码发给你,30K。
如果在您看来是一两句话的话,也麻烦指点一下,搞定后高分相送!!!
=====================================================================
project 1: "MsgMsgBoard " 的内容:
public delegate void EventDelegate(string info);
[Serializable]
public class MsgBoard : MarshalByRefObject
{
public event EventDelegate OnInfoAdded;
public void AddMessage(string info)
{
Console.WriteLine( "SERVER:{0} " , info);
if (OnInfoAdded != null)
OnInfoAdded(info); // error here: cannot connect to remote
server
}
}
另外一个类:
[Serializable]
public class EventClass : MarshalByRefObject
{
private EventClass()
{
}
public static EventClass Instance = EventClass.CreateInstance();
public static EventClass CreateInstance()
{
if (Instance == null)
{
Instance = new EventClass();
}
return Instance;
}
public event EventDelegate OnInfoAdded;
public void msgbd_OnInfoAdded(string info)
{
//Console.WriteLine( "info from server event:{0} ", info);
if (OnInfoAdded != null)
OnInfoAdded(info);
}
}
=====================================================================
project 2 "MsgBrdServer ", 服务器端
static void Main(string[] args)
{
RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,
false);
Console.WriteLine( "Server... ");
Console.ReadLine();
}
----------------------
下面是配置文件的内容
<?xml version= "1.0 " encoding= "utf-8 " ?>
<configuration>
<system.runtime.remoting>
<customErrors mode= "off "/>
<application>
<service >
<wellknown mode= "Singleton " type= "MsgBoardShard.MsgBoard,
MsgBoardShard " objectUri= "MsgBoard " />
</service>
<channels>
<channel ref= "http " port= "8080 ">
<serverProviders>
<formatter ref= "soap " typeFilterLevel= "Full "/>
<formatter ref= "binary " typeFilterLevel= "Full "/>
</serverProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
=====================================================================
Project 3, 配置文件:
<?xml version= "1.0 "?>
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown type= "MsgBoardShard.MsgBoard, MsgBoardShard "
url= "http://localhost:8080/MsgBoard "/>
</client>
<channels>
<channel ref= "http " port= "0 ">
<serverProviders>
<formatter ref= "soap " typeFilterLevel= "Full "/>
<formatter ref= "binary " typeFilterLevel= "Full "/>
</serverProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
-----------------------
program.cs:
static void Main(string[] args)
{
RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,
false);
EventClass cl = EventClass.Instance;
MsgBoard md = new MsgBoard();
md.OnInfoAdded += new EventDelegate(cl.msgbd_OnInfoAdded);
Console.Title = "MsgBdClient ";
string Ss = " ";
while (Ss!= "q ")
{
Ss = Console.ReadLine();
md.AddMessage(Ss);
}
Console.ReadLine();
}
[解决办法]
不懂 ,帮你顶一下吧!
[解决办法]
mark 学习
[解决办法]
http://www.itpub.net/570606.html
上面的文章也许能解决你的问题。我的理解是:
1 客户端退出之前主动取消订阅服务器消息
2 或者,在服务器端使用OneWayAttribute([OneWay])方法
3 或者,在服务器端遍历委托链,自己捕捉异常
[解决办法]
快沉了,顶上去
[解决办法]
但是还有一个问题,就是程序不正常退出之后,服务器端对于这个事件该怎么处理呢??
遍里服务器的委托练.. 自己处理异常
[解决办法]
up