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

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.

Related

How to fire up all docker containers on a same local ip address in django?

I am writing a django based application with docker where there are 3 projects apps running in different containers. All django applications run at 0.0.0.0:8000.
But when I check the ip address of containers to browser the application in browser, they all run at different ip addresses:
project1 runs at 172.18.0.10:8000 can be accessed at: 172.18.0.10:8000/app1
project2 runs at 172.18.0.9:8000 can be accessed at: 172.18.0.9:8000/app2
project3 runs at 172.18.0.7:8000 can be accessed at: 172.18.0.7:8000/app3
which makes the hyperlinks of my app unusable. How do I run all the containers at one single ip, 'localhost:8000'?
Any suggestions where I am going wrong?
You are wrong in the design, mapping multiple containers to one ip+port is simply impossible. One port on one ip is always one application that listens, no matter if it is container application or not.
Simple prove: And who would then decide to which container to send the request? To all of them? Then who would decide which response is the correct one? That's what are ip addresses and ports for, to be able to send request to specific aplications on specific machines.
I think you should reconsider whatever you are doing, and do a bit more research on networking. There are several online courses on that. (I don't want to discourage you in any way, just aim you the right direction)
Simple solution without redesign you app, is putting in front of your app reverse proxy (e. g. nginx). That's the response to my rhetorical question. Reverse proxy can be a middle man that can decide to which application send the request based on something else then ip/port. Reverse proxy listens on some specific port and then by rules you provide to it (e. g. path based), can proxy the request to specific app/ip/port and proxy the response back.
But reverse proxy in this case is more a hack than proper solution, keep that in mind.

Accessing a webservice on my localhost

I've a running WebService published on "http://localhost:8080/FreeMeteoWS/FreeMeteoWS?WSDL". I want to access this webservice from a device on the same network...what address should I put in order to retrieve the wsdl?
If that port i.e. 8080 is open for incoming connections on your computer you'd only need to find your local ip-address, this is done in different manners on different operating systems. When you have obtained local ip-address switch localhost out in favor for that ip-address.

Client/Server setup only works when on same machine

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.

Making my TCP server Internet-facing - where should I start?

I have written a client/server code.
The server program executes on a terminal which plainly receives text data from the client and the client is a GUI where in you could specify the IP address of the machine where the server would be running.
However this works only in a closed Network(LAN).
I have just learnt TCP IP and have written a few codes that run on a LAN.
I wanted to make this program work across the network(over the internet).
But I have some basic doubts like,does one need the permission of the local ISP for such programs to execute across the internet.Does it involve buying a domain or some kind of permissions?
Can some one please help me on,what should I be doing,or where should I start from?
Listener have to have IP port opened in some way. If you are behind router, you should set proper port forwarding on router and if ISP provides its own subnet, you should know how to setup such link. (i do not know what kind of tehnology ISP might use for this).
For beginning you do not need you own domain name but you should be able to address by ip. If you need domain, register own domain name or create subdomain for free (i was using http://freedns.afraid.org/ )
If your server is behind a router which creates a LAN, you have to configure the router that it will forward the packages from your client to the server.
You have to forward all the incoming packages at the specific port to the local ip of the server.

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).