473,395 Members | 1,474 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,395 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 6744
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.