远程客户端 - 服务器应用程序无法接受传入邮件 [英] Remote Client-Server Application cannot accept incoming messages

查看:204
本文介绍了远程客户端 - 服务器应用程序无法接受传入邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my .Net class, we are making a simple chat application. Our professor gave us a sample code as follows:

服务器:

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();

REMOTE OBJECT:

REMOTE OBJECT:

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

使用这个代码,它基本上打印一个Hello World 消息在服务器控制台屏幕上每次运行客户端。然而,我们不明白这是如何工作的,因为他没有完全解释每行的作用。我们只知道这些频道。抓住的是,使用这些代码,我们要创建一个Windows窗体的聊天。我们能够让这个应用程序发送一个由用户提供的消息,但我们不能弄清楚我们如何可以在Windows窗体中做,因为我们不明白代码开头。

With this code, it basically prints a "Hello World" message on the server console screen each time the client is run. However, we do not understand how this works since he didn't fully explain what each line does. We only know about the channels. The catch is that with these codes, we are to create a Windows Form of a chat. We were able to make this application send a message provided by the user but we can't figure out how we can do it in a windows form since we do not understand the code to start with.

如果任何人可以帮助我们一些指导和指南,我们如何能在Windows窗体中做到这一点,请让我们知道。

If anyone can help us with some pointers and guidelines on how we can do this in a Windows Form, please let us know. Any input is appreciated.

如果这将有助于以任何方式,下面的代码是我们现在可以走:

If this would help in any way, the code below is as far as we could go as of now:

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地址,然后提供了两个文本框 - 一个用于显示对话(多行),而另一个用于接受消息。此代码可以向服务器发送消息。

The code above basically asks for the IP Address of the second party (the party you are chatting with) and then provided are two text boxes - one is for displaying the conversation (multilined) while the other is for accepting messages. This code can send message to the server. But still, it cannot accept any incoming message from other parties.

推荐答案

服务器代码

创建新频道监听在 port 8085

Create a new channel listening communicating on port 8085

TcpChannel channel = new TcpChannel(8085);

使用远程管理服务注册。

Register with remoting channel services.

ChannelServices.RegisterChannel(channel);

告诉远程处理我们使用RemoteObject类型的服务,应该创建一次。

Tell remoting that we are using a service of type RemoteObject and it should be created once.

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

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

Readline is simply used to wait for enter (before exiting console application).

Console.ReadLine();

Console.ReadLine();

客户端代码

与服务器端相同

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

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

Create a remoting proxy proxy which communicates with server at localhost on port 8085 and used RemoteObject

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

向服务器发送邮件

remoteObject.PrintMessage("Hello world!");

最后字词

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

You can add more methods to the RemoteObject class to build a complete chat application.

另请参阅远程回调。你的教授有点过时了。 Remoting已被微软的WCF所取代。

Also read about remoting callbacks. Your professor is a bit out of date. Remoting have been replaced by WCF by microsoft.

这篇关于远程客户端 - 服务器应用程序无法接受传入邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆