Reputation: 909
I'm making a multiplayer game in XNA 4.0, using the .NET socket classes.
Since I had never programmed sockets before, I wrote a little client/server program, and tested it locally (client and server on two different computers) and they worked just fine.
However, when I tried to connect between two different networks (The server was at my house and the client was running at my friend's house who lives 5 minutes away) the client couldn't connect.
I used the Socket.BeginConnect() method, and .NET didn't throw any exceptions (If the target computer refused the connection, then .NET would have thrown a SocketException) There were no firewalls on either computer.
Any idea why this happened?
Here's a snippet of my code:
public void connect(String ipAddress, int port)
{
lock(_locker)
{
if (!_connecting)
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);
try
{
_socket.BeginConnect(endpoint, doConnect, null);
_connecting = true;
} catch (SocketException e)
{
// what happen? SOMEONE SET US UP THE BOMB!
}
}
}
}
Upvotes: 0
Views: 964
Reputation: 13925
If your friend connects to the Net through a router, the router is almost certainly blocking all unsolicited incoming traffic. That is how NAT routers work.
To get the router to accept incoming connections to a given port and pass them on to one of the machines attached to the router, your friend will have to set up port forwarding.
This site provides instructions on how to do this and covers a large number of router manufacturers and models.
Upvotes: 2