在c#.net中如何通过互联网向远程计算机发送消息?

发布于 2024-10-05 23:49:22 字数 134 浏览 2 评论 0原文

c#.net Framework 4.0 客户端配置文件,Windows 应用程序.. 我正在开发一款游戏,需要通过互联网将游戏的当前动作发送到安装了相同应用程序(游戏)的远程计算机。以同样的方式,远程计算机游戏的当前动作应该被发送回... 这怎么可能?

c#.net framework 4.0 client profile,Windows application..
i am developing a game which needs to send its current movements of the game through internet to remote computer where the same application(game) is installed.In Same way current movements of the game of remote computer should be send back...
How this could be possible ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浅沫记忆 2024-10-12 23:49:22

到目前为止,所有答案都使用基于 TCP 的方法。如果您需要高性能和低延迟,那么您可能会发现使用 UDP 更好。

TCP 带来了大量的开销,以保证数据包在丢失时将被重新发送(以及各种其他功能)。另一方面,UDP 让您自行处理未到达的数据包。如果您的游戏中丢失奇怪的更新并不重要,您可以通过使用 UDP 而不是 TCP 来实现更好的带宽使用、延迟和可扩展性。

尽管如此,UDP 仍然会给您带来防火墙、安全等所有问题。

如果您需要让它正常工作而不用担心防火墙问题,那么您需要选择通过端口 80 使用 HTTP 的解决方案。

All the answers so far are using a TCP based approach. If you need high performance and low latency then you might find it better to use UDP instead.

TCP brings a lot of overhead with it to guarantee that packets will be resent if they are lost (and various other bits of functionality). UDP on the other hand leaves it up to you to deal with packets not arriving. If you have a game where losing the odd update isn't important you can achieve far better bandwidth use, latency and scalability by using UDP instead of TCP.

UDP still leaves you with all the issues of firewalls, security etc though.

If you need to have it work without worrying about firewalls being an issue then you want to choose a solution that uses HTTP over port 80.

给我一枪 2024-10-12 23:49:22

为此,您需要通过 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);
    }
}

希望这会有所帮助。

To do that you need to implement a client-server behavior through TCP/IP
There are very different ways to do this
This code I've written could give you a start (it's an option, but not the only one, I leave it off to you to choose the method that suits you best)

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

Hope this Helps.

雪化雨蝶 2024-10-12 23:49:22

您应该研究一些中间件技术,例如 WCF网络服务
这是面向对象的,当你第一次掌握它时很容易开发

You should look into some middleware teknologies like WCF, Web service
this is object oriented and easy to develop when you first get the hang of it

甜味拾荒者 2024-10-12 23:49:22

为此你需要考虑很多。

您需要考虑安全防火墙问题等。

如果这些都放在一边,那么您可以设置 tcp 套接字服务器/客户端方法。
快速谷歌一下就会得到很多例子。

查看 Microsoft 示例 http://msdn.microsoft。 com/en-us/library/system.net.sockets.socket.aspx

您尝试过什么?

You have a lot to consider for this.

You will need to think about security, firewall issues etc.

If that is all put to one side, then you can set up a tcp socket server / client approach.
A quick google will yield plenty of examples.

Check out the Microsoft example http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx

What have you tried?

爱*していゐ 2024-10-12 23:49:22

您可以使用 System.Net 和 System.Net.Sockets 命名空间发送 TCP 数据包。

You can use the System.Net and System.Net.Sockets namespaces to send TCP packets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文