.Net远程处理问题.不能通过LAN或Internet工作 [英] .Net Remoting Problem. NOt working over LAN or Internet

查看:88
本文介绍了.Net远程处理问题.不能通过LAN或Internet工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的远程服务器中的form.cs

This is the form.cs in my REMOTE SERVER

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);
        }
    }
}



而且此代码存在于我的远程客户端中
它返回textBox1和textBox3中两个数字的总和.



And This code is present in my Remote Client
It returns the sumation of two numbers in textBox1 and textBox2 in textBox3

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

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;
        }
    }
}



当我在txtPath中输入localhost
效果很好


但是当我尝试通过lan
连接它时 (假设我的IP地址是192.168.1.1,服务器的IP地址是192.168.1.2)
我在txtPath中输入192.168.1.2

我收到System.Net.Sockets.SocketException:连接尝试失败,因为连接的一方在一段时间后没有响应,或者建立的连接失败,因为连接的主机未能响应192.168.1.2

当我在互联网上尝试时
我得到了例外
无法建立连接,因为目标计算机主动拒绝了它."


:confused:

当我尝试通过LAN连接时得到此消息:



在此先感谢

[已修改:pre标签用于格式化大块代码,例如:



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:

for (int i = 0; i<25; i++)
{
  Console.WriteLine(i.ToString());
}


代码标签用于格式化一行文本中的短语或关键字,例如是否要告诉某人使用TextBox而不是Label.您不能同时使用两者.

另外,请勿添加答案以添加信息.您需要更新问题.]


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.]

推荐答案

您是否已调整目标计算机上的防火墙以允许该端口上的传入流量?

它与您的代码无关,但与您用来在其上缠绕对象的机器有关.
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.


您发布的代码显示了代码中的转义序列会给您带来意想不到的结果的字符串,但是我怀疑那只是发布的疏忽.如果没有,从那里开始.

您将无法通过Internet与您发布的地址建立任何连接,因为那是一个不可路由的地址,不会超出LAN.这很简单,但是检查显而易见的内容却丝毫不伤脑筋.

对于局域网问题,我没有实质性建议.检查防火墙时,是否同时检查了两台计算机?服务器上的那个甚至会阻止连接,甚至无法开始连接,但这听起来像是返回连接失败.那可能被客户端计算机上的防火墙阻止.检查Windows防火墙和由病毒防护软件添加的个人"防火墙.

无关紧要的是,我认为最好在远程处理Singleton对象时设置租用期限".您可以通过在类中添加
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; }

来实现. (返回null会无限期使用.默认值为5分钟.)

to your "xx" class. (A null return gives it infinite lifetime. The default is 5 minutes.)


是的,我什至尝试关闭防火墙...

我的代码正确吗?
Yes I have even tried turning off the firewall...

Are my codes correct???


这篇关于.Net远程处理问题.不能通过LAN或Internet工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆