2010-12-02 76 views

回答

1

你應該看看一些中間件teknologies像WCFWeb service 這是面向對象的,易開發,當你第一次得到了它的竅門

2

要做到這一點,你需要實現通過TCP客戶端 - 服務器行爲/ IP
有很多不同的方法可以做到這一點 我寫的這段代碼可以給你一個開始(這是一個選項,但不是唯一的選擇,我將它留給你選擇最適合你的方法)

using System.Runtime.Remoting; 
using System.Runtime.Remoting.Channels; 
using System.Runtime.Remoting.Channels.Tcp; 

static class ServerProgram 
{   
    [STAThread] 
    static void Main() 
    {  
     ATSServer();  
    }  

    static void ATSServer() 
    {   
     TcpChannel tcpChannel = new TcpChannel(7000); 
     ChannelServices.RegisterChannel(tcpChannel); 

     Type commonInterfaceType = Type.GetType("ATSRemoteControl"); 
     RemotingConfiguration.RegisterWellKnownServiceType(commonInterfaceType, 
     "RemoteATSServer", WellKnownObjectMode.SingleCall); 
    } 
} 

public interface ATSRemoteControlInterface 
{ 
    string yourRemoteMethod(string parameter); 
}  

public class ATSRemoteControl : MarshalByRefObject, ATSRemoteControlInterface 
{ 
    public string yourRemoteMethod(string GamerMovementParameter) 
     { 
      string returnStatus = "GAME MOVEMENT LAUNCHED"; 
      Console.WriteLine("Enquiry for {0}", GamerMovementParameter); 
      Console.WriteLine("Sending back status: {0}", returnStatus); 
      return returnStatus; 
     } 
} 

class ATSLauncherClient 
{ 
    static ATSRemoteControlInterface remoteObject; 

    public static void RegisterServerConnection() 
    { 
     TcpChannel tcpChannel = new TcpChannel(); 
     ChannelServices.RegisterChannel(tcpChannel); 

     Type requiredType = typeof(ATSRemoteControlInterface); 

     //HERE YOU ADJUST THE REMOTE TCP/IP ADDRESS 
     //IMPLEMENT RETRIEVAL PROGRAMATICALLY RATHER THAN HARDCODING 
     remoteObject = (ATSRemoteControlInterface)Activator.GetObject(requiredType, 
     "tcp://localhost:7000/RemoteATSServer"); 

     string s = ""; 
     s = remoteObject.yourRemoteMethod("GamerMovement"); 
    } 

    public static void Launch(String GamerMovementParameter) 
    { 
     remoteObject.yourRemoteMethod(GamerMovementParameter); 
    } 
} 

希望這有助於。

2

迄今爲止所有的回答都使用基於TCP的方法。如果您需要高性能和低延遲,那麼您可能會發現使用UDP代替更好。

TCP會帶來很多開銷,以確保數據包在丟失(以及各種其他功能位)時會被重新發送。另一方面,UDP讓你處理未到達的數據包。如果你有一個遊戲,丟失奇怪的更新並不重要,你可以通過使用UDP而不是TCP來獲得更好的帶寬使用,延遲和可擴展性。

儘管UDP仍然讓你面對所有防火牆,安全等問題。

如果你需要把它無需擔心防火牆是一個問題工作,那麼你要選擇使用HTTP通過端口80

+0

同意的解決方案,UDP可能是一個更好的選擇。 – Matt 2010-12-02 13:06:31