Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in c#.net how to send message to remote computer throught internet?

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 ?

like image 538
Anand Avatar asked Dec 14 '25 22:12

Anand


2 Answers

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.

like image 146
andynormancx Avatar answered Dec 16 '25 12:12

andynormancx


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.

like image 26
Mehdi LAMRANI Avatar answered Dec 16 '25 12:12

Mehdi LAMRANI



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!