Click to See Complete Forum and Search --> : [RESOLVED] MFC & Internet


Feoggou
September 11th, 2009, 07:32 PM
Hi!

I am trying to develop a SIMPLE application that should transfer some files to another computer (i.e. from China to UK). I have succeeded some sort of data transfer (using CSocket - a simple way from MFC), but it seems it works only on the local network. and I have absolutely no ideea how to make it work on a wide network.

Can somebody PLEASE help me? (if there is a way that you don't have to use the winsock API directly, but ... say it is a simpler method implemented by MFC, that would be great)

please help.

Feoggou
September 12th, 2009, 07:02 AM
the connection to the server returned an error...
I checked with GetLastError() and returned 10060, which is WSAETIMEDOUT.

ok... one thing I found out...
it seems like I used my address from the router (for which, the connection to the other computer connected on the same router worked). I now used my 'real' ip, which should be the one specified here: http://www.whatsmyip.org. now, I don't get 10060 error anymore, but 10061 (WSAECONNREFUSED), and I can't connect at all.

does anybody have an ideea what I can do?

cosminflorea
September 14th, 2009, 02:23 AM
Are you sure the server is running? You can use telnet to test if the server is listening.

Feoggou
September 14th, 2009, 07:20 AM
well... using netstat, I saw that the server is listening;
(by the way, I understood that I had to do port forwarding from the router... I did, I assigned 192.168.2.8 ip, TCP as type, port range 5150). And also, from Windows Firewall, on Advanced, having highlighted Local Area Connection, I pressed Settings, and then added a service, the same IP, port, and chose TCP - I understood that this must be also done).

using telnet:
I tried the 'ip' like this:
telnet 192.168.2.8 5150
the error returned was "Could not open connection to the host, on port 5150: Connect failed" - the port that my program uses for communication is 5150.
I also tried the same with 127.0.0.1 and 192.168.2.2 - in both cases, I think it worked: the command prompt cleared, having the window title Telnet 127.0.0.1, Telnet 192.168.2.2 respectively.

hoxsiew
September 14th, 2009, 10:16 AM
OK, since your address is 192.168.x.x then it is a private network and in order for a server on this network to be seen from the Internet, you must supply a NAT route on your gateway router. For testing, I'd disable the windows firewall altogether. The big question is: what is the IP address of the machine you are trying to route? Above it seems it could be 192.168.2.2 or 192.168.2.8. Is it dynamically assigned by a DHCP server? This might be part of the problem: it might be changing.

Feoggou
September 14th, 2009, 12:56 PM
"what is the IP address of the machine you are trying to route? " - i don't pretty understand what you mean...

my router assigned 192.168.2.2 to my computer. I tried to port forward using ip 192.168.2.2 - and I also tried another one (192.168.2.8). My router assigns ip-s between 192.168.2.2 and 192.168.2.5.

I believe that it is dynamically assigned, I don't know how to check.

hoxsiew
September 14th, 2009, 02:25 PM
Since it worked with 192.168.2.2 and 127.0.0.1, I'd say the server is actually listening on the proper port. The problem seems to be connecting from a different computer. Do you have a computer on the same network (192.168.2.x) that you can try to connect with.

Trying to connect from a computer outside your network (Internet) will require that you connect to your router's internet address on the port that you have mapped with NAT. This will be completely different from anything in the 192.168.2.x range. The router will forward requests to the machine with the server running on it.

This has gone beyond the scope of this forum. This seems to be a routing/NAT issue and not a TCP/IP programming issue. Read up on NAT here:

http://en.wikipedia.org/wiki/Network_address_translation

Feoggou
September 15th, 2009, 01:01 PM
it finally worked!

steps I did:

- I chose at port forwarding, the IP 192.168.2.2 - the one that was assigned to my computer, by the router; and port 5150.
- I have added, at Windows Firewall options a server with the above port and IP.
- I have REBOOTED the system (I realized that, before rebooting, but after the first 2 steps, at whatsmyip.org, it did not show my ip. but after rebooting, it showed).
- I connected the client to the server, at the ip address displayed in whatsmyip.org (my real ip)
- It worked!

I have some questions, if you can answer to me (please)... btw... I read that info from the link you gave... but didn't understand too many things...

- If I would have had 7 computers bound to the router. If the server program is on one of them, and the other 6 want to connect (using the client program), to they need my local ip (192.168.2.2) or they will all find it with the real one (displayed at whatsmyip.org)? - there are 2 computers bound to the router, of which, one is mine. The other succeeded to connect to mine using the global ip... but if there are more who want to connect bound to the same router?
- If there are 3 server programs, on three different computer bound to this router. Can external clients connect to the right ones using, of course, the global ip (the one displayed at whatsmyip.org)?
- if there are 3 servers on this local network, can the other 4 internal clients connect to them, and also other 3 external clients? (all to be connected in the same time)?
in every case it is considered that they all have the same port (5150).

btw... thanks for helping me :)

hoxsiew
September 15th, 2009, 01:55 PM
It's difficult to parse your questions (I know English is probably not your first language) but I think I get the gist of it.

This is basic TCP/IP routing. The computer has a routing table which determines where a packet goes once it is sent. Packets bound for the local network (192.168.2.xxx) simply go to the other machines on that network (in theory. in practice there can be "smart" switches within the local network which act as mini routers themselves). If an address doesn't match the local routing rules, it goes to the default route. This should be your router's local IP address (probably 192.168.2.1). The router then uses it's internal routing tables to figure out where it goes from there. The router will have (at least) two addresses. The internal address (the 192.168.2.1) and the external address (the one shown by whatsmyip.org--let's assume this is 74.125.45.100 for our purposes below; this is actually one of google's IPs). If the router doesn't know what to do with the packet, it simply forwards it to its default route and so on across the Internet.

When a client on your local network (let's say 192.168.2.3) attempts to connect to your server (on 192.168.2.2) then the windows internal routing table knows that they are on the same network and connects directly. If you tell the same client to connect to your external address (we're assuming 74.125.45.100) then there is an extra hop to the router, but the router has a NAT mapping back to 192.168.2.2 so it just bounces it back onto the local network. If a client anywhere else in the world tries to connect to 74.125.45.100 then there will be a series of hops along the internet through various routers until one of them "knows" about that address (probably your ISP's router) and it eventually gets sent to your router. Your router knows that packets bound for 74.125.45.100 on port 5150 need to be routed to 192.168.2.2 on port 5150 so it rewrites the header of the packet to show 192.168.2.2 and sends it back to your internal network. It also saves information about this packet so that it knows about conversations between the two endpoints and can rewrite headers on outgoing packets to make them appear to the client to come from 74.125.45.100. Unfortunately, you can only have one mapping per port so any other machines on 192.168.2.xxx that are running your server will never get packets from anywhere outside your local network.

Feoggou
September 15th, 2009, 05:46 PM
You have answered my questions.
Thanks for your help! :)