【发布时间】:2010-12-02 10:18:09
【问题描述】:
c#.net framework 4.0 客户端配置文件,Windows 应用程序.. 我正在开发一款游戏,它需要通过互联网将其当前的游戏动作发送到安装了相同应用程序(游戏)的远程计算机。以同样的方式,远程计算机游戏的当前动作应该被发送回...... 这怎么可能?
【问题讨论】:
c#.net framework 4.0 客户端配置文件,Windows 应用程序.. 我正在开发一款游戏,它需要通过互联网将其当前的游戏动作发送到安装了相同应用程序(游戏)的远程计算机。以同样的方式,远程计算机游戏的当前动作应该被发送回...... 这怎么可能?
【问题讨论】:
为此,您需要通过 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);
}
}
希望这会有所帮助。
【讨论】:
您应该研究一些中间件技术,例如 WCF、Web service 这是面向对象的,当你第一次掌握它时很容易开发
【讨论】:
为此,您需要考虑很多。
您需要考虑security、firewall issues 等。
如果这一切都放在一边,那么您可以设置一个 tcp 套接字服务器/客户端方法。 一个快速的谷歌将产生大量的例子。
查看 Microsoft 示例 http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx
你试过什么?
【讨论】:
您可以使用 System.Net 和 System.Net.Sockets 命名空间来发送 TCP 数据包。
【讨论】: