This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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!
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?
Joined: Aug 15, 2011
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?
Joined: Aug 15, 2011
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?
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?
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.
Joined: Mar 23, 2011
I forgot to add one more. this exception will occur, when the maximum incoming requests queue of the socket server is full.