问题
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.
回答1:
Server code
Create a new channel listening communicating on port 8085
TcpChannel channel = new TcpChannel(8085);
Register with remoting channel services.
ChannelServices.RegisterChannel(channel);
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 is simply used to wait for enter (before exiting console application).
Console.ReadLine();
Client code
Same as server side
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel);
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");
Send a message to server
remoteObject.PrintMessage("Hello world!");
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.
回答2:
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:
- Create a form.
- Add a button
- In the button's Click event handler, call your "CLIENT" code that you included in your post.
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.)
来源:https://stackoverflow.com/questions/3325824/remote-client-server-application-cannot-accept-incoming-messages