How to Access WCF Service Behind NAT - c#

Here is my situation :
I have a WCF with TCP Binding Service behind NAT and I am not able
to reach it.
I can reach my service using BasicHttpBinding and Raw TCP Sockets.
At server side port forwarding is done.
What may cause this ?
My Network Topology: Picture Link

I'm not experienced with WCF and nettcpbinding but this could help.
Theoretically it is not possible because your service has a local IP address, corresponding to the LAN where it is attached, and not a public IP address. So it could not be reached from the Internet, outside the NAT. However, almost all NAT devices, maybe your router/Access Point, have a feature for "Port Forwarding". This feature allows you to define that all packets addressed to a specific port are forwarded to a specific machine within you LAN, with a private IP address.
This could be a solution for you if you can overcome two hinders:
1-I don't know how the node in the Internet will send a message to the callback server. If it uses the IP address from which it received the request, there is not problem. If it receives a callback-IP address, then you have to figure out how to find out the public IP address of you NAT (maybe router/Access Point) and send out that IP address.
2-Your service behind a NAT needs to open a client port, or ephemeral port, to communicate with the service outside the NAT. If the second sends the message to the client port from which it received the message, you have to find a way to determine the client port you want to open (must be the same you configured in port forwarding). Will it listen in port 808 by default?
I hope this helps.

Check with URI "net.tcp://0.0.0.0:8000", this will ensure that the service will accept connection from any inbound IP address. At server side, the IP address mentioned in the URI & address from which the connection is accepted differs so the server is dropping the connection.

Related

Can you run a client application and a server application on the same host machine

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

Not communicate with client when client on other machine

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

RemoteEndPoint giving wrong IP address

We're using Fleck for our Websockets.
When we test our network program locally it all works fine. The process of a client connecting is:
Establish TCP connection
Send/receive infrequent TCP messages
Start sending/receiving UDP stream to server
The problem is on the live server which is far away geographically, clients can receive and send TCP messages, and they can send UDP messages, but they do not receive UDP messages.
We've investigated it as far as we can, and have found that the RemoteEndPoint property of the TCP connection on the server has the wrong IP address. It appears to be an IP address from our ISP. We use this IP address to attempt to send UDP messages back to the client. As it has the wrong IP, the client never receives these UDP messages.
The actual source IP address of the client must be known somewhere, as the TCP messages make it back OK. I've gone through the Fleck source and have printed out the RemoteEndPoint of the underlying System.Net.Socket but it keeps giving the wrong IP address.
Does anyone know what is going wrong and how we can expose the actual IP addresses of the clients?
The most likely reason is that your client does not have a public IP address, but a private address behind Network Address Translation (a very common setup). A common private addresses are of the form 10.X.X.X or 192.168.X.X.
NAT device replaces private address in a packet from your client with its IP address, which is public. For the outside world it seems that the traffic originates from the NAT device. The main advantage of NAT is that it allows many clients to use a single public IP address (IP addresses are sparse). But NAT introduces a problem: an incoming packet needs to be routed to a correct client, but it does not have a client IP address. Some other strategy needs to be used to recognize to which client a packet should be routed and such routing is not always possible.
For example, the most well known limitation of NAT is that you can't simply start a listening TCP server behind a NAT and then connect to it from the outside world. The reason is that NAT device has no idea that your server listens on a given port and thus, it has no way to known that TCP SYN packets from the outside world need to be passed to your client. A workaround for this is to explicitly configure the NAT device to route SYN packets directed to a given port to a specific client (port forwarding).
When a client behind a NAT initiates a TCP connection, a NAT device remembers state of the connection (client address, client port, remote address, remote port). Because of this, when a response arrives, the device knows to which client the response should be passed. When the connection is closed, the device discards state information. This is why your client can communicate over TCP.
But UDP is more difficult to handle because it is connectionless and stateless. When the device sees a UDP packet it does now known if a reply is expected and when, so not all NAT devices handle UDP traffic. Although there is a technique that allows to do it.
To summarize: the problem you are seeing is not C# specific. Setting IP address in packets from your server to the client IP address won't help, because it is most likely a private address that is not routable. To solve the problem you need to use a NAT device that can pass UDP traffic in both directions. But if you are developing a generic client to be used in many setups, you may want to reconsider if UDP is the best option. TCP connection forwarding is a basic feature that all NAT devices support, but some devices may not support UDP.

Networking in c#,not lan

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.

Socket Communication C#- IP Address

I have a socket application which I can use in local network, at home.
I can make them communicate for example from 192.168.x.x to 192.168.y.y ip addresses.
What should I do if I want to use the application over internet, from a remote machine, not local. For example which ip addresses should I use if my friend who lives another country wants to access my application.
On the server end, the easiest way is to bind to all available addresses by using IPAddress.Any as the address. You'll need to give the client your public Internet address to connect to. If you're being a NAT, it might involve looking at your router for the address (or using http://www.whatismyip.com/) and configuring it to route the traffic to your PC.
You need to set up your router to forward the port that you wish to communicate on. Once you have that in place, give your friend your public IP address.
For instance, you can configure your router rules to point all port 80 requests to your 192.168.x.x machine, so that when ever a request for port 80 comes in, it automatically gets sent to a specific address on your subnet.
Your outgoing IP address. Use this site to see it: http://www.whatismyip.com/
And of cause port forward your router.
You will have to use the IP address provided by you ISP (internet service provider). Usually these IP addresses are non static, so that you need to provide some way to resolve your dynamic IP address to a static name (dyndns providers do this usually).
In addition you need to configure you router to forward the incomming traffic on port xxx to your local machine (this is usually not your router, except when you are using a modem). This is called port forwarding.

Categories