本机地址默认为localhost
或者是127.0.0.1
System.Net.Dns.GetHostByName(String hostName)出现异常可以使用
try..catch来进行捕获.
最好使用ip地址

解决方案 »

  1.   

    gujunyan(ivy阿亮):你可能是误解我的意思了。出现异常的信息如上
      

  2.   

    给你个例子,已经经过测试没有问题了:
    share.dll
    如下:
    using System;
    namespace helloremoting
    {
    public class sayhello:System.MarshalByRefObject
    {
    public string hello(string greeting)
    {
    Console.WriteLine("now is remoting transfer the value:{0}",greeting);
    return "hello";
    }
    }
    }server.cs如下:
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;class simpletcpsrvr
    {
    public static void Main()
    {
    int recv;
    byte[] data=new byte[1024];
    IPEndPoint ipep=new IPEndPoint(IPAddress.Any,9050); Socket newsock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); newsock.Bind(ipep);
    newsock.Listen(10);
    Console.WriteLine("waiting for a client  ");
    Socket client=newsock.Accept();
    IPEndPoint clientep=(IPEndPoint)client.RemoteEndPoint;
    Console.WriteLine("Connected with {0} at port{1}",clientep.Address,clientep.Port); string welcome="welcome to my test server";
    data=Encoding.ASCII.GetBytes(welcome);
    client.Send(data,data.Length,SocketFlags.None); while(true)
    {
    data=new byte[1024];
    recv=client.Receive(data);
    if(recv==0)
    break; Console.WriteLine(Encoding.ASCII.GetString(data,0,recv));
    client.Send(data,recv,SocketFlags.None); } Console.WriteLine("disconnected from {0}",clientep.Address);
    client.Close();
    newsock.Close();
    }
    }
    client.cs
    如下:
    using System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using helloremoting;namespace remotingsample
    {
    public class client
    {
    public static int Main(string[] args)
    {
    TcpChannel chan=new TcpChannel();
    ChannelServices.RegisterChannel(chan);
    sayhello obj=(sayhello)Activator.GetObject(typeof(sayhello),"tcp://localhost:8085/sayhello");
    if(obj==null)
    Console.WriteLine("cant not set the place to the remote server");
    else
    Console.WriteLine(obj.hello("hello,i am client,i have conect to the server"));
    Console.ReadLine();

    return 0;

    }
    }
    }
    测试的时候一定要先打开服务器server.exe,然后再运行client.exe
      

  3.   

    1. 网卡确定安装好了吗?
    2. Tcp/Ip协议安装了吗?Ip地址有没有指定?
    3. 机器的HostName是什么?首先查一下你的机器名,在commandline环境下ping你的机器名,ping到IP地址之前不需要调试你的程序。是机器设置问题。
      

  4.   

    机器名在My Computer的property里面可以看到。
    从My Network Places的property里面设置IP地址,网卡信息。
      

  5.   

    如果你开机的时候没有插网线,网卡不会被初始化。你的程序可能会有问题。我的cbuilder的程序是这样,不知道C#是否会针对这种情况单独处理?你可以ipconfig一下看看。如果有问题,应该就是这个原因了。
      

  6.   

    kuangren(J※『天若有情天亦老,人间正道是沧桑』):你的代码我试过了,一样的异常现象,我想可能真是网络没有配置好的原因,可我的电脑是单用户的,没条件和其他电脑联网,怎么办呀?
    IP/Tcp我也配置过了,也不行。
      

  7.   

    呵呵,没有接上网线,实际上Tcp/Ip服务除了localhost/127.0.0.1是正常的,其它都不可以被访问到,就算你设了计算机名称也没有用,设了ip也白搭。这个问题是系统造成的,跟你用什么编程工具没有关系。你就用localhost有没有问题?
    实在不行了,你另外再买一个网卡,自己跟自己接。(注意要互连线,就是接线顺序要改变的那种)