TCP Problem
TCP Problem
TCP Problem
TCP Problem
TCP Problem
TCP Problem TCP Problem TCP Problem TCP Problem TCP Problem TCP Problem TCP Problem TCP Problem
TCP Problem TCP Problem
TCP Problem
Go Back  Xtreme Visual Basic Talk > > > TCP Problem


Reply
 
Thread Tools Display Modes
  #1  
Old 06-06-2011, 09:38 AM
sanjon sanjon is offline
Newcomer
 
Join Date: Nov 2010
Posts: 3
Default TCP Problem


Hello,
I created a program in vb.net, and managed to make it communicate with another program on my computer using TCPListener and TCPClient. I can send data fine and it all works perfectly. However, it only works on my own pc. As a IP I used 127.0.0.1 (localhost) in order to test it. But now I want the program to communicate with a program on a different pc. I have tried for a long time now, but I can't get it to work. I used this IP for connecting with the other computer: 192.168.1.101. But for some reason the program doesn't even connect. Please help.
Reply With Quote
  #2  
Old 06-06-2011, 10:42 AM
AtmaWeapon's Avatar
AtmaWeaponTCP Problem AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Anything in the 192.168.x.x range is a local address (I think 10.0.x.x is another local address range.) This IP is assigned to computers behind a NAT device. In other words, here's how it looks:
Code:
                            -------- 192.168.1.101
                            |
real IP ------ | router | --|------- 192.168.1.102
                            |
                            |------- 192.168.1.103
To the internet, all three machines behind the router look like they have whatever "real IP" is. When the router gets a packet from the real internet, it does some magic to figure out which of the 3 machines that packet should go to then sends it along the way. As far as the 3 machines know, that 192.168.x.x address is their real IP. But if a machine on the real internet tries to hit 192.168.x.x, they won't get one of those machines.

So if the machine you're trying to connect to is not behind the same router as the machine that is making the connection, there's no way a 192.168.x.x address is going to connect. You need to find the machine's real external IP. Sites like whatismyip.com help, VPN software can simulate being on the same network, and dynamic DNS is a solution. But for the most part this is solved by giving servers static IP addresses that never change.

If the machine is on the same network, odds are there is a firewall getting in the way. If you're using Windows Firewall, check its settings. You may need to add an exclusion to allow your application to make the connection. Both the server and the client may need their firewall settings updated.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 06-06-2011, 10:58 AM
sanjon sanjon is offline
Newcomer
 
Join Date: Nov 2010
Posts: 3
Default

Thanks a lot for your response. The computer I am trying to connect to is using the same router as me. So it should work fine with the IP address I am using. This means that it is something to do with the firewall. I only allowed the program to go through the firewall on the server's PC, not on my own. Maybe that's the problem. I'll try again and let you know. But just out of curiosity, how would you connect to a server that doesn't share your router? You would need to tell the TCPClient the IP of the router AND the IP of the person's computer. Or is there another way to do it? Again, many thanks.
Reply With Quote
  #4  
Old 06-06-2011, 11:30 AM
AtmaWeapon's Avatar
AtmaWeaponTCP Problem AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

You have to have the internet-facing IP of the remote host in order to connect. Usually that's their router, but the router may reject connections unless the other machine has made a request for you to connect to it.

The only sure-fire way to make a connection between two machines across the internet is to know their IPs. If NAT routers are involved (and they practically always are these days,) the best solution involves a middleman server with a static IP that negotiates the connection. This diagram pretends IP addresses are simple numbers for simplicity:
Code:
|client| ---- |router|------|server| ------- |router| ----- | you |
IP = 100      IP = ???      IP = 333         IP = ???       IP = 100
This diagram demonstrates that behind the router, it doesn't matter if two machines have the same IP. At the router, you don't usually have a reliable idea of an IP because most ISPs assign one through DHCP and it might change daily or more frequently. However, you can register a server with a static IP address that never changes. Once you and the client agree on a server, this is how you can get each others' IP:
  • Client sends some message to the server like, "I'm client 'bumblebee'."
  • The server will get the message from the router; since it comes from the router the router's IP will be revealed. The server remembers something like "Client 'bumblebee' is at IP 225."
  • The client starts listening on some port; we'll assume the router knows to let traffic through to that port.
  • You send a message to the server like, "I'm client 'queenbee' and I'd like to connect to 'bumblebee'."
  • The server gets your message and notes your router's IP, 582.
  • The server responds, "Roger, 'queenbee'. Client 'bumblebee' is last known at 225.
  • You attempt to connect to IP 225; since 'bumblebee' is listening the connection succeeds.
In some cases, the server is the one that makes the connections and acts as a true middle-man. You send a message to the server and say "send it to 'bumblebee'. The server sends it, bumblebee sends a response, then the server sends the response along to you.

This works because the server's IP can never change.

An alternative involves something called a dynamic DNS provider. When you set up dynamic DNS, you register your machine with a service that does lookup and get some URL like "bumblebee.somedns.com" that will point to the machine. You run software on the machine that periodically sends a message like "Hi, I'm bumblebee!" to the dynamic DNS service; they figure out your IP from the message (since it comes from the router) and update their records so the URL points to that IP. When you want to connect to bumblebee, you use the URL. This causes your machine to ask the DNS service for the IP, you get the IP, and everything's shiny.

Of course, it's not that easy. Most routers reject connections if they aren't expecting them; this is good for security. You usually have to tell the router "traffic on this port should go to this machine." Further, the machine might have a firewall that rejects traffic. You have to tell it to accept that traffic. It can be a pain in the butt to get it set up right.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 06-06-2011, 12:43 PM
sanjon sanjon is offline
Newcomer
 
Join Date: Nov 2010
Posts: 3
Default Thanks.

Thanks a lot for you in-depth explanation. I will definitely bookmark this thread for future reference. You helped me a lot. I am now able to connect the two computers. The issue was - just like you said - about the firewall. Anyways, I don't think I will try to connect with a computer outside my router network just yet. Sounds really complicated. But I really appreciate your time and effort .
Reply With Quote
Reply

Tags
client, listener, problem, tcp


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
TCP Problem
TCP Problem
TCP Problem TCP Problem
TCP Problem
TCP Problem
TCP Problem TCP Problem TCP Problem TCP Problem TCP Problem TCP Problem TCP Problem
TCP Problem
TCP Problem
 
TCP Problem
TCP Problem
 
-->