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"
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 have a server program and a client program. While developing the program I run the server and the client on the same machine for convenience. The server starts listening to incoming connections using these lines:
var listener = new TcpListener(IPAddress.Any, 7070);
listener.Start();
The client connects to the server using these lines (simplified):
var client = new TcpClient(AddressFamily.InterNetwork);
client.Connect(IPAddress.Loopback, 7070);
I use IPAddress.Loopback because I run the programs on the same machine. But, knowing that the server and the client won't be necessarily run on the same machine in the future, I changed it to my public IP from http://icanhazip.com (IPAddress.Parse(...)). Because of that the client was unable to connect to the server on the same machine with the exception No connection could be made because the target machine actively refused it <my public ip:7070>.
I tried disabling my firewall but it's still not working. Why is the server refusing the connection? Didn't I specifically tell it to listen to all interfaces with IPAddress.Any?
Why does that happen and how do I fix it?
Here's an answer built from my comments on the question, which are hopefully correct:
Your public IP is provided by your ISP and is actually the address of your router. The router does network address translation (NAT) for outgoing connections from computers within your local network. These requests look to the Internet like they're all coming from one IP, and your router sends responses to the right local computer based on an address translation table. This works for outgoing connections but not incoming connections.
If something tries to open a TCP connection from the Internet to your router, the router has no idea what local computer it might be trying to connect to unless you specifically configure it to forward that traffic to a particular computer on your local network. That's where port forwarding comes in. If you haven't configured port forwarding, the router just says, "sorry, I'm not handling incoming requests on port 7070."
Is your development machine behind a router?
Network traffic sent to you via your public IP address reaches your router on a given port via a specific networking protocol. Your router needs to know where to send this traffic internally on your network. Traffic is coming from the Internet to your machine, and your router either cannot or will not forward the traffic to your computer's machine.
You don't notice this in your day-to-day life thanks to the power of Network Address Translation (NAT) and Universal Plug and Play (UPnP). Glossing over some details here, Network Address Translation allows traffic headers to be modified to route traffic from your public IP to your actual machine's IP on the network. When incoming traffic attempts to open a port for connectivity on your network, the router needs to be configured to forward that traffic appropriately. Universal Plug and Play is a protocol supported on many modern routers to allow software and devices to seamlessly route traffic without the need to forward ports.
This leaves you with two options:
For development purposes, access your router and forward the desired port to your development machine
For a more robust application, especially if you're going to be running this on different machines or different networks, consider adding UPnP support to your application while also understanding that UPnP may not be supported or enabled by some users, in which case port forwarding is still necessary.
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 C# TCP Server program which is running into VMWare and network type is NAT. There TCP server works as a http server, so that I can connect it using browser. Now when I am connecting it using this url http://localhost:33333/OrderDetails, it works without any problem. But when I am trying to connect it from outside of VMWare using this link http://1.39.37.243:33333/OrderDetails (1.39.37.243 is my PC's public ip), then its not working. I can't change the network type NAT, because my customer's office Network system is also NAT system. Where am I doing it wrong ? Any thoughts or suggestion will be highly appreciated.
This isn't working because the machine receiving the TCP packets has no reason to forward them to the VM. In other words, this isn't working because you haven't done anything to make it work.
You need to configure VMWare's NAT to forward these incoming TCP connections to the VM.
You could also have other problems:
The server might not be listening on the right IP address. For example, if the listening socket is bound to localhost, only localhost connections will work.
There might be something else in the path keeping the TCP packets from getting to the host. For example, if the host itself is behind NAT, port forwarding may need to be configured in the router.
Port forwarding won't work from inside the LAN unless the router supports hairpinning. If the host itself is behind NAT, test from outside the LAN after setting up port forwarding in the router.
I wanted to learn how networking in c# works, so I learned how to use TCP server and clients.
The only problem is that it's working only if both computers are connected to the same network..
How can I make them communicate even if they aren't?
TCP/IP sockets should work between any two end points as long as there is a route between them. If there is no route between them then you are talking about a case where there are two separate disconnected networks. In that case you will need something to bridge the two networks.
If you are using TCP/IP server/client communication and the computers are on different networks that has a route connecting them and they cannot communicate then you should look at firewall settings and other network settings to make sure TCP/IP packets from one network are able to reach the other network.
Make sure you are using the correct IP address when the client tries to connect to the server. If you have a server at IP address 10.0.0.5 listening on port 4823 try to telnet to that IP address from the client using the server IP address 10.0.0.5 and port 4823. If it connects that usually means that you have things set up right.
From a command prompt: telnet 10.0.0.5 4823
Communication in TCP is done with IP addresses. So even if the client and the server are not on the same network if you specify the IP address of the server, the client will be able to communicate with it (assuming of course the network that the client resides on is configured properly and knows how to reach the server's network). You could also use the DNS service and provide the FQDN of the server instead of an IP address. The DNS server on the client network will resolve the server's FQDN to an IP address.