给你个例子,已经经过测试没有问题了: 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();
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
2. Tcp/Ip协议安装了吗?Ip地址有没有指定?
3. 机器的HostName是什么?首先查一下你的机器名,在commandline环境下ping你的机器名,ping到IP地址之前不需要调试你的程序。是机器设置问题。
从My Network Places的property里面设置IP地址,网卡信息。
IP/Tcp我也配置过了,也不行。
实在不行了,你另外再买一个网卡,自己跟自己接。(注意要互连线,就是接线顺序要改变的那种)