Pergunta
Este é o meu Olá Mundo Remoting App.
using System;
using System.Collections.Generic;
using System.Text;
namespace Remoting__HelloWorld.UI.Client
{
public interface MyInterface
{
int FunctionOne(string str);
}
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Remoting__HelloWorld.UI.Client
{
class MyClient
{
public static void Main()
{
TcpChannel tcpChannel = new TcpChannel();
ChannelServices.RegisterChannel(tcpChannel);
MyInterface remoteObj = (MyInterface)
Activator.GetObject(typeof(MyInterface), "tcp://localhost:8080/FirstRemote");
Console.WriteLine(remoteObj.FunctionOne("Hello World!"));
}
}
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using Remoting__HelloWorld.UI.Client;
namespace Remoting__HelloWorld.UI.Server
{
public class MyRemoteClass : MarshalByRefObject, MyInterface
{
public int FunctionOne(string str)
{
return str.Length;
}
}
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Remoting__HelloWorld.UI.Server
{
class Program
{
static void Main(string[] args)
{
TcpChannel tcpChannel = new TcpChannel(9999);
ChannelServices.RegisterChannel(tcpChannel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyRemoteClass), "FirstRemote", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Press ENTER to quit");
System.Console.ReadLine();
}
}
}
Mas depois de executar este aplicativo, eu estou recebendo a seguinte exceção:
No connection could be made because the target machine
actively refused it 127.0.0.1:8080
Como posso corrigir isso?
Solução
Altere o servidor como este:
TcpChannel tcpChannel = new TcpChannel(8080);
ou alterar o cliente assim:
Activator.GetObject(typeof(MyInterface), "tcp://localhost:9999/FirstRemote");
No lado do servidor, você está abrindo um canal no número de porta específico (no seu exemplo, você está usando a porta 9999). Em essência, este informa o servidor para 'ouvir' para solicitações de entrada na porta 9999. No lado do cliente, você diga a ele o que número de porta para conectar-se (no seu exemplo, você está usando a porta 8080). Então você tem uma situação onde o servidor está escutando na porta 9999, mas o seu cliente está tentando se conectar na porta 8080. Esses números de porta devem corresponder.
Outras dicas
o servidor TcpChannel é 9999 as solicitações do cliente em relação 8080
O servidor está abrindo o canal na porta 9999 enquanto o cliente está à procura de 8080.