Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the form.cs in my REMOTE SERVER
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Microsoft.Win32;

namespace remoteserver
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            TcpChannel ch = new TcpChannel(8085);
            ChannelServices.RegisterChannel(ch);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(remoteclass.xx), 
                    "Ankit", WellKnownObjectMode.Singleton);
        }
    }
}


And This code is present in my Remote Client
It returns the sumation of two numbers in textBox1 and textBox2 in textBox3
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters.Soap;
using System.Threading;

namespace remoteclient
{
    public partial class Form1 : Form
    {
        remoteclass.xx obj = new remoteclass.xx();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {
                obj = (remoteclass.xx)Activator.GetObject(typeof(remoteclass.xx),
                  "tcp:\\"+txtPath.Text+":8085\Ankit");
                int x = Int32.Parse(textBox1.Text);
                int y = Int32.Parse(textBox2.Text);

                textBox3.Text = (obj.sum(x, y)).ToString();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }



My REMOTE CLASS

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.Remoting;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.InteropServices;
namespace remoteclass
{
    public class xx : MarshalByRefObject
    {
        public int sum(int a, int b)
        {
            return a + b;
        }
    }
}


when I input localhost in txtPath
it works great


But when I try to connect it over lan
(Say my ip address is 192.168.1.1 and the server's ip address is 192.168.1.2)
and i input 192.168.1.2 in txtPath

I get the System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not respond after a period of time, or established connection failed because connected host has failed to respond 192.168.1.2

and when i try it over the internet
I get the exception
"No connection could be made because the target machine actively refused it."


:confused:

I get this when I try to connect over LAN:



Thanks in advance

[Modified: pre tags are meant for formatting large chunks of code like:
C#
for (int i = 0; i<25; i++)
{
  Console.WriteLine(i.ToString());
}

code tags are meant for formatting phrases or keywords within a line of text like if you wanted to tell someone to use a TextBox instead of a Label. You don't use both at the same time.

Also, don't add an answer to add information. You need to update the question.]
Posted
Updated 3-Aug-10 9:08am
v3

have you adjusted the firewall on the target machine to allow incoming traffic on that port?

It's nothing to do with your code, but everything to do with the machine that you're using to hose the object on.
 
Share this answer
 
The code you posted shows escape sequences in your strings that will give you unexpected results, but I suspect that was just a posting oversight. If not, start there.

You won't get any connection over the Internet with the address you posted since that is a non-routable address that won't go outside the LAN. That's simplistic, but it never hurts to check the obvious.

I don't have substantial suggestions for the LAN issue. When you checked the firewall, did you check both machines? The one on the server would block the connection from even starting to connect, but it sounds like it's the return connection that's failing. That one could be blocked by a firewall on the client machine. Check for Windows Firewall and a "personal" firewall added by virus protection software.

Somewhat unrelated, I believe it's advisable to set the Lease Lifetime when remoting Singleton objects. You would do this by adding
public override object InitializeLifetimeService()
{ return null; }
to your "xx" class. (A null return gives it infinite lifetime. The default is 5 minutes.)
 
Share this answer
 
Yes I have even tried turning off the firewall...

Are my codes correct???
 
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