Click here to Skip to main content
15,887,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im coding a Chat App using .NET Remoting. The problem is that i want to use Invoke to edit a form (addMsg) in the client but i cant call it because i can only instantiate the superclass (in clientObject constructor) and not the subclass because its not visible for me. I can't explain the problem very well but its explicit in the code in the comments.

Client code:
C#
namespace DADChat {
public partial class Form1 : Form
{
    private MyRemoteObject objetoServidor = null;
    private clientObject objetoCliente = null;

    public Form1()
    {
        InitializeComponent();
    }

    private void sendButton_Click(object sender, EventArgs e)
    {
        string mensagem = messageBox.Text;
        string nick = nickBox.Text;

        objetoServidor.enviaMensagem(nick, mensagem);
    }

    public void addMsg(string m) //adiciona mensagem à conversa
    {
        textBox4.Text += "\r\n" + m;
    }

    private void connectButton_Click(object sender, EventArgs e)
    {
        string url = "tcp://localhost:" + portBox.Text + "/objectClientName";
        int port = Int32.Parse(portBox.Text);
        string nick = nickBox.Text;

        TcpChannel channel = new TcpChannel(port);
        ChannelServices.RegisterChannel(channel, true);

        objetoCliente = new clientObject(this); /* i use this to pass the reference of this object so i can edit the form in the remote class */

        RemotingServices.Marshal(objetoCliente, "objectClientName", typeof(clientObject));

        objetoServidor = (MyRemoteObject)Activator.GetObject(typeof(MyRemoteObject), "tcp://localhost:8096/MyRemoteObjectName");

        NovoCliente cliente = new NovoCliente();
        cliente.Nome = nick;

        objetoServidor.adicionarCliente(cliente.Nome, url, port);
        objetoServidor.printListaClientes();
    }
}

}

The Remote class: namespace ChatObjects { public delegate void delChat(string m);
public class MyRemoteObject : MarshalByRefObject
{
    private List<novocliente> listaClientes = new List<novocliente>();
    public event delChat serverParaCliente;

    public string metodoOla()
    {
        return "Ola!";
    }

    public void adicionarCliente(string nome, string url,int port)
    {
        if (nome != null)
        {
            NovoCliente cliente = new NovoCliente();
            cliente.Nome = nome;
            cliente.Porto = port;
            cliente.URL = url;
            listaClientes.Add(cliente);
        }
    }

    public void printListaClientes()
    {
        for (int i = 0; i < listaClientes.Count; i++)
            Console.WriteLine("Cliente: " + listaClientes[i].Nome + " e a sua localização é " + listaClientes[i].Porto + "\n");
    }

    public void enviaMensagem(string nickname, string mensagem) //método responsável por receber a mensagem do utilizador e envia para o chat 
    {
        string mensagemFinal = nickname + ": " + mensagem;

       foreach (NovoCliente cliente in listaClientes)
        {
            clientObject obj = (clientObject)Activator.GetObject(typeof(clientObject), cliente.URL);
            obj.recebeMsg(mensagemFinal);
        }
    }
}

public class clientObject : MarshalByRefObject
{
    public Form f; /* the problem resides here because to call addMsg it should Form1 f and not Form */
    public clientObject(Form f)
    {
        this.f = f;
    }

     public void recebeMsg(string msg)
    {
        f.Invoke(new delChat(f.addMsg), new object[] { msg });   /* cant call addMsg */  
    }
}
public class NovoCliente
{
    private string nome;
    private int porto;
    private string url;

    public string Nome
    {
        get
        {
            return nome;
        }
        set
        {
            nome = value;
        }
    }
    public string URL
    {
        get
        {
            return url;
        }
        set
        {
            url = value;
        }
    }

    public int Porto
    {
        get
        {
            return porto;
        }
        set
        {
            porto = value;
        }
    }
}

}

Thank you for the help in advance
Posted
Updated 15-Oct-15 11:55am
v2

1 solution

I am not sure how your chat code works, however I can suggest to use an interface with addMsg() method and have your clientObject class modified like below.

C#
public interface MyInterface
{
 void addMsg(string msg);
}

public class MyRemoteObject : MarshalByRefObject, MyInterface
{
//Your exisitng code
}

public class clientObject : MarshalByRefObject
{
    public Form f;

    public clientObject(Form f)
    {
        this.f = f;
    }
 
     public void recebeMsg(string msg)
    {
       MyInterface obj = (MyInterface) f;

        f.Invoke(new delChat(obj.addMsg), new object[] { msg });  
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900