【问题标题】:Remote Client-Server Application cannot accept incoming messages远程客户端-服务器应用程序无法接受传入消息
【发布时间】:2011-03-20 13:01:50
【问题描述】:

在我的 .Net 课程中,我们正在制作一个简单的聊天应用程序。我们的教授给了我们一个示例代码如下:

服务器:

TcpChannel channel = new TcpChannel(8085);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "myobject", WellKnownObjectMode.Singleton);
Console.ReadLine();

客户:

TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel);
RemoteObject remoteObject = (RemoteObject)Activator.GetObject(typeof(RemoteObject), "tcp://localhost:8085/myobject");
remoteObject.PrintMessage("Hello world!");
Console.ReadLine();

远程对象:

[Serializable]
public class RemoteObject : MarshalByRefObject
{
   public void PrintMessage()
   {
      Console.Write("Hello World!");
      Console.ReadLine();
   }
}

使用此代码,它基本上会在每次运行客户端时在服务器控制台屏幕上打印一条“Hello World”消息。但是,我们不明白这是如何工作的,因为他没有完全解释每一行的作用。我们只知道渠道。问题在于,使用这些代码,我们将创建一个聊天的 Windows 窗体。我们能够让这个应用程序发送用户提供的消息,但我们无法弄清楚如何在 Windows 窗体中执行此操作,因为我们不了解开始的代码。

如果有人可以为我们提供一些关于如何在 Windows 窗体中执行此操作的指示和指南,请告诉我们。任何意见表示赞赏。

如果这对我们有任何帮助,下面的代码就是我们现在所能做的:

public partial class Form1 : Form
{
    RemoteObject ro;

    public Form1()
    {
        InitializeComponent();

        TcpChannel serverChannel = new TcpChannel(8085);
        ChannelServices.RegisterChannel(serverChannel, true);
        RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "myobject", WellKnownObjectMode.Singleton);
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            ro = (RemoteObject)Activator.GetObject(typeof(RemoteObject), "tcp://" + txtIpAddress.Text + ":8085/myobject");

            ro.PrintMessage(txtMessage.Text);
            txtChatArea.AppendText(System.Environment.MachineName + ": " + txtMessage.Text + "\n");
            txtMessage.Clear();
        }
        catch (SystemException error)
        {
            MessageBox.Show("Error 101: " + error.Message, "Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

上面的代码基本上是询问第二方(与您聊天的一方)的 IP 地址,然后提供了两个文本框 - 一个用于显示对话(多行),另一个用于接受消息。此代码可以向服务器发送消息。但是,它仍然不能接受来自其他方的任何传入消息。

【问题讨论】:

    标签: c# client-server chat


    【解决方案1】:

    服务器代码

    创建一个新频道监听port8085 上的通信

    TcpChannel channel = new TcpChannel(8085);
    

    注册远程频道服务。

    ChannelServices.RegisterChannel(channel);
    

    告诉remoting我们正在使用RemoteObject类型的服务,它应该被创建一次。

    RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "myobject", WellKnownObjectMode.Singleton);
    

    Readline 仅用于等待进入(在退出控制台应用程序之前)。

    Console.ReadLine();

    客户端代码

    同服务器端

    TcpChannel channel = new TcpChannel();
    ChannelServices.RegisterChannel(channel);
    

    创建一个远程代理代理,它在 localhost 的 8085 端口上与服务器通信并使用 RemoteObject

    RemoteObject remoteObject = (RemoteObject)Activator.GetObject(typeof(RemoteObject), "tcp://localhost:8085/myobject");
    

    向服务器发送消息

    remoteObject.PrintMessage("Hello world!");
    

    结束语

    您可以向 RemoteObject 类添加更多方法来构建完整的聊天应用程序。

    还可以阅读remoting callbacks。你的教授有点过时了。微软已将远程处理替换为 WCF。

    【讨论】:

    • 谢谢这位先生。还有一件事,你能看看我上面的编辑吗?我已将当前代码的副本放在那里。我只是想知道从哪里开始。因为我们只是从外部项目中引用 RemoteObject 类库。我不知道“Hello World!”是怎么回事。当我什至看不到任何调用 PrintMessage() 方法甚至在服务器端调用 Console.WriteLine("Hello World!") 时,正在打印。非常感谢。
    【解决方案2】:

    我不会通过编写代码来为你做功课,但这可能会让你朝着正确的方向前进:

    客户:

    1. 创建一个表单。
    2. 添加按钮
    3. 在按钮的 Click 事件处理程序中,调用您在帖子中包含的“CLIENT”代码。

    服务器:

    与其调用仅在控制台应用程序中真正有用的“Console.WriteLine()”,不如在服务器需要显示消息时显示一个对话框,或者将文本添加到 TextBox, ListView 或其他控件。

    有很多不同的方法可以做到这一点,在这种情况下,正确的方法实际上取决于您的教授的偏好。我会去他的办公室探查更多信息。 (顺便说一句,大多数教授都喜欢你这样做。他们可能不会给你想要的答案,但更好地了解他们不会有坏处。)

    【讨论】:

      猜你喜欢
      • 2014-02-28
      • 2021-09-05
      • 2015-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多