简体   繁体   English

用.NET连接2台计算机

[英]Connect 2 Computers with .NET

I'm working on an online version of Connect 4, and I want to be able to connect 2 computers together. 我正在开发Connect 4的在线版本,我希望能够将2台计算机连接在一起。

I have some knowledge on creating networks between computers the TcpClient and Listener classes, however to my knowledge these only work on computer connected to the same network (LAN) 我有一些关于在计算机之间创建TcpClient和Listener类的网络的知识,但据我所知,这些仅适用于连接到同一网络(LAN)的计算机

I want to be able to be on a different part of the Earth and play connect 4 with my little sister, regardless on what LAN we are on. 我希望能够在地球的不同部分与我的妹妹玩连接4,无论我们在哪个局域网上。

Is this possible? 这可能吗?

Yes. 是。

Connecting two computers on a LAN is the same as connecting over the Internet. 连接LAN上的两台计算机与通过Internet连接相同。 The only differences are: 唯一的区别是:

  • You will need to connect based on your external ip address instead of your local (try http://whatismyipaddress.com ) 您需要根据外部 IP地址而不是本地地址进行连接(请尝试http://whatismyipaddress.com
  • You will need to ensure that your router understands to redirect incoming traffic to your computer (your external IP address maps to your router. You need to ensure the router makes the connection back to your computer). 您需要确保您的路由器能够将传入流量重定向到您的计算机(您的外部IP地址映射到您的路由器。您需要确保路由器将连接重新连接到您的计算机)。

Without a more specific question, I can't get more detailed than that. 没有更具体的问题,我无法得到更详细的信息。

Take a look at Jabber, it is an instant messaging protocol (used by Gtalk). 看看Jabber,它是一个即时消息传递协议(由Gtalk使用)。 Hookup with either a public jabber server or setup your own Jabber host. 使用公共jabber服务器连接或设置您自己的Jabber主机。 Jabber-net is a library for .NET Jabber-net是.NET的库

You can extend it to do pretty much all you want including gaming etc. 您可以扩展它以完成您想要的所有操作,包括游戏等。

TcpClient will be able to connect to a socket anywhere on the internet. TcpClient将能够连接到互联网上的任何地方的套接字。 However, firewalls or a NAT-ed network could get in the way. 但是,防火墙或NAT-ed网络可能会妨碍您。 You'll have to make sure at least one of you has a publicly accessible IP address and open port. 您必须确保至少有一个人拥有可公开访问的IP地址和开放端口。

Since you're writing this game, you may consider writing a server-only application that will allow two people to connect from behind firewalls. 由于您正在撰写此游戏,因此您可以考虑编写一个仅限服务器的应用程序,允许两个人从防火墙后面进行连接。 Then, you'll just have to figure out a way to host it. 然后,你只需要找到一种方法来托管它。

That was the first network game I wrote too. 那是我写的第一个网络游戏。

It might be possible. 这可能是可能的。 The problem is that you may have to open ports on a firewall, router, etc. to allow incoming communication from one computer to the other. 问题是您可能必须在防火墙,路由器等上打开端口,以允许从一台计算机到另一台计算机的传入通信。 This is a problem even for professionals. 这对于专业人士来说也是一个问题。

Your best bet might be to create a "game server" with a well-known address. 您最好的选择可能是创建一个具有众所周知地址的“游戏服务器”。 A cheap webserver with a domain name would work nicely. 具有域名的廉价网络服务器可以很好地工作。 One application sends messages to the server using a Web Service, while the other application checks it once a second looking for new messages. 一个应用程序使用Web服务将消息发送到服务器,而另一个应用程序每秒检查一次以查找新消息。

If you have a website you can call a page on the website and log the IP address of the calling computer. 如果您有网站,可以在网站上调用页面并记录主叫计算机的IP地址。 In PHP you can do: 在PHP中,您可以:

$Calling_IP_Address = $_SERVER['REMOTE_ADDR']; 

Then, save the data to a text file. 然后,将数据保存到文本文件中。 When you want to play one of the players needs to call the webpage to log their IP address and the other player reads the saved text file to find out where to connect to. 当你想玩其中一个玩家需要调用网页来记录他们的IP地址而另一个玩家读取保存的文本文件以找出连接的位置。

Job done. 任务完成。

You can use the .NET client and listener classes to connect to any computer on any IP address located anywhere on the planet. 您可以使用.NET客户端和侦听器类连接到位于地球上任何位置的任何IP地址上的任何计算机。 The catch is you will need to know the IP address of the other machine and it will need to accept connections from you. 问题是你需要知道其他机器的IP地址,它需要接受你的连接。

If the other machine is behind a firewall, things get more difficult. 如果另一台机器在防火墙后面,事情变得更加困难。 The firewall will block unsolicited connection requests - including requests from your machine to talk to the other machine behind the firewall. 防火墙将阻止未经请求的连接请求 - 包括来自您的计算机的请求与防火墙后面的其他计算机通信。 Instant messaging clients have this problem all the time - they work around it by having each end of the connection talk to an intermediate server that relays data between the two connections. 即时消息客户端始终存在这个问题 - 它们通过使连接的每一端与在两个连接之间中继数据的中间服务器通信来解决它。

That is, your machine can make calls out to machines that are on the public internet, and when you make such a connection through a firewall your firewall will allow the machines you connect to to reply to you, but you cannot connect directly to another machine that is behind a firewall unless you make prior arrangements like opening a specific TCP/IP port number on the firewall. 也就是说,您的计算机可以拨打公共互联网上的计算机,当您通过防火墙建立连接时,您的防火墙将允许您连接的计算机回复您,但您无法直接连接到另一台计算机除非您事先安排在防火墙上打开特定的TCP / IP端口号,否则它位于防火墙后面。

There are now protocol standards such as UPnP that would allow your computer to request that your local firewall open a port for a specific connection. 现在有协议标准(如UPnP)允许您的计算机请求本地防火墙为特定连接打开端口。 If the computer at the other end also does this, then it would be possible to establish a TCP/IP connection between the two machines through their respective firewalls, IF you can figure out a way to communicate the port numbers to each other. 如果另一端的计算机也这样做,则可以通过各自的防火墙在两台计算机之间建立TCP / IP连接, 如果您可以找到一种方法来将端口号相互通信。 You would need to send the port number that is opened on your firewall to the other computer, and they would need to send you the open port number on their firewall. 您需要将防火墙上打开的端口号发送到另一台计算机,并且需要在防火墙上向您发送开放端口号。 So you'll still need some sort of intermediary to at least exchange this connection info so that each party can connect directly to the other. 因此,您仍然需要某种中介来至少交换此连接信息,以便每一方可以直接连接到另一方。

Build a web site/application for this, and then you do not need to worry about conectivity. 为此构建一个网站/应用程序,然后您不必担心连贯性。 It will also work cross-platform, esp. 它也可以跨平台工作,尤其是。 if you avoid flash. 如果你避免闪光。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM