473,324 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

TCPListener and TCPClient not working over nat

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
Oct 3 '06 #1
3 6732
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.

On Wed, 04 Oct 2006 00:22:59 +0100, Jon Slaughter
<Jo***********@Hotmail.comwrote:
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



--
David Edwards
Developer of Hypereon, a Massively Multiplayer Online Game.
http://www.hypereon.net.
Oct 4 '06 #2

"Knossos" <kn*****@gmail.comwrote in message
news:op***************@artume.lan...
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.
On Wed, 04 Oct 2006 00:22:59 +0100, Jon Slaughter
<Jo***********@Hotmail.comwrote:
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



--
David Edwards
Developer of Hypereon, a Massively Multiplayer Online Game.
http://www.hypereon.net.
Oct 4 '06 #3
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.

On Wed, 04 Oct 2006 15:37:25 +0100, Jon Slaughter
<Jo***********@Hotmail.comwrote:
>
"Knossos" <kn*****@gmail.comwrote in message
news:op***************@artume.lan...
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.
On Wed, 04 Oct 2006 00:22:59 +0100, Jon Slaughter
<Jo***********@Hotmail.comwrote:
>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





--
David Edwards
Developer of Hypereon, a Massively Multiplayer Online Game.
http://www.hypereon.net.
Oct 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: zZ | last post by:
Hi All, I'm building a TCPListener as the server to accept TCPClient connections, using TCPListener.AcceptTcpClient. I'd like to retrieve client's IP after connected, but found no way to do...
2
by: Coder | last post by:
hi, i have a simple question; Suppose that followig code is a thread; /***************************************************/ listener = new TcpListener( IP, PORT); listener.Start(); ...
1
by: Benny Raymond | last post by:
Is it possible to get the ip address of the pc connecting to my TcpListener? Here's how my code works so far: listener = new TcpListener(_myIP, _port); listener.Start(); // block until we...
2
by: Dave Coate | last post by:
Hi, I am working on a client-server app. I can get two applications to talk to each other on the same machine using 127.0.0.1, but as soon as I try it using a computer name or actual IP address...
23
by: iwdu15 | last post by:
hi i have a working code for a tcpclient, but i cant seemt of figure out how to use a tcplistener to connect with it. i just want to clck a button and listen for a TCP conection and conect to a...
2
by: Terry Olsen | last post by:
Hoping someone can help me here. I've got this code written, and it works fine for the first connection. But if I connect another client (while the first is still connected), I get connected but...
5
by: Jerry Spence1 | last post by:
I have the following example of a tcpListener. I have a device external to my PC which is sending a TCP request on port 10002. This is working OK as I have a network sniffer on my PC and can see...
11
by: batmanfreeze | last post by:
I am using C# in .Net 1.1, and need to access the 'Client' Property of TcpClient, so I created a derived class to do this based upon the Microsoft Sample, located at:...
1
by: secutos | last post by:
When a TcpListener accepts a connection, it returns a TcpClient, which allows you to recieve data. But what about TcpListener.Server.Receive? Does that also allow to receive data in the same way? Or...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.