C# and networking

  • Thread starter Thread starter Hans Kamp
  • Start date Start date
H

Hans Kamp

I found a useful client/server combination for newbies at
http://www.msdnaa.net/Resources/Display.aspx?ResID=1375

I typed both of the programs and I ran them on one of my 2 computers (I have
a desktop and a laptop PC).

Then I copied the client program to the laptop, connected the laptop to my
PC, and I changed the following line:

TcpClient tcpClient = new TcpClient ("localhost", 4444);

to:

TcpClient tcpClient = new TcpClient(myIP, 4444);

(in which myIP is the IP address of my computer, that is displayed if you
typed "ipconfig" at the command prompt).

This all worked fine! I had to disconnect my computer from the Internet
(since I have a cable modem).

Now my question. Imagine the following situation:
- A friend of mine runs the server side program on his computer and gives me
his IP address;
- Both he and I connect our computers to the Internet (both he and I have
cable modems);
- I change the above statement (in the cliend side program) to:

TcpClient tcpClient = new TcpClient(MyFriendsIP, 4444);

- I run the client side program.

Will the communication still work?

I am still a network newbie but my first few steps were successful.

Will this also work with a router and two computers connected to it?

Hans Kamp.
 
inline
Hans Kamp said:
I found a useful client/server combination for newbies at
http://www.msdnaa.net/Resources/Display.aspx?ResID=1375

I typed both of the programs and I ran them on one of my 2 computers (I have
a desktop and a laptop PC).

Then I copied the client program to the laptop, connected the laptop to my
PC, and I changed the following line:

TcpClient tcpClient = new TcpClient ("localhost", 4444);

to:

TcpClient tcpClient = new TcpClient(myIP, 4444);

(in which myIP is the IP address of my computer, that is displayed if you
typed "ipconfig" at the command prompt).

This all worked fine! I had to disconnect my computer from the Internet
(since I have a cable modem).

I don't see why you had to disconnect from the internet...
Now my question. Imagine the following situation:
- A friend of mine runs the server side program on his computer and gives me
his IP address;
- Both he and I connect our computers to the Internet (both he and I have
cable modems);
- I change the above statement (in the cliend side program) to:

TcpClient tcpClient = new TcpClient(MyFriendsIP, 4444);

- I run the client side program.

Will the communication still work?

Assuming there are no interdicting problems, such as firewalls, it should.
I am still a network newbie but my first few steps were successful.

Will this also work with a router and two computers connected to it?

It should on a cable\dsl router. But, depending on the router, it is
possible it won't (sorry to be complex, but it is a area with alot of
maybe's).
 
Daniel O'Connell said:
inline



It should on a cable\dsl router. But, depending on the router, it is
possible it won't (sorry to be complex, but it is a area with alot of
maybe's).

Just make sure the router that the server is behind has port 4444 (or
whatever you are using) open and pointed to your server pc. The client's
router shouldn't need any special setup at all (since outgoing connections
are generally enabled, unless your friend setup some sort of special
firewall filtering, or is working behind a corporate firewall).
 
Michael Mayer said:
Just make sure the router that the server is behind has port 4444 (or
whatever you are using) open and pointed to your server pc. The client's
router shouldn't need any special setup at all (since outgoing connections
are generally enabled, unless your friend setup some sort of special
firewall filtering, or is working behind a corporate firewall).

In that latter case, how does the outside world (or my friend) know the IP
address of the server computer?

Hans Kamp.
 
Hans Kamp said:
Just make sure the router that the server is behind has port 4444 (or

In that latter case, how does the outside world (or my friend) know the IP
address of the server computer?

I'm assuming that by router you mean a (~$100) box by Linksys, Netgear, SMC,
etc that is doing internet sharing through NAT (network address
translation). In this case, your server computer's IP address never shows
up on the internet. Instead, you want the IP address of the router itself.

Visit this page from your server:
http://www.whatismyip.com/

You'll find out that the ip address they report is most likely not the one
your server thinks it is (your server probably has a private domain address
such as 10.x.x.x or 192.168.x.x ). What's happenning is the router is
translating the private ip addresses from your server and laptop (and any
other computers behind the router) into one public ip address that was given
to you by your ISP (cable modem company, DSL provider, etc). The address
reported above (by whatismyip.com) is the one your friend needs. This is
the one that your router responds to, and then (assuming you've forwarded
the necessary port) the router will forward the connection to your server.

Note, if you have a dynamic address (changes periodically, like every day,
every week, etc) you might want to look into a dynamic dns provider such as
www.changeip.com ( i use these guys). You can setup a domain name to always
point to the latest IP address you got. I use them with my domain. They
have some free domains you can use, as well.
 
Michael Mayer said:
I'm assuming that by router you mean a (~$100) box by Linksys, Netgear, SMC,
etc that is doing internet sharing through NAT (network address
translation). In this case, your server computer's IP address never shows
up on the internet. Instead, you want the IP address of the router itself.

Visit this page from your server:
http://www.whatismyip.com/

This URL gives the same result on both computers.
You'll find out that the ip address they report is most likely not the one
your server thinks it is (your server probably has a private domain address
such as 10.x.x.x or 192.168.x.x ). What's happenning is the router is
translating the private ip addresses from your server and laptop (and any
other computers behind the router) into one public ip address that was given
to you by your ISP (cable modem company, DSL provider, etc). The address
reported above (by whatismyip.com) is the one your friend needs. This is
the one that your router responds to, and then (assuming you've forwarded
the necessary port) the router will forward the connection to your server.

Yes, but it isn't clear what is the right PC, when both my laptop and
my PC show the same address.

Hans Kamp.
 
Hans Kamp said:
"Michael Mayer" <[email protected]> wrote in message

This URL gives the same result on both computers.
server.

Yes, but it isn't clear what is the right PC, when both my laptop and
my PC show the same address.

I found a solution in a different way. I can configure my router by using
"Virtual Servers" (with the Sitecom DC-202 that is
http://192.168.0.1/vs.htm) in which I can type the ports that must be
forwarded and the computer (in the format <ComputerName> (<IP>)
<KindOfConnection> in the combobox). In my case I must give a new name for
my program, giving 4444 as the Internal Port No. and as the External Port
No.

It should be both possible for servers like WinMX, a webserver, a telnet
server, an e-mail and a FTP server, and for the little servers you write
with C#.

Hans Kamp.


Hans Kamp.
 
Yes, it will if they are both behind the same router. It's giving the ip
address of the router, not actually you're computer. (Type ipconfig ad dos
prompt to see what your computer's actual IP address is)
I found a solution in a different way. I can configure my router by using
"Virtual Servers" (with the Sitecom DC-202 that is
http://192.168.0.1/vs.htm) in which I can type the ports that must be
forwarded and the computer (in the format <ComputerName> (<IP>)
<KindOfConnection> in the combobox). In my case I must give a new name for
my program, giving 4444 as the Internal Port No. and as the External Port
No.

It should be both possible for servers like WinMX, a webserver, a telnet
server, an e-mail and a FTP server, and for the little servers you write
with C#.

Exactly. That's what I meant - sorry if I wasn't clear. Glad you got it
going.
 
Back
Top