[英]How to make the connection string of an application in C#.net,Computer independent?
[英]in c#.net how to send message to remote computer throught internet?
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);
}
}
希望这可以帮助。
你需要考虑很多。
您需要考虑security
, firewall issues
等。
如果这一切都放在一边,那么你可以设置一个tcp套接字服务器/客户端方法。 一个快速的谷歌将产生大量的例子。
查看Microsoft示例http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx
你有什么尝试?
您可以使用System.Net和System.Net.Sockets命名空间来发送TCP数据包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.