5 Replies Latest reply: Jul 20, 2011 6:24 PM by jhovarie RSS

    can we use ServerSocket and Socket for Wide area network?

    jhovarie
      cross posted http://www.coderanch.com/t/546244/sockets/java/we-ServerSocket-Socket-Wide-area#2478571

      Hi im not sure if this is the right place to post this question. you know guys ServerSocket and Socket is very common in networking because it is always use in local area network and my question is can we use ServerSocket and Socket for wide area network? if not what is the code to achieve wide area network using java?

      i ask this because i use ServerSocket and Socket in local-area network and now i want to connect my computer to a long distance example my computer is in the country Philippines and my friends computer is in USA how to connect them like client server?

      is this possible using ServerSocket and Socket? if not what are the code to achieve that?

      im asking this because i want to create chat application like what i did using ServerSocket and Socket in local-area network.
      then now i want to create chat application in wide area network but i dont know how. plss.. suggest.

      Edited by: jhovarie on Jul 21, 2011 3:58 AM
        • 1. Re: can we use ServerSocket and Socket for Wide area network?
          801313
          The same basic tenets exist, and it's definitely possible to use ServerSocket to achieve this. But you will have to deal with firewall issues - which isn't necessarily a java issue - you need to set up your router etc, and you need to know the IP address of where you're trying to connect to. Unless you have a static IP (if you're not sure, you don't - you probably don't) you really need to have a web server intermediary with a static IP that both parties can connect to that will connect both ends.

          Edited by: EJP on 21/07/2011 09:09: tenants -> tenets, assume that's what you meant?
          • 2. Re: can we use ServerSocket and Socket for Wide area network?
            796440
            Yes, you can use Socket and ServerSocket on a WAN. Your code can never know or care whether it's talking on a LAN or WAN. That's all handled at a lower layer that Java does not expose. All you have are IP addresses, sockets, and streams.
            • 3. Re: can we use ServerSocket and Socket for Wide area network?
              796283
              tjacobs01 wrote:
              you really need to have a web server intermediary with a static IP
              Even then, it doesn't have to be a static IP (or even known). If the web server has a registered domain name, java can just as easily connect to the server by the host name. I've been doing that recently while attempting to learn about the javax.sound.sampled package by making a mini-skype program with the server running on my website.
              • 4. Re: can we use ServerSocket and Socket for Wide area network?
                jhovarie
                "jverd" Yes, you can use Socket and ServerSocket on a WAN. Your code can never know or care whether it's talking on a LAN or WAN. That's all handled at a lower layer that Java does not expose. All you have are IP addresses, sockets, and streams.


                hi in local area network this is working

                //server
                ServerSocket ss = new ServerSocket(2346);

                //client
                String hostname = "PCNaameHere";
                Socket theSocket = new Socket(hostname, 2345);

                then my question is some IP addresses are the same.
                correct me if im wrong is IP addresses thesame with TCP IP? if not where to find IP addresses that is needed to use for WAN network.

                what kind of IP address is needed?
                on client side problem.
                String hostname = "PCNaameHere"; //what should i put here.
                Socket theSocket = new Socket(hostname, 2345);


                hey i tried this code


                import java.net.*;
                import java.util.*;

                public class IPAddress {
                public void getInterfaces (){
                try {
                Enumeration e = NetworkInterface.getNetworkInterfaces();

                while(e.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) e.nextElement();
                System.out.println("Net interface: "+ni.getName());

                Enumeration e2 = ni.getInetAddresses();

                while (e2.hasMoreElements()){
                InetAddress ip = (InetAddress) e2.nextElement();
                System.out.println("IP address: "+ ip.toString());
                }
                }
                }
                catch (Exception e) {
                e.printStackTrace();
                }
                }

                public static void main(String[] args) {
                IPAddress ip = new IPAddress();
                ip.getInterfaces();
                }
                }

                then the output is

                Net interface: lo
                IP address: /127.0.0.1
                Net interface: eth0
                Net interface: eth1
                IP address: /192.168.50.1
                Net interface: ppp0
                IP address: /10.157.10.50

                is the last result "IP address: /10.157.10.50" is the ip address that is needed for
                String hostname = "PCNaameHere"; //<-- is this IP address: /10.157.10.50 what you are saying?
                Socket theSocket = new Socket(hostname, 2345);

                Edited by: 871484 on Jul 20, 2011 4:20 PM
                • 5. Re: can we use ServerSocket and Socket for Wide area network?
                  796440
                  Your question at this point has nothing to do with Java. It is purely a networking question.

                  On a LAN, such as your home or work network, your computers will (if everything is properly configured), have private IP addresses, such as 10.x.y.z or 192.168.x.y (http://en.wikipedia.org/wiki/Private_ip_address). An unlimited number of computers throughout the universe can have the same private IP address, since they are not intended to be addressable from the internet in general, only within the LAN.

                  If you want to open a connection to one of these computers from outside the network, then 1) An internet-facing router has to be listening on whatever port the client is trying to connect to, 2) That router has to be forwarding traffic on that port to some port on your server on your LAN, 3) Your server has to be listening on that port, and 4) The client has to know, or be able to look up (such as via DNS) the public IP address of your router.

                  The details of setting up port forwarding have nothing to do with Java (your code will never know the difference) and are not an appropriate topic for these forums.

                  Note that your employer or ISP may disallow some or all incoming connections to that router. That is also not an issue for this forum.