TCPListener and TCPClient not working over nat

J

Jon Slaughter

I can't seem to get these to work over my router. I have all the ports open
and ftp works from outside. Is there anything special I have to do or what?
(my app does work over the LAN though) I'm using the bare basics and the app
is very simple. I was just trying to see how hard it was to do.

// sends and recieves text.
private void button1_Click(object sender, EventArgs e)
{

richTextBox1.AppendText(UserName + ": " + richTextBox2.Text +
"\n");


string s1 = richTextBox2.Text;
w.Write(s1 + "\n");

string s = r.ReadString();

richTextBox1.AppendText("Client: " + s + "\n");
richTextBox2.Clear();



}

// Starts Server
private void button3_Click(object sender, EventArgs e)
{



richTextBox1.AppendText("Starting Server...\n");

listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 21);
listener.Start();

try
{
client = listener.AcceptTcpClient();
richTextBox1.AppendText("Client Connected...\n");
stream = client.GetStream();
w = new BinaryWriter(stream);
r = new BinaryReader(stream);


}
catch (Exception ex)
{
richTextBox1.AppendText("\n" + ex.ToString());
}

}

// Starts Client
private void button4_Click(object sender, EventArgs e)
{
string IP = numericUpDown1.Value.ToString() + "." +
numericUpDown2.Value.ToString() + "." + numericUpDown3.Value.ToString() +
"." + numericUpDown4.Value.ToString();

client = new TcpClient();
client.Connect(IPAddress.Parse(IP), 21);
stream = client.GetStream();
w = new BinaryWriter(stream);
r = new BinaryReader(stream);


}

}
}

(again, the code isn't ment to be a good implementation of a client/server
but just something very basic. basically a very crude chat program. No
asynchronous IO or anything like that).

Thanks,
Jon
 
K

Knossos

You could try a different port, try something higher up like 12321.

Make sure that port 12321 is forwarded to your computers local address.

If it works over the LAN then its not your code at fault, its your router.
Port 21 might be used by your router, so best to use higher numbers.

Hope that helps,
Dave.
 
J

Jon Slaughter

You could try a different port, try something higher up like 12321.

Make sure that port 12321 is forwarded to your computers local address.

If it works over the LAN then its not your code at fault, its your router.
Port 21 might be used by your router, so best to use higher numbers.

Hope that helps,
Dave.

I've tried. I also unblocked all my other ports too and tried port
forwarding(even though it shouldn't have mattered since it was unblocked
anyways.
 
K

Knossos

If you're running a router then you definately need to forward ports.

I'm not sure what you mean by unblocking, I guess you mean a firewall
right?

Basically, the scenario you have, is you have:

Someone-You-Dont-Know -> Internet -> Your Router -> { Computer1,
Computer2, ComputerN... }

You will have 1 person from the internet trying to connect to your
computer through your router, but how does your router know which PC to
send the connection to? Thats where NAT comes in, your router can assign
specific ports to specific computers.

For information on NAT: http://computer.howstuffworks.com/nat1.htm

If your router supports it, you could setup a DMZ (DeMilitarized Zone).
That would tell your router to send ALL port connections to whatever IP
you specify. After you've finished testing you should turn off the DMZ (it
represents a security risk).

Hope that helps,
Dave.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top