aspose file tools*
The moose likes Beginning Java and the fly likes A good, simple tutorial for making sockets work over the Internet. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A good, simple tutorial for making sockets work over the Internet." Watch "A good, simple tutorial for making sockets work over the Internet." New topic
Author

A good, simple tutorial for making sockets work over the Internet.

John McClellan
Ranch Hand

Joined: Aug 15, 2011
Posts: 35
Hello. Yesterday and today I've learned a few basic things about making sockets in Java an using them to create client/server-type applications. The problem here is that I'm not seeing much in the way of really simple, beginner's tutorials about how to apply that across more than a single computer (specifically across the Internet). All I've seen so far at least looks like it would only work on a single computer (they use localhost as the server, don't get into URLs, etc.). Does anybody have a good link to such a tutorial please? Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

The reason you aren't seeing those specialized tutorials is that there isn't any difference between a socket connecting your application to a server on localhost and a socket connecting your browser to www.coderanch.com. You don't have to write your code any differently either.

Do you have a specific problem behind this question, or are you just asking out of curiosity?
John McClellan
Ranch Hand

Joined: Aug 15, 2011
Posts: 35
Well, I haven't actually tried doing this across multiple computers, but it's just hard for me to see how it would know which thing to connect to if I didn't pass it some sort of information that describes where the server is and what it is. Below I've included the actual code I was using for the client side. What would I have to change if I were to try to access the server across the Internet, instead of on the same computer?

John McClellan
Ranch Hand

Joined: Aug 15, 2011
Posts: 35
I mean, it probably has something to do with changing the name "localhost" to something different, but I'm not sure of the specifics. Also would it still be using that port number the same way?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Spot on: you'd change "localhost" to the name or IP address of the machine you're trying to connect to, and you're in business. Ports are the same everywhere.

Note that you may encounter problems using Reader and Writer classes with sockets: http://www.fenestrated.net/~macman/mirrors/Apple%20Technotes%20%28As%20of%202002%29/tn/tn1157.html. But the code seems to be aware of how to handle newlines, so you're probably good.
John McClellan
Ranch Hand

Joined: Aug 15, 2011
Posts: 35
Now what I'm having trouble with is the following: I'm using a wireless router, and when I call InetAddress.getByName(<some sort of internal, local ip>) on my own computer, that works just fine. But then when I try to call InetAddress.getByName(<the router's ip>), either on my own computer or on another one on the same router, that doesn't work. What's the problem?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

In what way doesn't it work?
John McClellan
Ranch Hand

Joined: Aug 15, 2011
Posts: 35
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Check your firewall settings.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John McClellan
Ranch Hand

Joined: Aug 15, 2011
Posts: 35
I've tried turning it off and running the program, but I still got the same error.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
John:

Did you try running it against localhost? What was the output?

John.
John McClellan
Ranch Hand

Joined: Aug 15, 2011
Posts: 35


The nature of this output has to do with what the program was made to do.
Keerthivasan Ramanathan
Greenhorn

Joined: Mar 23, 2011
Posts: 11
you are getting this exception because the socket server is not running at the specified port of the specified host. When you get 'Connection refused', it is either your socket server doesn't run or firewall/security rights issue.
Keerthivasan Ramanathan
Greenhorn

Joined: Mar 23, 2011
Posts: 11
I forgot to add one more. this exception will occur, when the maximum incoming requests queue of the socket server is full.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A good, simple tutorial for making sockets work over the Internet.