远程客户端-服务器应用程序无法接受传入消息
在我的 .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 地址,然后提供两个文本框- 一个用于显示对话(多行),另一个用于接受消息。这段代码可以向服务器发送消息。但它仍然无法接受来自其他方的任何传入消息。
In my .Net class, we are making a simple chat application. Our professor gave us a sample code as follows:
SERVER:
TcpChannel channel = new TcpChannel(8085);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "myobject", WellKnownObjectMode.Singleton);
Console.ReadLine();
CLIENT:
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:
[Serializable]
public class RemoteObject : MarshalByRefObject
{
public void PrintMessage()
{
Console.Write("Hello World!");
Console.ReadLine();
}
}
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.
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);
}
}
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
服务器代码
创建一个新通道,监听 上的通信端口 8085
注册远程通道服务。
告诉远程处理我们正在使用 RemoteObject 类型的服务,并且应该创建一次它。
Readline 只是用于等待输入(在退出控制台应用程序之前)。
Console.ReadLine();
客户端代码
与服务器端相同
创建一个远程代理代理,它与本地主机的 8085 端口上的服务器进行通信,并使用 RemoteObject
向服务器发送消息
结束语
您可以添加更多方法RemoteObject 类来构建完整的聊天应用程序。
另请阅读远程回调。你的教授有点过时了。 Remoting 已被微软的 WCF 取代。
Server code
Create a new channel listening communicating on port 8085
Register with remoting channel services.
Tell remoting that we are using a service of type RemoteObject and it should be created once.
Readline is simply used to wait for enter (before exiting console application).
Console.ReadLine();
Client code
Same as server side
Create a remoting proxy proxy which communicates with server at localhost on port 8085 and used RemoteObject
Send a message to server
Final words
You can add more methods to the RemoteObject class to build a complete chat application.
Also read about remoting callbacks. Your professor is a bit out of date. Remoting have been replaced by WCF by microsoft.
我不会通过编写代码来为您做作业,但这可能会让您朝着正确的方向前进:
客户:
服务器:
您不必调用仅在控制台应用程序中真正有用的“Console.WriteLine()”,而是必须在服务器需要显示消息时显示一个对话框,或者将文本添加到 TextBox、ListView或其他控制。
有很多不同的方法可以做到这一点,在这种情况下正确的方法实际上取决于教授的偏好。我会去他的办公室询问更多信息。 (顺便说一句,大多数教授都喜欢你这样做。他们可能不会给你你想要的答案,但更好地了解他们也没什么坏处。)
I'm not going to do your homework for you by writing the code, but this may get you going in the right direction:
Client:
Server:
Rather than calling "Console.WriteLine()", which is only really useful in console applications, you'll either have to show a dialog box when the server needs to display a message, or add the text to a TextBox, ListView or other control.
There are many different ways to do this, and the right one in this scenario really depends on your professor's preference. I'd drop by his office and probe for more information. (BTW, most professors love it when you do this. They may not give you the answer that you'd like, but it can't hurt to get to know them better.)