Talk2Tom11 Posted January 28, 2005 Posted January 28, 2005 I am writing a client/server application. In the server part it looks up for an IP address. The only problem is that it looks for the local IP. Which with a local IP, if a user wants to access the server froma client that is not within that network then it will not be able to log in. I have inserted the code below that I used to find the IP for the server, but for the local IP. If anyone knows how to find the Static IP please post. :) // Determine the IPAddress of this machine IPAddress [] aryLocalAddr = null; String strHostName = ""; try { // NOTE: DNS lookups are nice and all but quite time consuming. strHostName = Dns.GetHostName(); IPHostEntry ipEntry = Dns.GetHostByName( strHostName ); aryLocalAddr = ipEntry.AddressList; } catch( Exception ex ) { Console.WriteLine ("Error trying to get local address {0} ", ex.Message ); } // Verify we got an IP address. Tell the user if we did if( aryLocalAddr == null || aryLocalAddr.Length < 1 ) { Console.WriteLine( "Unable to get local address" ); return; } Console.WriteLine( "Listening on : [{0}] {1}:{2}", strHostName, aryLocalAddr[0], nPortListen ); // Create the listener socket in this machines IP address Socket listener = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); listener.Bind( new IPEndPoint( aryLocalAddr[0], 399 ) ); //listener.Bind( new IPEndPoint( IPAddress.Loopback, 399 ) ); // For use with localhost 127.0.0.1 listener.Listen( 10 ); Quote
*Gurus* Derek Stone Posted January 30, 2005 *Gurus* Posted January 30, 2005 Assuming that by "local IP" you mean the IP address assigned to the computer by NAT or the one specified manually in the TCP/IP configuration dialog, and by "static IP" you mean the IP address of the gateway (router), you can't retrieve the "static IP" without issuing a network request, nor should you have to. Most routers allow for port forwarding, which will easily deal with issues such as this. Quote Posting Guidelines
Talk2Tom11 Posted January 30, 2005 Author Posted January 30, 2005 umm... ok. I kind of understand what you are saying. I am not all to familiar with this stuff, but how would I get my server to look for that other IP and then listen on it Quote
twistedm1nd Posted February 1, 2005 Posted February 1, 2005 As long as the client knows the static IP of the server he can connect to it on port 399 ( in yer case) your existing server code should work just fine. You can access http://www.whatsmyip.org from a browser on the machine thats gonna host the server to get the static IP of the machine [ im sure there are easier methods butthis is the one that i'm aware of ] the above code snipit just ensures that the machine running has an IP, and then uses that address to bind a socket and listen for requests. ther real work happens after you call accept ie Socket requestHandler = listener.Accept(); Quote
Talk2Tom11 Posted February 1, 2005 Author Posted February 1, 2005 Thank you very much. I was wondering where to place that piece of code in my server. Socket requestHandler = listener.Accept(); Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.