2

I have 2 pc's with some software, now I need one pc to execute a method on another pc, now I've searched high and low but could not find anything on how to do this. I could do this with writing my own little interface that serializes the arguments, method name and return objects and sending this over a socket then deserialize this execute the method using reflection, and return an result object over the socket. But I would like someone else's opinion before I start writing something that is much easier another way.

  • Send multiple arguments (they will all be received and send as an object)
  • Return an object
  • serialize back an exception object if any has occurred

I have not done anything in serializing objects and sending them over a socket, but are all standard objects serializable? Like a List<> array[] float dateTime?

I hope to have explained this ok, if not I'm sorry and ask what is not clear.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jeffnl
  • 261
  • 1
  • 4
  • 21

3 Answers3

3

Create service WCF and config WCF to work over TCP.

This will give you most things 'out of the box' (serialize /deserialize, open/close socket )

There are good examples here, here and good reading here

Community
  • 1
  • 1
Mzf
  • 5,210
  • 2
  • 24
  • 37
  • I have searched for examples for this, but i canot find anything i understand completely, what i need is that there are multiple comuters on a network that can execute methods on the other comuters, but i have not found any example that does this over the network that is easy to read. – Jeffnl Jun 10 '13 at 07:41
1

I have searched the internet for examples and pasted together some code, i'm posting it here if somone needs it too. This is a dirty code but it works, the InvokeMethod is on the client side, and the startIBC is what needs to be started on each server:

[ServiceContract]
    public interface IBlissRequest
    {
        [OperationContract]
        object SystemRequest(string InstanceName, string MethodName, params object[] Parameters);
    }

    public class BlissRequest : IBlissRequest
    {
        public object SystemRequest(string InstanceName, string MethodName, params object[] Parameters)
        {
            return System21.BlissProcessingUnit.BPU.RequestFromIBC(InstanceName, MethodName, Parameters);
        }
    }

public static object InvokeMethod(string targetIpAddress, string InstanceName, string MethodName, params object[] Parameters)
        {
            try
            {
                var ep = "net.tcp://" + targetIpAddress + ":9985/IBC";

                NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);

                ChannelFactory<IBlissRequest> pipeFactory = new ChannelFactory<IBlissRequest>(binding, new EndpointAddress(ep));

                IBlissRequest pipeProxy = pipeFactory.CreateChannel();

                return pipeProxy.SystemRequest(InstanceName, MethodName, Parameters);
            }
            catch 
            {
                BPUConsole.WriteLine(BPUConsole.WriteSource.IBC, "Unable to execute method: '" + MethodName +"' on Instance: '"+InstanceName+"' becouse IBC is unable to connect to: "+ targetIpAddress);
                throw new Exception("Unable to connect to: " + targetIpAddress);
            }
        }

        public static void StartIBC()
        {
            var uri = "net.tcp://" + BlissProcessingUnit.BPUInformation.LocalIpAddresses[0] + ":9985";
            Console.WriteLine("Opening connection on: " + uri);

            ServiceHost host = new ServiceHost(typeof(BlissRequest), new Uri[] { new Uri(uri) });

            NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);

            host.AddServiceEndpoint(typeof(IBlissRequest), binding, "IBC");

            host.Open();

            Console.WriteLine("Service is available. " + "Press <ENTER> to exit.");

        }
Jeffnl
  • 261
  • 1
  • 4
  • 21
0

What you are describing sounds like remote procedure calling (RPC). RPC allows you to create a single object on a server which clients can interact with as if it were a local object (so completely avoid the need to deal with sockets). Alternatively each client can also create its own unique server object to interact with.

A full implementation of RPC can be found in the network library networkcomms.net. The following code snippet is taken from the available RPC example and uses an object of type MathClass that can perform simple maths calculations.

The object exists server side:

//Register a single object server side called "Calculator"
RemoteProcedureCalls.Server.RegisterInstanceForPublicRemoteCall<MathClass, IMath>(new MathClass(), "Calculator");

On the client side:

//Get a reference to the remote object named "Calculator"
IMath calc = RemoteProcedureCalls.Client.CreateProxyToPublicNamedInstance<IMath>(connection, "Calculator", out instanceId);
//We can now use the calculator object as if it were local
//The following WriteLine outputs '12' where the calculation was performed on the server
Console.WriteLine(calc.Multiply(4, 3));

Disclaimer: I have to add that I am a developer for this library.

MarcF
  • 3,169
  • 2
  • 30
  • 57