0

I am creating a server (desktop based) which listens on a port 4504 using this bit of code

                IPAddress[] AddressAr = null;
                String ServerHostName = "";

                try
                {
                    ServerHostName = Dns.GetHostName();
                    IPHostEntry ipEntry = Dns.GetHostByName(ServerHostName);
                    AddressAr = ipEntry.AddressList;
                }
                catch (Exception) { }

                if (AddressAr == null || AddressAr.Length < 1)
                {
                    return "Unable to get local address ... Error";
                }

                Listener_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                Listener_Socket.Bind(new IPEndPoint(AddressAr[0], Port));
                Listener_Socket.Listen(-1);

                Listener_Socket.BeginAccept(new AsyncCallback(EndAccept), Listener_Socket);

                return ("Listening On " + AddressAr[0].ToString() + ":" + Port + "... OK");

Now the problem is that, I want to run this on my server and the value of AddressAr[0] I want to be is the public IP of my server, but this snippet returns the local lan address of the server.

Like I want AddressAr[0] = "180.123.45.6" something [which is the public IP of my server], but with this snippet I am getting AddressAr[0] = "192.168.2.2"

PS: I am running this server as a desktop app and my trials were in the debugging mode.

I'd appreciate any help. Thank You.

3 Answers 3

1

You need to listen on your local host, which will be your 192.168.2.2 (this is correct) and then forward the external packets/traffic from your public address to your local machine.

If you're using, for instance, a linksys router, you can go to NAT/QOS and forward any incoming traffic on a specific port, e.g. 4504 to your local machine address 192.168.2.2

Any standard home router will have port forwarding built into the firmware.

Sign up to request clarification or add additional context in comments.

1 Comment

@Joy This is less of a programming question and more of a networking question, but as it relates to listening for traffic via code, it's at least educational.
1

When programing server, you should always listen local ip "127.0.0.1" and all your connections from outside redirect to your local ip address where server is installed.

Routing request from outside you should handle on your router. So basically that is it.

I hope this helps.

7 Comments

So, I can't expose my port on the server to the outside environment directly? Is the port forwarding on the router the only way to go? This server would be hosted on RackSpace cloud, so you mean I would have to ask rackspace guys to adjust the router to forward all the port request form public IP like 184.123.4.4:4502 to localhost:4502?
Yes its the only way. One way or another you need to setup this on your router. So basically what you need to do: Add port forwarding from publicip:port to your_computer_ip:port and that is all. If your server is on they by default transfer all trafic from your public IP to your server in theirs network, only thing that you need to ask them to open you port.
Opening a port just means that unblocking it in the firewall or there is more to that. I kinda tried to unblock from firewall from my local machine, but still when I use port scanner tools the desired port is blocked. Can you please help?
Yes, you need to add exception to your firewall on your local machine.
I actually did that and then even disabled my firewall. Yet when I scan using yougetsignal.com/tools/open-ports I see it to be blocked!
|
0

You can simply listen on any IP address that your machine has:

listenerSocket.Bind(new IPEndPoint(IPAddress.Any, port));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.