How to access remote object from other computer in remoting - c#

I have a server object as
TcpChannel tcp = new TcpChannel(1234);
ChannelServices.RegisterChannel(tcp, false);
string s = ConfigurationManager.AppSettings["remote"];
RemotingConfiguration.RegisterWellKnownServiceType(typeof(TestRemoting.InitialClass1), "TestRemoting", WellKnownObjectMode.Singleton);
Console.WriteLine("Server is running on 1234 channel...");
Console.WriteLine("Press Enter to Stop");
Console.ReadLine();
and I am accessing that object in mu client form like
InitialClass1 Icls = (InitialClass1)Activator.GetObject(typeof(InitialClass1), "tcp://localhost:1234//TestRemoting");
Now I am keeping my remote object in another computer. How can I access my server object from that computer. If I access the server object from my (local) computer, I will use
"tcp://localhost:1234//TestRemoting"
I am using localhost here, because my server object and my client is same. So If those two are different, How can I access the server object.
I tried with my another computer IP
as
tcp://200.100.0.52:1234//TestRemoting
that time I am getting an exception as
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 200.100.0.52:1234

The message means most of the time that the client cannot find the service. You can use the following steps to determine where the connection fails:
Check if the service is running on the remote machine.
Check if the port number is correct.
Ping the remote machine from the client.
Open a telnet session to the remote machine on the port number of service.
The step on which it fails gives an indication of why it is failing.

Related

Can't connect to WebSocket server on remote machine

I use the trial of PowerWebSocket API
https://www.noemax.com/powerwebsockets/
I want connect a Client to the Server, but the server is on a remote machine.
Server
var server = new WebSocketServer();
server.AddEndpoint<LevelOneWS>("http://localhost:30000");
server.Open();
Client
var LevelOne = new WebSocketClient<TickService>http://www.xxx.yyy.zzz:30000);
LevelOne.Open();
The port 30000 is open on the server, client, router/modem (with redirection).
But I can't connect to server, AddEndPoint cant connect, server dont accept the connection.
On the same computer, full localhost, its perfect.
I think the error is, that your server listens on localhost, that means the internal loopback interface: 127.0.0.1 It should listen on it's external interface.

Renci ssh.net No connection could be made because the target machine actively refused it

We recently deployed a project into production and are now receiving this error message when we attempt to connect to the external sftp machine, "No connection could be made because the target machine actively refused it". When I was developing the application and testing it, we had no issues connecting to this server.
What would be different? I have administrative privileges and the app pool on the production server does not. I'm not sure if this could be what's causing the issue or if it may be something on the external client's server or their firewall blocking us.
If you are using localhost, specify 127.0.0.1 instead.
When I was using SSH.Net and the host was specified as localhost it threw the exception that the host actively refused the connection WSAECONNREFUSED - 10061. When I specified 127.0.0.1 instead, the connection could be made.
I think this is due to the implementation in SSH.Net:
internal static IPAddress GetIPAddress(this string host)
{
IPAddress ipAddress;
if (!IPAddress.TryParse(host, out ipAddress))
ipAddress = Dns.GetHostAddresses(host).First();
return ipAddress;
}
Which does not resolve a valid hostname for localhost, apparently. C# Interactive yields the following:
> Console.WriteLine(Dns.GetHostAddresses("localhost").First());
::1
The value ::1 does not seem to be a valid IP address for sockets to use.
This is a standard TCP error
WSAECONNREFUSED - 10061
From the Client's point of view it means 'there is no socket at that address listening to that port' (the "actively refused" is a red herring).
i) check the address
ii) check the port
iii) check firewall(s)
From our extensive experience (we offer own networking components) -- it's a firewall problem. The firewall doesn't let your requests pass. It's a common situation when you run say Putty, and it works, but your code doesn't. This is because many firewalls detect well-known applications and let them pass, while preventing other applications.

TcpClient error: No connection could be made because the target machine actively refuses it

I wrote a simple tcp/ip chat and it worked with the localhost IP(127.0.0.1) as well as connecting to another computer on the local network.
Through the Internet (v4 IP address) it didn't work so I tried the codeproject example:
I downloaded but it threw the same exception on connecting:
TcpClient Client = New TcpClient(IPAddr, 65535);
I tried with another port and turned off Windows-Firewall and Avira-Free.
Update:
If I connect to another Internet IP address, the following exception is thrown:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond IP-Address: 65535
Probably this won't help, but perhaps try another port?

What is the easiest way to test if a user is connected to our WAN?

A bit of background. I work for a corporation that has a global WAN. External users connect to it through a VPN and must be connected to it in order to have access to their emails and, of course, shared network directories.
I have a .Net application that is published and installed from our website using ClickOnce. User's who both do and do not work for the company have access to this application and it sends requests to a web service that is also published to the web server.
For reasons outside the scope of this question, when a user attempts to log into the application they should connect to the web service server by explicitly referencing the the computer name of the server (i.e. http://**server**/webservice/ws.asmx.) If a user is not connected to our WAN, they need to access it through our web address (i.e. https://**www.companywebsite.com**/webservice/ws.asmx.)
I know how to dynamically set the associated URL with the web service. My problem is that I can't think of an fast, reliable and easy way to test for the WAN connection.
Initially I tried to send a ping request (using the .Net Ping class) to the server with a rather short timeout. However, calling a ping operation against the server, when not connected to the WAN, returns an exception that states
An exeception occurred during the Ping request.
Below is the code associated with the property that tests for the WAN connection. It's simple but it keeps giving me above message when I test this off of our network.
public static bool ConnectedToWAN
{
get
{
try
{
Ping ping = new Ping();
PingReply reply = ping.Send("server", 2000);
return reply.Status == IPStatus.Success;
}
catch (Exception e)
{/* ' An exeception occurred during the Ping request.' */};
}
I would prefer not returning my connection state based off of the exception that I'm getting. Rather, I'd make an explicit call to the network that can validate the connection.
One quick note, I can't check the user's IP. Some of our users work for other organizations that have there own WAN. Since I'm checking their IP within a .Net WinForms application there is a chance that their IP address may be associated with their WAN. Instead, I need to check and see if they are specifically connected to our WAN, not just any WAN.
How can I do this?
I think the question is you need to resolve the host name if there are connected within company network rather than public accessible internet.
if that is the case, you can use DNS.BeginGetHostEntry and using asynchronous method to get the domain name system for specified host computer.
e.g
var host = '**Internal Server Name**';
Boolean isWANConnected = false;
String ConnectMessage ;
AsyncCallback callBack = new AsyncCallBack(GetHostName);
Dns.BeginGetHostEntry(host, callBack, host);
static void GetHostName(IAsyncResult result)
{
string hostname = (string)result.AsyncState;
try
{
IPHostEntry host = Dns.EndGetHostEntry(result)
ConnectMessage = host as String;
}
catch(SocketException e)
{
isWANConnected = false;
ConnectMessage = e.Message;
}
}
Can a user not on the WAN resolve that server IP? If not, you can use a DNS query to determine where you are.

TCP Connect to Azure Fails

I recently deployed an Azure app to the staging environment which is supposed to accept TCP packets in port 2993. When run in the Azure emulator, it does this very well, and performs exactly as it should.
However, now that it is on Azure's staging servers, whenever I try to connect to the TCP port, i get the following SocketException:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
My web app has two instances, one is a worker role (it has 2993 configured in its endpoints) and a web role (only for port 80). Port 80, by the way, functions perfectly. The web app was created with Visual Studio in C#. Why is this happening and how can I fix it?
All help is greatly appreciated.
I would like to explain what was the issue and why adding above code solve this problem for others benefit.
In your worker role, you configure any TCP endpoint along with any port number. When this VM starts the VM is provisioned to use this port and firewall is included an exception to provide access to this port. By the time your is ready to start your Azure VM already have TCP/IP address and configured port is enabled.
Now when your role starts and if you have any service or application required access this port you really need to l
For example If I have Tomcat/Java configured in my worker role, I must have to create a TCP listener on my configured port. This can be done when my worker role starts as below:
Get the IP address and Port number of the current role instance
Create a TCP Listener and start on this IP address and Port
Add the code to perform step 1) and 2) in my worker role OnStart() function.
The code will look like as below:
TcpListener TomcatPortListener = new TcpListener(RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["TomcatWeb80"].IPEndpoint);
TomcatPortListener.Start();
In your case you you did exactly the same you got the IP address and Port number from and then created an endpoint from a socket address using IP/Port:
RoleInstanceEndpoint externalEndPoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["TCPin"];
IPEndPoint localEndPoint = new IPEndPoint(externalEndPoint.IPEndpoint.Address, externalEndPoint.IPEndpoint.Port);
localEndPoint.Create();
It is a must setting for any web or worker role which create TCP endpoint.
It seems the problem had something to do with the program's detection of the server's IP address. By using the following server code instead, I managed to fix the problem:
RoleInstanceEndpoint externalEndPoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["TCPin"];
IPEndPoint localEndPoint = new IPEndPoint(externalEndPoint.IPEndpoint.Address, externalEndPoint.IPEndpoint.Port);
The IPEndpoint can then be used with the tcpListener socket.

Categories