• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 28330
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Saloon Keeper
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28330
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In what way doesn't it work?
 
John McClellan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check your firewall settings.
 
John McClellan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried turning it off and running the program, but I still got the same error.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John:

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

John.
 
John McClellan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The nature of this output has to do with what the program was made to do.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to add one more. this exception will occur, when the maximum incoming requests queue of the socket server is full.
 
Goodbye moon men. Hello tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic