How to pass not simple types using .NET Remoting
can't count how many .NET remoting tutorials and book excerpts i read.
Every single time the example involves a chat client passing strings or
some other example where strings, ints, and simple user defined types
are begin passed. I am looking for an example which shows me how to
pass pointer types, like most things in .NET library. For example, I
want to pass a FileInfo or FileStream object from server to client. I
tried that, it didn't work. Someone told me that's because
System.IO.FileStream object, for example, is a pointer to a memory
location which would have meaning on the machine it was instantiated
on. If I pass that pointer to another machine than that pointer would
have no meaning on that machine.
But I did check and FileInfo
object derives from MarshalByRefObject, so I thought that means it
could be used accross machine boundries.
I am trying to write
the simplest and non-dirties of file transfer apps where the client
passes a FileStream to a server, server reads it and saves it localy.
Simple stuff. Cannot figure it out.
Here is a simple client server app where the server accepts a FileInfo object and shows its FileName property, simple. The server also accepts a string object and shows it. The server will have no problems displaying the string or the FileName of a FileInfo object when the programs are run on the same machine, using "tcp://localhost" for connection. Howerver, put the server and client on different machines, and server will show the string no problems, but it will hang when it tries to access FileInfo object:
Client:
-
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.IO;
namespace RemotingTest
{
class Program
{
static void Main(string[] args)
{
TcpChannel tcpChannel = new TcpChannel();
ChannelServices.RegisterChannel(tcpChannel, false);
RemotingConfiguration.RegisterWellKnownClientType(
typeof(ServerInterface),
"tcp://www.server-ip.com/Server"); // change to appropriate address here
ServerInterface server = new ServerInterface();
server.ShowFileInfo("C:\\test.txt"); // works
server.ShowFileInfo(new FileInfo("C:\\test.txt")); // hangs
}
}
}
Server interface:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace RemotingTest
{
public class ServerInterface: MarshalByRefObject
{
public void ShowFileInfo(string filename)
{
Console.WriteLine(filename);
}
public void ShowFileInfo(FileInfo fileInfo)
{
Console.WriteLine(fileInfo.Name);
}
}
}
Server:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace RemotingTest
{
class Program
{
static void Main(string[] args)
{
TcpChannel tcpChannel = new TcpChannel(1024);
ChannelServices.RegisterChannel(tcpChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(ServerInterface),
"Server", WellKnownObjectMode.SingleCall);
}
}
}

