Hello everyone,
In my dotNet 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. Thanks a lot in advance.
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 ipaddress of the second party (the party you are chatting with) and then provided are two textboxes - 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 incomming message from other parties.