开发者

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.

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)

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.

Is this possible?


Yes.

Connecting two computers on a LAN is the same as connecting over the Internet. The only differences are:

  • You will need to connect based on your external ip address instead of your local (try 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).

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). Hookup with either a public jabber server or setup your own Jabber host. Jabber-net is a library for .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. However, firewalls or a NAT-ed network could get in the way. You'll have to make sure at least one of you has a publicly accessible IP address and open port.

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.


You can use the .NET client and listener classes to connect to any computer on any IP address located anywhere on the planet. The catch is you will need to know the IP address of the other machine and it will need to accept connections from you.

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.

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. 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. 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.


If you have a website you can call a page on the website and log the IP address of the calling computer. In PHP you can do:

$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.

Job done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜