I have made a simple server running locally using tcplisten and it works fine over local network e.g. (127.0.0.开发者_JAVA技巧11:8888).
But how do I now receive connections to the server from the internet, what ip and port do I set??
Thanks
You can still use 8888. Just make sure that your firewall settings permit incoming connections to that port.
Assuming this is a windows machine, and assuming that it is a simple network (i.e. that your firewalls are forwarding connections from 8888 to the internal IP, or that the machine has an appropriate externally exposed interface) you should be able to connect via the public interface to your machine. If you are instancing your listener with a particular IP address (127.0.0.1) you may be preventing connections.
You can use IPAddress.Any or save a list of IPs in a configuration and load those from disk.
IPEnpoint e = new IPEndPoint(IPAddress.Any, 8888)
TcpListener l = new TcpListener(e);
There are a few things you need to do:
1) Log in to your Router and set up port forwarding:
- From a computer that is connected to the router...
- Find out your router's local network IP address and connect to that address in your web browser (common ip's are 10.0.1.1 or 10.0.1.254 or 192.168.1.1 or 192.168.1.254).
- Find the menu option in the router that allows you to set up port forwarding.
- Add a port to forward and enter your server's network IP address (should be close to the router's ip address) and desired port number.
- Save and close.
2) Add an exception to your firewall to allow TCP traffic from your desired port.
3) Find out the public IP address of your router (try http://www.myipaddress.com) and attempt to connect to your server using that address.
That should be all you need to do.
You may need to check whether you are behind some kind of NAT box. You can do this by checking your own ip from an internet site like whatismyip and comparing it to the ip of your machine.
You should also change your firewall rules to allow incoming connections.
PS : Web browsers by default connect to port 80. You can connect to different ports by mentioning port in the url like - http://www.example.com:8080/index.html .
You need to set a firewall rule up on your network to forward any traffic coming into your public IP on that port to be forwarded to the internal IP address of the machine running your server application.
Your server will need to run on machine with real-live IP. The server can bind itself to default IP or you can explicitly provide the IP address to bind to. Ports are not a problem as long as no other server application is listening on the same IP:Port.
精彩评论