java.net.ConnectException: Connection refused

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

java.net.ConnectException: Connection refused

 
0
  #1
Jun 25th, 2008
new to network stuff going through the java tutorials and i did what it said heres the code:

  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class EchoClient {
  5. public static void main(String[] args) throws IOException {
  6.  
  7. Socket echoSocket = null;
  8. PrintWriter out = null;
  9. BufferedReader in = null;
  10.  
  11. try {
  12. echoSocket = new Socket("bill-laptop", 7);
  13. try{
  14. out = new PrintWriter(echoSocket.getOutputStream(), true);
  15. } catch (IOException e) {
  16. System.err.println("Couldn't get I/O for "
  17. + "the connection to: OUT.");
  18. System.exit(1);
  19. }
  20. try{
  21. in = new BufferedReader(new InputStreamReader(
  22. echoSocket.getInputStream()));
  23. } catch (IOException e) {
  24. System.err.println("Couldn't get I/O for "
  25. + "the connection to: IN.");
  26. System.exit(1);
  27. }
  28. } catch (UnknownHostException e) {
  29. System.err.println("Don't know about host: taranis.");
  30. System.exit(1);
  31. }
  32.  
  33. BufferedReader stdIn = new BufferedReader(
  34. new InputStreamReader(System.in));
  35. String userInput;
  36.  
  37. while ((userInput = stdIn.readLine()) != null) {
  38. out.println(userInput);
  39. System.out.println("echo: " + in.readLine());
  40. }
  41.  
  42. out.close();
  43. in.close();
  44. stdIn.close();
  45. echoSocket.close();
  46. }
  47. }

bill-laptop is the name of the computer that i am trying to run this on here is the stack trace:
  1. java.net.ConnectException: Connection refused: connect
  2. at java.net.PlainSocketImpl.socketConnect(Native Method)
  3. at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
  4. at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
  5. at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
  6. at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
  7. at java.net.Socket.connect(Socket.java:519)
  8. at java.net.Socket.connect(Socket.java:469)
  9. at java.net.Socket.<init>(Socket.java:366)
  10. at java.net.Socket.<init>(Socket.java:180)
  11. at EchoClient.main(EchoClient.java:12)

running on a vista :-( but the laptop is brand new duel-core :-)
any advise
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 811
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: java.net.ConnectException: Connection refused

 
0
  #2
Jun 27th, 2008
I know that your tutorial probably says to connect to port 7 for an echo, but try connecting to a port > 1024. Chances are your errors are due to trying to connect to a port that is in use.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: java.net.ConnectException: Connection refused

 
0
  #3
Jun 27th, 2008
thanks for the reply

i tried it, but it didn't work
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 811
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: java.net.ConnectException: Connection refused

 
0
  #4
Jun 27th, 2008
Try replacing your computer name with your IP Address. You will need to create an InetAddress object using your IP and pass that object into the constructor for Socket.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: java.net.ConnectException: Connection refused

 
0
  #5
Jun 28th, 2008
For running the program mentioned, you need an echo server running on your system, which is provided by most UNIX machines and runs on port no 7.
To check if your Vista box has one running just hit telnet localhost 7 , if it shows connection refused or could not connect, then it means you box does not have an echo server, and hence your program will not work and thats the reason I feel for your ConnectException exception.
I know that your tutorial probably says to connect to port 7 for an echo, but try connecting to a port > 1024. Chances are your errors are due to trying to connect to a port that is in use.
Now this prob would have occurred if we were trying to create a ServerSocket on the given port, But as we are creating a client socket here to connect on the given port we apparently want the opposite, that is the port should be in use by the "echo server" so that we can connect to it.
Last edited by stephen84s; Jun 28th, 2008 at 6:36 am. Reason: Spell mistakes
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1
Reputation: sri.sista is an unknown quantity at this point 
Solved Threads: 1
sri.sista sri.sista is offline Offline
Newbie Poster

Re: java.net.ConnectException: Connection refused

 
0
  #6
Jul 18th, 2008
I too have the same problem.If u get any solution pls let me.
Originally Posted by sciwizeh View Post
new to network stuff going through the java tutorials and i did what it said heres the code:

  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class EchoClient {
  5. public static void main(String[] args) throws IOException {
  6.  
  7. Socket echoSocket = null;
  8. PrintWriter out = null;
  9. BufferedReader in = null;
  10.  
  11. try {
  12. echoSocket = new Socket("bill-laptop", 7);
  13. try{
  14. out = new PrintWriter(echoSocket.getOutputStream(), true);
  15. } catch (IOException e) {
  16. System.err.println("Couldn't get I/O for "
  17. + "the connection to: OUT.");
  18. System.exit(1);
  19. }
  20. try{
  21. in = new BufferedReader(new InputStreamReader(
  22. echoSocket.getInputStream()));
  23. } catch (IOException e) {
  24. System.err.println("Couldn't get I/O for "
  25. + "the connection to: IN.");
  26. System.exit(1);
  27. }
  28. } catch (UnknownHostException e) {
  29. System.err.println("Don't know about host: taranis.");
  30. System.exit(1);
  31. }
  32.  
  33. BufferedReader stdIn = new BufferedReader(
  34. new InputStreamReader(System.in));
  35. String userInput;
  36.  
  37. while ((userInput = stdIn.readLine()) != null) {
  38. out.println(userInput);
  39. System.out.println("echo: " + in.readLine());
  40. }
  41.  
  42. out.close();
  43. in.close();
  44. stdIn.close();
  45. echoSocket.close();
  46. }
  47. }

bill-laptop is the name of the computer that i am trying to run this on here is the stack trace:
  1. java.net.ConnectException: Connection refused: connect
  2. at java.net.PlainSocketImpl.socketConnect(Native Method)
  3. at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
  4. at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
  5. at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
  6. at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
  7. at java.net.Socket.connect(Socket.java:519)
  8. at java.net.Socket.connect(Socket.java:469)
  9. at java.net.Socket.<init>(Socket.java:366)
  10. at java.net.Socket.<init>(Socket.java:180)
  11. at EchoClient.main(EchoClient.java:12)

running on a vista :-( but the laptop is brand new duel-core :-)
any advise
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: java.net.ConnectException: Connection refused

 
0
  #7
Jul 18th, 2008
Have you tried what I had mentioned in my previous post ?????
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: java.net.ConnectException: Connection refused

 
0
  #8
Jul 18th, 2008
thanks for the replies,

telnet? i've never used telnet, is it command prompt? if so this doesn't have it, sorry for my ignorance

i think it may just be vista refusing the VM access, but other things that use java.net will run
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: java.net.ConnectException: Connection refused

 
0
  #9
Jul 18th, 2008
ok i figured out telnet and telnet localhost 7 did give connection failed, so what should i do to get an echo server running?
Last edited by sciwizeh; Jul 18th, 2008 at 11:56 am.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 8
Reputation: amjad277 is an unknown quantity at this point 
Solved Threads: 1
amjad277 amjad277 is offline Offline
Newbie Poster

Re: java.net.ConnectException: Connection refused

 
0
  #10
Jul 18th, 2008
I used your code and executed it with localhost (as name of my computer) and the port 4000.
echoSocket = new Socket("localhost", 4000);
It works without errors.

you can use code for server like this:
  1. try{
  2. serverSocket = new ServerSocket(4000);
  3. } catch (IOException e) {
  4. System.err.println("Could not listen on port: 4000.");
  5. System.exit(1);
  6. }
  7. Socket clientSocket = null;
  8. try {
  9. clientSocket = serverSocket.accept();
  10. } catch (IOException e) {
  11. System.err.println("Accept failed.");
  12. System.exit(1);
  13. }
  14. out = new DataOutputStream(clientSocket.getOutputStream());
  15. is = clientSocket.getInputStream();
Last edited by Tekmaven; Jul 18th, 2008 at 6:15 pm. Reason: Code tags
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 17549 | Replies: 19
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC