Client/Server setup only works when on same machine - c++

I have two applications. One is the server app and the other is the client app. The server app listens for connections and then feeds messages to all connected clients.
So far all my testing has been done by running both applications on the same machine, and this is working fine. Now though I am trying to test the server running on one machine and the client running on a different machine.
Each of these machines are within my own home network. That network is behind a router with a firewall. From what I have read, my problem might have to do with port forwarding not setup on my router. Does that sound likely?
Is this something I can fix with just my code? Do I really have to require all my users to manually open the port on their firewalls? I have used plenty of other commercial applications that connect to servers and don't require me to open ports on my firewall. What is different about those applications and mine?
Then again, maybe I am barking up the wrong tree here. Maybe there is some other problem preventing my code from working in this situation?
I can provide any code snippets that might help, but I am unsure as to where to start looking and I don't want to just post my entire solution :)
Thank you in advance

You need to bind to 0.0.0.0 (simplified version) in order to accept connections not coming from the local machine. Have you checked that you don't bind to 127.0.0.1?
When you bind to 0.0.0.0 you actually bind to all current and future addresses of the current host and so will be able to accept connections from everywhere, you can also bind to just the address of one of the network interfaces.

Related

Connection to server while behind the rounter in WinSock2, C++

I'm writing a very simple server-client application in C++, using WinSocks.
When I set client's parameters as: resolveHost ("google.pl") for server's IP and 80 for port, I receive a html (with GET header).
When I run the server first (with port 10000) and then client (port 10000 IP inet_addr("127.0.0.1") it seems to works (I receive something).
But when I change the server's IP in client to inet_addr("188.246.158.171") (my IP for now, I don't have static IP, I've checked my actual IP with some website), it for sure DOES NOT works.
My computer is behind the router - can it be the problem?
If so, can I solve it in the way that don't requite any configuration to router (port forwarding etc.). Also, I don't want to make anything router's model-specific, so I intentionally haven't posted the model of my router ;)
Why I don't want to change configuration?
Because my application will be for "simple people". And simple people won't try to make configuration, and I won't require them to do so.
Also, many applications (on-line games for example) works fine with my router without any configuration - my application cannot be exception (by the example of other applications on my computer, I know it's possible and common to achieve it).
I use Visual Studio 2012, works on Windows 7 x64.
If you run a listening server behind a router, you must configure port forwarding rules on the router if you want outside clients to reach the server through the router. You cannot avoid that. However, if the router supports uPnP (Universal Plug and Play), your server code can configure port forwarding rules programmably, such as with Microsoft's IUPnPNAT interface. Otherwise, you have to use the router's own configuration software (usually an HTTP interface running on the router itself).
You also have to configure ports/permissions on a local firewall, if installed. Some firewalls have APIs for that, such as Microsoft's Firewall.
Online games and other peer-to-peer apps employ several NAT Traversal techniques to get around NATs, firewalls, etc. Hole Punching, Role Reversal, etc. Do some searches, information is readily available.

what is need for localhost in socket programming as well as general application?

Localhost follows the loopback mechanism.
why we have to loop back the packets to our computer itself? what is the need for that(general case and specially socket programming)?
Also kindly specify some practical applications of localhost too?
And another clarfication i need was
localhost resolves to 127.0.0.1 (most time)
myhost name say "vinoth-computer" resolves to 192.168.111.12
is 127.0.0.1 and 192.168.111.12 one and same?
Think about next situation: you have a client and server application running on separate stations in production. But in QA or for unit testing you want to run the client and the server instances on the same station. You can put in client definitions or parameters address of server as 'localhost' or '127.0.0.1'.
Also, sometimes you want to run 2 separate processes on the same station, when by design they should be running on the same station. You can set a communication between them through sockets and use localhost on the client side part.
Local loop back can be used for communication applications with each other. There is a lot ways to do this, but this is one of simplest.
To specify application, great example is Apache server, which by default listen on localhost as well. So when you are developing web application, you can simply use localhost or 127.0.0.1 as address in your favorite browser.
192.168.111.12 is not same as 127.0.0.1
In your case its IP which refer to your computer in your local network (behind some router). Other computers in your network can address you computer using this address.
If you want to know more, or explain something in more detail, feel free to ask.

Accessing a Jetty server from an external machine

I have made a simple Jetty server using this tutorial here, http://www.seas.upenn.edu/~cis550/jetty.html
Now that I have the initial steps completed, I can succesfully connect to my server by entering localhost:8080/hello or 127.0.0.1:8080/hello in my browser. Everything is going great up until this point. The servlet is working properly.
I am now interested in connecting to the server using "Machine B". I want to build a server that can be connected by any browser, anywhere.
My IP is XX.173.XXX.216, parts hidden for obvious privacy reasons. On my "Machine B" I try and enter XX.173.XXX.216:8080/hello into the browser URL. The website cannot be found. From experience I questioned my port mapping and opened my 8080 port.
After a few google searches and a while of reading, I give up. Does anyone know a possible solution to my problem? Are there any further steps I need to do in the setup of the Jetty server to make it accessible from external machines?
It is usually not hard getting from the inside of a firewall and out. It is much trickier to get from the outside and in (just have a look at the hoops Skype jumps through to be able to get two computers each behind a firewall to talk to each other directly).
First of all you need to ensure that Jetty listens to the right network devices.
Identify the IP-number of the computer running Jetty (not 127.0.0.1) and see if you can connect to "http://that-ip-number:8080" from the machine itself. If this doesn't work, fix your jetty configuration. Then put another computer on the same network and connect to "http://that-ip-number:8080" too. This MUST work before you go any further. If this doesn't work, fix your firewall configuration on the computer running Jetty.
When the above works,then you can start thinking about exposing the port through your router. The router most likely has a web interface where you can configure this, but the easiest way to do that is to use a uPNP tool (if your router supports that) which can redirect an external port to a specific host and port on your internal network. I use "Port Map" under OS X which has a very simple interface.
Good luck

Bind to 127.0.0.2

I'm running a client/server application locally on my Windows XP PC and for testing purposes I want to run multiple clients.
The server has a configuration file containing the IP addresses of the clients that can connect; in the real world, these would all be on separate hosts with separate IP addresses.
Currently I am able to test locally with a single client which binds to 127.0.0.1 however because I can only have one client-IP mapping in the server configuration (that's how the system works and can't be redesigned!) I can only run one client on my development PC.
I've tried to start another client application bound to 127.0.0.2 connecting to the server which is bound to 0.0.0.0 however the server thinks that the client is connecting from 127.0.0.1 again and so rejects what it believes is a second connection from the first client.
Can anyone suggest a way to get around this problem? I believe I could run one more client bound to the external IP address of the PC but I'd really like to be able to run multiple.
I know I could use VirtualBox or similar to run new instances but I'd like all of the client applications to be running in the Visual Studio debugger.
Any help greatly appreciated!
Nick.
PS. Not sure if it matters but the applications are written in C++ using standard winsock sockets.
You might be able to create more loopback interfaces. See the chosen answer to How do you create a virtual network interface on Windows?
AFAIK Windows 7 (maybe Vista too) lets you add multiple IP addresses to a single interface (card).

C/C++ detect network type

I need to write a win32 c/c++ application which will be able to determine whether the PC it's running on is connected to one of 2 networks. The first network is the company LAN (which has no internet connection) and the second network is a standalone switch with a single PC connected to it (the PC that the program is running on).
I'm pretty new to network programming but so far I have tried testing to see if a network drive which is held on our LAN can be mapped. This works fine if the PC is connected to the LAN, the drive mapping succeeds so so LAN detection is successful. However, if the PC is connected to the switch, this results in a VERY long timeout which is not a suitable as it will delay the program so much as to make it unusable.
Does anyone have any alternative suggestions?
I'm using c/c++ in VS 6.0
[Update]
Whilst trying a few different ideas and looking at some of the suggestions below I thought I should update with some additional information as many (if not all) of the suggestions I don't think will work.
(1) The aforementioned LAN has no external connections at all, it is completely isolated so no resolving of external DNS or pinging websites is possible.
(2) Hostname, MAC address, IP, Default Gateway, Subnet etc etc (basically everything you see in ipconfig -all) are all manually configured (not dynamic from the router) so checking any of these settings will return the same whether connected to the LAN or the switch.
(3) Due to point (2), any attempts to communicate with the switch seem to be unsuccessful, in fact almost all networking commands (ping, arp etc) seem to fail - I think due to the machine trying to connect to the LAN when it isn't there :-(
One thing I have found which works is pinging the default gateway IP which times out when connected to the switch. This is sort of ok as I can reduce the timeout of ping so it doesn't just hang for ages but it feels like a bit of a hack and I would certainly appreciate any better solutions.
Thanks
As far as TCP/IP is concerned there is no such thing as a LAN on WAN. There are a set of non-internet routable addresses like 192.168.x.x and 10.x.x.x but these are sometimes used by ISP short of IP addresses.
You best bet is to use Asynchronous APIs when making TCP/IP connections. WIN32 defines a whole buch of OVERLAPPED APIs for this purpose. This will prevent your application from grinding to a halt while waiting for a remote connection.
Alternatively put the socket stuff into another thread and then only notify the UI when the operation is done.
I would first try to differentiate between the two using information available locally--that is, from your computer. Does the output of ipconfig /all differ depending on which network you're connected to? If so, exploit that difference if you can.
Is it possible to get the MAC address of the standalone switch? Of the switch that controls the company LAN? That would be a sure way to tell. Unless somebody cloned the MAC address.
If you try using the existence or non-existence of some network service to determine which network you're connected to, you can never be sure. For example, if you failed to map that network drive, all you know is that the network drive isn't available. You can't say for certain that you're not connected to the company LAN. Same is true if you use ping. Lack of response from a particular machine means only that the machine didn't respond.
Various things you can look at for differentiation:
DNS domain name (GetComputerNameEx)
MAC address of gateway (ping it, then GetIpNetTable)
Routing table(do you have a gateway and default route on the company LAN)
WNet discovered network resources (WNetOpenEnum, WNetEnumResource)
Ability to resolve external hostnames (try a 5-10 names like www.google.com, www.microsoft.com and so on, if one resolves you should have internet)
You'll have to decide how many indicators are "enough" to decide you're on one or the other LAN though if tests fail. Then keep retrying until you have a definite result.
http://msdn.microsoft.com/en-us/library/aa366071%28v=VS.85%29.aspx has a lot of network related functions that you can experiment with to create further indicators.