wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes can we use ServerSocket and Socket for Wide area network? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "can we use ServerSocket and Socket for Wide area network?" Watch "can we use ServerSocket and Socket for Wide area network?" New topic
Author

can we use ServerSocket and Socket for Wide area network?

Jhovarie Guiang
Greenhorn

Joined: Jul 13, 2011
Posts: 15
cross posted http://forums.oracle.com/forums/message.jspa?messageID=9743986#9743986

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

ServerSocket and Socket have no limitations of their own for their connectivity, so yes, you can use ServerSocket and Socket for the WAN. However, you will need to configure your firewall / router to allow traffic from the WAN to the machine with the ServerSocket on that specific port. Otherwise the firewall / router will block the connections.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41115
    
  45
You'd be using the same classes. The principal difficulty will be to make a network connection to the target machine. If it's your friend's home machine, he can just open his firewall on the port you intend to use. But if he's on a larger network (like a company network), incoming traffic will be blocked.

Which is pretty much what Rob Said, I now see...


Ping & DNS - my free Android networking tools app
Jhovarie Guiang
Greenhorn

Joined: Jul 13, 2011
Posts: 15
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);

i tried this code



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 //< is this the ip that is needed?
Socket theSocket = new Socket(hostname, 2345);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

You'd need the external IP, or WAN IP, of the server*. Your client socket then sends the request through the WAN to that IP address. At that end there are two options:
1) The server PC is directly connected with that IP address (which is usually less secure). The connection is made directly, unless the PC's firewall software blocks it.
2) There is a firewall / router (let's call it FW) connected with that IP address, and the server PC is connected to the FW. The FW needs to redirect the connection to the server PC. This is called port forwarding. You'll need to check out the FW manual for more information on how to do this.

Sometimes it's also possible to use a hostname for the server, but one needs to be configured using DNS. The hostname is actually little more than an alias for the IP address.


* You can use sites like http://www.whatsmyip.org/ to find out what the WAN IP is.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: can we use ServerSocket and Socket for Wide area network?
 
Similar Threads
Is there a mirror of java.net downloads
question regarding server socket if server socket is other machine
Struts2 - Dynamic values in expression while doing validations using annotations?
ORA-01874: time zone hour must be between -12 and 14
ejbSelect and the Max() function