I have a piece of code in a service which listens to a port and then logs the IP addresses using the HttpListenerContext.Request.RemoteEndPoint
I have noticed, that in one machine "A" when the remote endpoint is also on the same machine A, HttpListenerContext.Request.RemoteEndPoint returns the IP address of the machine A i.e something like 192.168.20.12
Where as when the same piece of code is run on a different machine B, with the remote endpoint also in the same machine B, the HttpListenerContext.Request.RemoteEndPoint returns 127.0.0.1
Does anybody know why there is a difference ?
For my application, I would like to have the proper IP address(192.168.20.12) and not the loopback address. How can one go about getting this information ?
More info : Machine A is a physical desktop running windows 7. Machine B is an EC2 instance running windows server 2012.
The problem isn't that HttpListenerContext.Request.RemoteEndPoint is returning the wrong IP address... the issue is that whatever is connecting to the port is connecting to the loopback address and your listener is correctly reporting that. Assuming that you actually want to use your listener to monitor connections from a remote machine, this is really an immaterial concern (since you can't connect to the loopback address from a remote machine). Likewise, if a machine connecting to your port has multiple IP addresses, you aren't going to be able to choose which one HttpListenerContext.Request.RemoteEndPoint returns. It should return whichever IP address the remote machine selected to connect to your server.
Related
I have a synchronous TCP server and client application the works absolutely fine on two separate host machines.
What I'd like to know is what IP and port do I bind the server socket and the client socket to when the applications are both running on the same host machine.
I can't find any solid information on Google about this.
When I try and use my network IP which was 192.168.0.32 I get an error that says the Host actively refused the connection.
I cannot find any reasonable information about this error.
Can I listen and send on the same Port?
What IP address should I use to bind the server and the client, when both applications are running on the same machine?
Thanks for your time.
In order to run both client and server applications on the same host you should bind your server socket to localhost (you can actually write "localhost" it's a preserved word or 127.0.0.1 ) and address it from the client as well.
Localhost allways refers to the computer you work on.
If you'd like to access your server from a machine which is outer to your local network using your network ip you've mentioned, you should first search for "IP FORWARDING" option in your router settings and forward incomming requests to the machine where the server is running on.
Or (my favourite) use the great IP TUNNELING service of ngrok. You can find it here https://ngrok.com/
good luck.
So the answer to this question is that I must bind to my loop back address with separate ports for the client and the server !!
The IP address could be the loopback 127.0.0.1 for both, or your IP address, I don't see why it would not work.
The port on the other hand has to be the same for it to work, assuming the client application doesn't also listens to the port that you "bind" it to.
You have to tell the server on which port it should listen. The client then has to send data on the same port for the server to get the information.
This example should get you going: https://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C
I make a TCP/IP server in C# and client too. TCP-server is broadcasting packets to clients.
But it only broadcast on local IP not on other computers/machines.
All computers on connected SN MP server. I also changed IP address in code from (127.0.0.0) to network IP address (SNMP server IP Address). I pinged others IP with my computer; it's working but not making a connection with my TCP-server that I have made in C#.
Can you help me in this scenario?
Can't give a proper answer without seeing some code, but for starters, you can check whether the port is blocked in firewall or something (try to allow incoming connections in firewall for the specific port (whitelist it) )
The Tcp client/server code I'm working with is here: client and server.
As the title goes, I'm running the server code on my computer and I want to connect to it from another device using the client code. My question is, to what ip address does my client code have to connect? I know there are many related posts, but I'm only getting information on what's wrong rather than the solution.
This is a common problem when developing client/server applications. In a typical home network, there are multiple local IP addresses and a single external IP address. All devices communicating with your network from the outside must use the external IP address. However, when writing client/server applications, if you simply input the external IP address as the address to connect to, you'll quickly discover this won't work.
You need to use Port Forwarding. The client and server will be communicating over a specific port, and Port Forwarding is how your router knows which local IP address to send data to when the client is connecting to the external IP address. You want to login to your router settings, navigate to the section regarding Port Forwarding, and specify that communication over the port you're using in your server should be redirected to the local IP address that your server is running on. Exactly how to change these settings on your router depends on which router you're using.
Run the ipconfig command in a Windows Command Prompt on the machine running your server. Obtain the local IPv4 address from the results. This is the address to use when Port Forwarding the port used by your client/server applications. Adjust your router settings accordingly, and then your client should be able to use your external IP address just fine.
To find your external IP address, any website such as http://www.whatsmyip.org/ should work fine.
If you want to avoid all of these problems for now and simply test your application on your home network, then use the local IPv4 address found when running the ipconfig command on the machine your server is running on. Note that this will only work if both the client and server are running on the same network.
I have server running (on localhost currently) and when I connect to the server (using one of 3 different user accounts) I want to get the IP address from the servers viewpoint.
So lets say I have the server running on the same machine the IP should be 127.0.0.1
If the server is running in a intranet the ip would probably some local ip like 10.10.10.***
And then ofcourse there is a external address if you are connected from another location
Also more than one computer will be able to connect with the same user credentials so I don't know how I could get get the correct ip from mysql.user when there will be multiple ips per username.
I'm newbie in WCF, so I really need your help.
I have two programs which uses WCF.
If they are running on one computer and client uses adress net.tcp:\Localhost:8001\MyService to connect to server(which has adress 192.168.1.1 for example) everything is OK.
But when I'm changing adress for client to net.tcp:\192.168.1.1:8001\MyService I'm getting system.timeoutexception because client cannot connect server.
Thanks for help.
You may need to allow external access to the port in the firewall.
As long as you're communication from localhost to localhost, the firewall doesn't come into play, as these requests are handled by the loopback adapter.
But when communicating from localhost to the machines IP address, you're making a network request that's handled by the firewall (even though you're still on the same machine physically).
By the way: For the server side it doesn't matter whether you start the WCF service with endpoint localhost or 192.168.1.1.
EDIT
I wrote that it doesn't matter whether you listen on localhost or 192.168.1.1 - this is only true if you only have one network adapter available.
As soon as there are two or more network cards (for example: Wireless LAN is turned on and you're connected via cable), localhost or 0.0.0.0 will make the service listen on any adapter. Using 192.168.1.1 will make the service listen only for connections on that IP address.
This is important to know especially in cases where the different network adapters become members of different (sub)networks.
For example: One adapter is connected to the 192.168.1 network and the other adapter is connected to the 192.168.2 network. If your service listens on localhost or 0.0.0.0 it will be reachable from both networks. If it only listens on 192.168.1.1, it will not be available for the 192.168.2 network.
Please check "192.168.1.1" is included in your "hosts" file. And give it a try.
The hosts file can be found in "C:\Windows\System32\drivers\etc"